2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.core;
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;
32 public class DefaultScope implements Scope {
36 private List<Scope> parents;
41 private Map<String, Object> properties;
46 private Map<String, Object> runtimes;
51 private Map<ProvidedInterface, ProvidedInterfaceImplementation> provided;
56 private List<ProvidedInterface> externallyProvided;
59 * Creates a new DefaultScope object.
61 * @param aExternallyProvided DOCUMENT ME!
63 public DefaultScope(List<ProvidedInterface> aExternallyProvided) {
64 this(aExternallyProvided.toArray(new ProvidedInterface[0]));
68 * Creates a new DefaultScope object.
70 * @param aExternallyProvided DOCUMENT ME!
72 public DefaultScope(ProvidedInterface[] aExternallyProvided) {
73 this(aExternallyProvided, new ArrayList<Scope>());
77 * Creates a new DefaultScope object.
79 * @param aExternallyProvided DOCUMENT ME!
80 * @param aParent DOCUMENT ME!
82 public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
83 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
87 * Creates a new DefaultScope object.
89 * @param aExternallyProvided DOCUMENT ME!
90 * @param aParent DOCUMENT ME!
92 public DefaultScope(ProvidedInterface[] aExternallyProvided,
93 List<Scope> aParent) {
94 parents = new ArrayList<Scope>(aParent);
95 properties = new HashMap<String, Object>();
96 runtimes = new HashMap<String, Object>();
97 provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
98 externallyProvided = new ArrayList<ProvidedInterface>();
99 externallyProvided.addAll(Arrays.asList(aExternallyProvided));
105 * @return DOCUMENT ME!
108 public List<ProvidedInterface> getProvidedInterfaces() {
109 return Collections.unmodifiableList(externallyProvided);
115 * @param aKey DOCUMENT ME!
117 * @return DOCUMENT ME!
120 public Object get(String aKey) {
121 return properties.get(aKey);
127 * @param aKey DOCUMENT ME!
128 * @param aValue DOCUMENT ME!
131 public void put(String aKey, Object aValue) {
132 properties.put(aKey, aValue);
138 * @param aComponent DOCUMENT ME!
139 * @param aRuntime DOCUMENT ME!
142 public void addRuntime(Component aComponent, Object aRuntime) {
143 runtimes.put(aComponent.getName(), aRuntime);
149 * @param aComponent DOCUMENT ME!
151 * @return DOCUMENT ME!
154 public Object getRuntime(Component aComponent) {
155 return runtimes.get(aComponent.getName());
161 * @param aName DOCUMENT ME!
163 * @return DOCUMENT ME!
166 public Object getRuntime(String aName) {
167 return runtimes.get(aName);
173 * @param aInterface DOCUMENT ME!
174 * @param aImplementation DOCUMENT ME!
177 synchronized public void publishInterface(ProvidedInterface aInterface,
178 Object aImplementation) {
179 provided.put(aInterface,
180 new ProvidedInterfaceImplementation(aInterface, aImplementation));
186 * @param <T> DOCUMENT ME!
187 * @param aInterface DOCUMENT ME!
188 * @param aType DOCUMENT ME!
190 * @return DOCUMENT ME!
193 public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
195 if (aInterface == null) {
199 ProvidedInterfaceImplementation providedIntf = provided.get(aInterface);
201 if (providedIntf == null) {
202 for (Scope parent : parents) {
203 T impl = parent.getInterfaceImplementation(aInterface, aType);
212 return providedIntf.getImplementation(aType);