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;
24 import java.util.TreeMap;
25 import java.util.UUID;
27 public class DefaultScope implements Scope {
29 private List<Scope> _parents;
30 private Map<String, Object> _properties;
31 private Map<String, Object> _runtimes;
32 private Map<ProvidedInterface, ProvidedInterfaceImplementation> _provided;
33 private List<ProvidedInterface> _externallyProvided;
35 public DefaultScope(List<ProvidedInterface>aExternallyProvided) {
36 this(aExternallyProvided.toArray(new ProvidedInterface[0]));
39 public DefaultScope(ProvidedInterface[] aExternallyProvided) {
40 this(aExternallyProvided, new ArrayList<Scope>());
43 public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
44 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
47 public DefaultScope(ProvidedInterface[] aExternallyProvided,
48 List<Scope> aParent) {
49 _parents = new ArrayList<Scope>(aParent);
50 _properties = new HashMap<String, Object>();
51 _runtimes = new HashMap<String, Object>();
52 _provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
53 _externallyProvided = new ArrayList<ProvidedInterface>();
54 _externallyProvided.addAll(Arrays.asList(aExternallyProvided));
58 public List<ProvidedInterface> getProvidedInterfaces() {
59 return Collections.unmodifiableList(_externallyProvided);
63 public Object get(String aKey) {
64 return _properties.get(aKey);
68 public void put(String aKey, Object aValue) {
69 _properties.put(aKey, aValue);
73 public void addRuntime(Component aComponent, Object aRuntime) {
74 _runtimes.put(aComponent.getName(), aRuntime);
78 public Object getRuntime(Component aComponent) {
79 return _runtimes.get(aComponent.getName());
83 public Object getRuntime(String aName) {
84 return _runtimes.get(aName);
88 synchronized public void publishInterface(ProvidedInterface aInterface,
89 Object aImplementation) {
90 _provided.put(aInterface, new ProvidedInterfaceImplementation(aInterface,
95 public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
97 if ( aInterface == null ) {
100 ProvidedInterfaceImplementation provided = _provided.get(aInterface);
101 if (provided == null) {
102 for (Scope parent : _parents) {
103 T impl = parent.getInterfaceImplementation(aInterface, aType);
104 if ( impl != null ) {
110 return provided.getImplementation(aType);