(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / CheckExternallyRequiredVisitor.java
1 /*
2  * Copyright 2005-2010 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 import java.util.logging.Logger;
26
27 /**
28  * Visitor that checks whether all required external interfaces of the container
29  * are provided.
30  * 
31  * @author Erik Brakkee
32  */
33 public class CheckExternallyRequiredVisitor implements Visitor {
34     private static final Logger LOG = Logger
35         .getLogger(CheckExternallyRequiredVisitor.class.getName());
36
37     private Graph graph;
38
39     /**
40      * Creates a new CheckExternallyRequiredVisitor object.
41      * 
42      */
43     public CheckExternallyRequiredVisitor(Graph aGraph) {
44         graph = aGraph;
45     }
46
47     @Override
48     public void visitEdge(Edge aEdge) {
49         // Empty.
50     }
51
52     @Override
53     public void visitNode(Node aNode) {
54         if (aNode instanceof ExternalRequiredInterfaceNode) {
55             ExternalRequiredInterfaceNode required = (ExternalRequiredInterfaceNode) aNode;
56
57             if (!required.getRequired().isOptional() &&
58                 (required.getRequired().getProvider() == null)) {
59                 throw new SystemAssemblyException(aNode +
60                     ": External required interface is not provided");
61             }
62
63             List<Edge> edges = graph.findIncoming(aNode);
64
65             if (edges.isEmpty()) {
66                 LOG.warning(aNode + ": Superfluous required interface");
67             }
68
69             for (Edge edge : edges) {
70                 Node from = edge.getFrom();
71                 assert from instanceof RequiredInterfaceNode;
72
73                 RequiredInterfaceNode reqNode = (RequiredInterfaceNode) from;
74
75                 if (!reqNode.getRequired().isOptional() &&
76                     required.getRequired().isOptional()) {
77                     throw new SystemAssemblyException(
78                         aNode +
79                             ": externally required interface is optional but a corresponding internal required interface is mandatory: " +
80                             reqNode);
81                 }
82             }
83         }
84     }
85 }