(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / container / DefaultInterfaceRestriction.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.container;
17
18 import org.wamblee.system.core.Component;
19 import org.wamblee.system.core.ProvidedInterface;
20 import org.wamblee.system.core.RequiredInterface;
21
22 /**
23  * An interface restriction on the required interface of a component. 
24  * 
25  * @author Erik Brakkee
26  *
27  */
28 public class DefaultInterfaceRestriction implements InterfaceRestriction {
29
30     private String _client;
31     private String _required;
32     private String _server;
33     private String _provided;
34
35     /**
36      * Constructs the restriction. If the client name and required interface
37      * name match, then the server name and interface name must also match. 
38      * Otherwise the restriction is violated. 
39      * 
40      * This can be used to explicitly connect required and provided interfaces
41      * of components. 
42      * 
43      * @param aClient
44      *            Client or null if no restriction on the client.
45      * @param aRequired
46      *            Required interface name or null if no restriction on the
47      *            required interface.
48      * @param aServer
49      *            Server or null if no restriction on the server.
50      * @param aProvided
51      *            Provided interface name or null if no restriction on the
52      *            provided interface.
53      */
54     public DefaultInterfaceRestriction(String aClient, String aRequired,
55             String aServer, String aProvided) {
56         
57         if ( aClient == null && aRequired == null ) { 
58             throw new IllegalArgumentException("No restriction on the required interface");
59         }
60         if ( aServer == null && aProvided == null ) { 
61             throw new IllegalArgumentException("No restriction on the provided interface");
62         }
63         _client = aClient;
64         _required = aRequired;
65         _server = aServer;
66         _provided = aProvided;
67     }
68
69     @Override
70     public boolean isViolated(Component aClient, RequiredInterface aRequired,
71             Component aServer, ProvidedInterface aProvided) {
72         
73         if ( _client != null && !aClient.getName().equals(_client)) { 
74             return false; 
75         }
76         if ( _required != null && !aRequired.getName().equals(_required)) { 
77             return false; 
78         }
79         
80         // The required interface matches
81         
82         if ( _server != null && !aServer.getName().equals(_server)) {
83             // Server was specified and does not match.
84             return true; 
85         }
86         if ( _provided != null && !aProvided.getName().equals(_provided)) { 
87             // provided interface was specified and doe not match. 
88             return true; 
89         }
90         
91         // The required and provided interfaces match so this is not a violation.
92         
93         return false; 
94     }
95
96 }