3e15ea9c8f5a6eec5a9695545d12fdd1c172c6b4
[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 public class DefaultScope implements Scope {
26
27         private List<Scope> parents;
28         private Map<String, Object> properties;
29         private Map<String, Object> runtimes;
30         private Map<ProvidedInterface, ProvidedInterfaceImplementation> provided;
31         private List<ProvidedInterface> externallyProvided;
32         
33         public DefaultScope(List<ProvidedInterface>aExternallyProvided) { 
34             this(aExternallyProvided.toArray(new ProvidedInterface[0]));
35         }
36
37         public DefaultScope(ProvidedInterface[] aExternallyProvided) {
38                 this(aExternallyProvided, new ArrayList<Scope>());
39         }
40
41         public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
42                 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
43         }
44
45         public DefaultScope(ProvidedInterface[] aExternallyProvided,
46                         List<Scope> aParent) {
47                 parents = new ArrayList<Scope>(aParent);
48                 properties = new HashMap<String, Object>();
49                 runtimes = new HashMap<String, Object>();
50                 provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
51                 externallyProvided = new ArrayList<ProvidedInterface>(); 
52                 externallyProvided.addAll(Arrays.asList(aExternallyProvided));
53         }
54
55         @Override
56         public List<ProvidedInterface> getProvidedInterfaces() {
57                 return Collections.unmodifiableList(externallyProvided);
58         }
59
60         @Override
61         public Object get(String aKey) {
62                 return properties.get(aKey);
63         }
64
65         @Override
66         public void put(String aKey, Object aValue) {
67                 properties.put(aKey, aValue);
68         }
69
70         @Override
71         public void addRuntime(Component aComponent, Object aRuntime) {
72                 runtimes.put(aComponent.getName(), aRuntime);
73         }
74
75         @Override
76         public Object getRuntime(Component aComponent) {
77                 return runtimes.get(aComponent.getName());
78         }
79         
80         @Override
81         public Object getRuntime(String aName) {
82             return runtimes.get(aName);
83         }
84
85         @Override
86         synchronized public void publishInterface(ProvidedInterface aInterface,
87                         Object aImplementation) {
88                 provided.put(aInterface, new ProvidedInterfaceImplementation(aInterface,
89                                 aImplementation));
90         }
91
92         @Override
93         public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
94                         Class<T> aType) {
95                 if ( aInterface == null ) { 
96                         return null; 
97                 } 
98                 ProvidedInterfaceImplementation providedIntf = provided.get(aInterface);
99                 if (providedIntf == null) {
100                         for (Scope parent : parents) {
101                                 T impl = parent.getInterfaceImplementation(aInterface, aType);
102                                 if ( impl != null ) { 
103                                         return impl; 
104                                 }
105                         }
106                         return null; 
107                 } else {
108                         return providedIntf.getImplementation(aType);
109                 }
110         }
111 }