Added basic graph functionality as a first step towards simplifying the container...
[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.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24 import java.util.UUID;
25
26 public class DefaultScope implements Scope {
27
28         private List<Scope> _parents;
29         private Map<String, Object> _properties;
30         private Map<String, Object> _runtimes;
31         private Map<String, ProvidedInterfaceImplementation> _provided;
32         private ProvidedInterface[] _externallyProvided;
33
34         public DefaultScope(ProvidedInterface[] aExternallyProvided) {
35                 this(aExternallyProvided, new ArrayList<Scope>());
36         }
37
38         public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
39                 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
40         }
41
42         public DefaultScope(ProvidedInterface[] aExternallyProvided,
43                         List<Scope> aParent) {
44                 _parents = new ArrayList<Scope>(aParent);
45                 _properties = new HashMap<String, Object>();
46                 _runtimes = new HashMap<String, Object>();
47                 _provided = new HashMap<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.getName(), aRuntime);
69         }
70
71         @Override
72         public Object getRuntime(Component aComponent) {
73                 return _runtimes.get(aComponent.getName());
74         }
75         
76         @Override
77         public Object getRuntime(String aName) {
78             return _runtimes.get(aName);
79         }
80
81         @Override
82         synchronized public void publishInterface(ProvidedInterface aInterface,
83                         Object aImplementation) {
84                 String id = UUID.randomUUID().toString();
85                 _provided.put(id, new ProvidedInterfaceImplementation(aInterface,
86                                 aImplementation));
87                 aInterface.setUniqueId(id);
88         }
89
90         @Override
91         public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
92                         Class<T> aType) {
93                 if ( aInterface == null ) { 
94                         return null; 
95                 }
96                 String id = aInterface.getUniqueId(); 
97                 if ( id == null ) { 
98                         // optional interface that was not published.
99                         return null;
100                 }
101                 ProvidedInterfaceImplementation provided = _provided.get(id);
102                 if (provided == null) {
103                         for (Scope parent : _parents) {
104                                 T impl = parent.getInterfaceImplementation(aInterface, aType);
105                                 if ( impl != null ) { 
106                                         return impl; 
107                                 }
108                         }
109                         return null; 
110                 } else {
111                         return provided.getImplementation(aType);
112                 }
113         }
114 }