7cfd2cab390266ae169a13da6efcb630e970e9cd
[utils] / system / general / src / main / java / org / wamblee / system / core / DefaultScope.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.core;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class DefaultScope implements Scope {
33     /**
34      * DOCUMENT ME!
35      */
36     private List<Scope> parents;
37
38     /**
39      * DOCUMENT ME!
40      */
41     private Map<String, Object> properties;
42
43     /**
44      * DOCUMENT ME!
45      */
46     private Map<String, Object> runtimes;
47
48     /**
49      * DOCUMENT ME!
50      */
51     private Map<ProvidedInterface, ProvidedInterfaceImplementation> provided;
52
53     /**
54      * DOCUMENT ME!
55      */
56     private List<ProvidedInterface> externallyProvided;
57
58 /**
59      * Creates a new DefaultScope object.
60      *
61      * @param aExternallyProvided DOCUMENT ME!
62      */
63     public DefaultScope(List<ProvidedInterface> aExternallyProvided) {
64         this(aExternallyProvided.toArray(new ProvidedInterface[0]));
65     }
66
67 /**
68      * Creates a new DefaultScope object.
69      *
70      * @param aExternallyProvided DOCUMENT ME!
71      */
72     public DefaultScope(ProvidedInterface[] aExternallyProvided) {
73         this(aExternallyProvided, new ArrayList<Scope>());
74     }
75
76 /**
77      * Creates a new DefaultScope object.
78      *
79      * @param aExternallyProvided DOCUMENT ME!
80      * @param aParent DOCUMENT ME!
81      */
82     public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
83         this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
84     }
85
86 /**
87      * Creates a new DefaultScope object.
88      *
89      * @param aExternallyProvided DOCUMENT ME!
90      * @param aParent DOCUMENT ME!
91      */
92     public DefaultScope(ProvidedInterface[] aExternallyProvided,
93         List<Scope> aParent) {
94         parents                = new ArrayList<Scope>(aParent);
95         properties             = new HashMap<String, Object>();
96         runtimes               = new HashMap<String, Object>();
97         provided               = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
98         externallyProvided     = new ArrayList<ProvidedInterface>();
99         externallyProvided.addAll(Arrays.asList(aExternallyProvided));
100     }
101
102     /**
103      * DOCUMENT ME!
104      *
105      * @return DOCUMENT ME!
106      */
107     @Override
108     public List<ProvidedInterface> getProvidedInterfaces() {
109         return Collections.unmodifiableList(externallyProvided);
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @param aKey DOCUMENT ME!
116      *
117      * @return DOCUMENT ME!
118      */
119     @Override
120     public Object get(String aKey) {
121         return properties.get(aKey);
122     }
123
124     /**
125      * DOCUMENT ME!
126      *
127      * @param aKey DOCUMENT ME!
128      * @param aValue DOCUMENT ME!
129      */
130     @Override
131     public void put(String aKey, Object aValue) {
132         properties.put(aKey, aValue);
133     }
134
135     /**
136      * DOCUMENT ME!
137      *
138      * @param aComponent DOCUMENT ME!
139      * @param aRuntime DOCUMENT ME!
140      */
141     @Override
142     public void addRuntime(Component aComponent, Object aRuntime) {
143         runtimes.put(aComponent.getName(), aRuntime);
144     }
145
146     /**
147      * DOCUMENT ME!
148      *
149      * @param aComponent DOCUMENT ME!
150      *
151      * @return DOCUMENT ME!
152      */
153     @Override
154     public Object getRuntime(Component aComponent) {
155         return runtimes.get(aComponent.getName());
156     }
157
158     /**
159      * DOCUMENT ME!
160      *
161      * @param aName DOCUMENT ME!
162      *
163      * @return DOCUMENT ME!
164      */
165     @Override
166     public Object getRuntime(String aName) {
167         return runtimes.get(aName);
168     }
169
170     /**
171      * DOCUMENT ME!
172      *
173      * @param aInterface DOCUMENT ME!
174      * @param aImplementation DOCUMENT ME!
175      */
176     @Override
177     synchronized public void publishInterface(ProvidedInterface aInterface,
178         Object aImplementation) {
179         provided.put(aInterface,
180             new ProvidedInterfaceImplementation(aInterface, aImplementation));
181     }
182
183     /**
184      * DOCUMENT ME!
185      *
186      * @param <T> DOCUMENT ME!
187      * @param aInterface DOCUMENT ME!
188      * @param aType DOCUMENT ME!
189      *
190      * @return DOCUMENT ME!
191      */
192     @Override
193     public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
194         Class<T> aType) {
195         if (aInterface == null) {
196             return null;
197         }
198
199         ProvidedInterfaceImplementation providedIntf = provided.get(aInterface);
200
201         if (providedIntf == null) {
202             for (Scope parent : parents) {
203                 T impl = parent.getInterfaceImplementation(aInterface, aType);
204
205                 if (impl != null) {
206                     return impl;
207                 }
208             }
209
210             return null;
211         } else {
212             return providedIntf.getImplementation(aType);
213         }
214     }
215 }