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