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