2 * Copyright 2005-2010 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;
30 public class DefaultScope implements Scope {
31 private List<Scope> parents;
33 private Map<String, Object> properties;
35 private Map<String, Object> runtimes;
37 private Map<ProvidedInterface, ProvidedInterfaceImplementation> provided;
39 private List<ProvidedInterface> externallyProvided;
42 * Creates a new DefaultScope object.
45 public DefaultScope(List<ProvidedInterface> aExternallyProvided) {
46 this(aExternallyProvided.toArray(new ProvidedInterface[0]));
50 * Creates a new DefaultScope object.
53 public DefaultScope(ProvidedInterface[] aExternallyProvided) {
54 this(aExternallyProvided, new ArrayList<Scope>());
58 * Creates a new DefaultScope object.
61 public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
62 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
66 * Creates a new DefaultScope object.
69 public DefaultScope(ProvidedInterface[] aExternallyProvided,
70 List<Scope> aParent) {
71 parents = new ArrayList<Scope>(aParent);
72 properties = new HashMap<String, Object>();
73 runtimes = new HashMap<String, Object>();
74 provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
75 externallyProvided = new ArrayList<ProvidedInterface>();
76 externallyProvided.addAll(Arrays.asList(aExternallyProvided));
80 public List<ProvidedInterface> getProvidedInterfaces() {
81 return Collections.unmodifiableList(externallyProvided);
85 public Object get(String aKey) {
86 return properties.get(aKey);
90 public void put(String aKey, Object aValue) {
91 properties.put(aKey, aValue);
95 public void addRuntime(Component aComponent, Object aRuntime) {
96 runtimes.put(aComponent.getName(), aRuntime);
100 public Object getRuntime(Component aComponent) {
101 return runtimes.get(aComponent.getName());
105 public Object getRuntime(String aName) {
106 return runtimes.get(aName);
110 synchronized public void publishInterface(ProvidedInterface aInterface,
111 Object aImplementation) {
112 provided.put(aInterface, new ProvidedInterfaceImplementation(
113 aInterface, aImplementation));
117 public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
119 if (aInterface == null) {
123 ProvidedInterfaceImplementation providedIntf = provided.get(aInterface);
125 if (providedIntf == null) {
126 for (Scope parent : parents) {
127 T impl = parent.getInterfaceImplementation(aInterface, aType);
136 return providedIntf.getImplementation(aType);