Removed DOCUMENT ME comments that were generated and applied source code
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / CheckExternallyRequiredVisitor.java
1 /*
2  * Copyright 2008 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.graph.component;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.wamblee.system.core.SystemAssemblyException;
22 import org.wamblee.system.graph.Edge;
23 import org.wamblee.system.graph.Graph;
24 import org.wamblee.system.graph.Node;
25 import org.wamblee.system.graph.Visitor;
26
27 import java.util.List;
28
29 /**
30  * Visitor that checks whether all required external interfaces of the container
31  * are provided.
32  * 
33  * @author Erik Brakkee
34  */
35 public class CheckExternallyRequiredVisitor implements Visitor {
36     private Log LOG = LogFactory.getLog(CheckExternallyRequiredVisitor.class);
37
38     private Graph graph;
39
40     /**
41      * Creates a new CheckExternallyRequiredVisitor object.
42      * 
43      */
44     public CheckExternallyRequiredVisitor(Graph aGraph) {
45         graph = aGraph;
46     }
47
48     @Override
49     public void visitEdge(Edge aEdge) {
50         // Empty.
51     }
52
53     @Override
54     public void visitNode(Node aNode) {
55         if (aNode instanceof ExternalRequiredInterfaceNode) {
56             ExternalRequiredInterfaceNode required = (ExternalRequiredInterfaceNode) aNode;
57
58             if (!required.getRequired().isOptional() &&
59                 (required.getRequired().getProvider() == null)) {
60                 throw new SystemAssemblyException(aNode +
61                     ": External required interface is not provided");
62             }
63
64             List<Edge> edges = graph.findIncoming(aNode);
65
66             if (edges.isEmpty()) {
67                 LOG.warn(aNode + ": Superfluous required interface");
68             }
69
70             for (Edge edge : edges) {
71                 Node from = edge.getFrom();
72                 assert from instanceof RequiredInterfaceNode;
73
74                 RequiredInterfaceNode reqNode = (RequiredInterfaceNode) from;
75
76                 if (!reqNode.getRequired().isOptional() &&
77                     required.getRequired().isOptional()) {
78                     throw new SystemAssemblyException(
79                         aNode +
80                             ": externally required interface is optional but a corresponding internal required interface is mandatory: " +
81                             reqNode);
82                 }
83             }
84         }
85     }
86 }