Started work on componentizing the current user management.
[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
25 public class DefaultScope implements Scope {
26
27         private List<Scope> _parents;
28         private int _count;
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                 _count = 0;
46                 _properties = new HashMap<String, Object>();
47                 _runtimes = new HashMap<String, Object>();
48                 _provided = new HashMap<String, ProvidedInterfaceImplementation>();
49                 _externallyProvided = aExternallyProvided;
50         }
51
52         @Override
53         public ProvidedInterface[] getProvidedInterfaces() {
54                 return _externallyProvided;
55         }
56
57         @Override
58         public Object get(String aKey) {
59                 return _properties.get(aKey);
60         }
61
62         @Override
63         public void put(String aKey, Object aValue) {
64                 _properties.put(aKey, aValue);
65         }
66
67         @Override
68         public void addRuntime(Component aComponent, Object aRuntime) {
69                 _runtimes.put(aComponent.getName(), aRuntime);
70         }
71
72         @Override
73         public Object getRuntime(Component aComponent) {
74                 return _runtimes.get(aComponent.getName());
75         }
76         
77         @Override
78         public Object getRuntime(String aName) {
79             return _runtimes.get(aName);
80         }
81
82         @Override
83         synchronized public void publishInterface(ProvidedInterface aInterface,
84                         Object aImplementation) {
85                 String id = "" + _count++;
86                 _provided.put(id, new ProvidedInterfaceImplementation(aInterface,
87                                 aImplementation));
88                 aInterface.setUniqueId(id);
89         }
90
91         @Override
92         public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
93                         Class<T> aType) {
94                 if ( aInterface == null ) { 
95                         return null; 
96                 }
97                 String id = aInterface.getUniqueId(); 
98                 if ( id == null ) { 
99                         // optional interface that was not published.
100                         return null;
101                 }
102                 ProvidedInterfaceImplementation provided = _provided.get(id);
103                 if (provided == null) {
104                         for (Scope parent : _parents) {
105                                 T impl = parent.getInterfaceImplementation(aInterface, aType);
106                                 if ( impl != null ) { 
107                                         return impl; 
108                                 }
109                         }
110                         return null; 
111                 } else {
112                         return provided.getImplementation(aType);
113                 }
114         }
115 }