50c6cc3b2ad6ebd682b0f57336963e019b34c1be
[utils] /
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 provided = _provided.get(aInterface);
99                 if (provided == 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 provided.getImplementation(aType);
109                 }
110         }
111 }