457ae1d82942cfbba20e03bb11500ceaccacd3b0
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / CheckExternallyProvidedVisitor.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.wamblee.system.core.SystemAssemblyException;
19 import org.wamblee.system.graph.Edge;
20 import org.wamblee.system.graph.Graph;
21 import org.wamblee.system.graph.Node;
22 import org.wamblee.system.graph.Visitor;
23
24 import java.util.List;
25
26
27 /**
28  * Visitor that checks whether all externally provided interfaces are
29  * actually provided  by any of the internal components.
30  *
31  * @author Erik Brakkee
32  */
33 public class CheckExternallyProvidedVisitor implements Visitor {
34     /**
35      * DOCUMENT ME!
36      */
37     private Graph graph;
38
39 /**
40      * Constructs the visitor. 
41      * @param aGraph Component graph. 
42      */
43     public CheckExternallyProvidedVisitor(Graph aGraph) {
44         graph = aGraph;
45     }
46
47     /**
48      * DOCUMENT ME!
49      *
50      * @param aEdge DOCUMENT ME!
51      */
52     @Override
53     public void visitEdge(Edge aEdge) {
54         // Empty.
55     }
56
57     /**
58      * DOCUMENT ME!
59      *
60      * @param aNode DOCUMENT ME!
61      */
62     @Override
63     public void visitNode(Node aNode) {
64         if (aNode instanceof ExternalProvidedInterfaceNode) {
65             ExternalProvidedInterfaceNode provided = (ExternalProvidedInterfaceNode) aNode;
66             List<Edge>                    edges    = graph
67                 .findOutgoing(provided);
68
69             if (edges.size() > 2) {
70                 createDuplicateException("External provided interfaces has multiple internal matches",
71                     aNode, edges);
72             }
73
74             if (edges.size() == 0) {
75                 throw new SystemAssemblyException(aNode
76                     + ": external provded interface is not provided by any of the internal components");
77             }
78         }
79     }
80
81     /**
82      * DOCUMENT ME!
83      *
84      * @param aMsg DOCUMENT ME!
85      * @param aNode DOCUMENT ME!
86      * @param edges DOCUMENT ME!
87      */
88     private void createDuplicateException(String aMsg, Node aNode,
89         List<Edge> edges) {
90         StringBuffer buf = new StringBuffer();
91         buf.append(aNode + ": " + aMsg + ": ");
92
93         for (Edge edge : edges) {
94             buf.append(edge.getTo() + "/ ");
95         }
96
97         throw new SystemAssemblyException(buf.toString());
98     }
99 }