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;
25 public class DefaultScope implements Scope {
27 private List<Scope> _parents;
28 private Map<String, Object> _properties;
29 private Map<String, Object> _runtimes;
30 private Map<ProvidedInterface, ProvidedInterfaceImplementation> _provided;
31 private List<ProvidedInterface> _externallyProvided;
33 public DefaultScope(List<ProvidedInterface>aExternallyProvided) {
34 this(aExternallyProvided.toArray(new ProvidedInterface[0]));
37 public DefaultScope(ProvidedInterface[] aExternallyProvided) {
38 this(aExternallyProvided, new ArrayList<Scope>());
41 public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
42 this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
45 public DefaultScope(ProvidedInterface[] aExternallyProvided,
46 List<Scope> aParent) {
47 _parents = new ArrayList<Scope>(aParent);
48 _properties = new HashMap<String, Object>();
49 _runtimes = new HashMap<String, Object>();
50 _provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
51 _externallyProvided = new ArrayList<ProvidedInterface>();
52 _externallyProvided.addAll(Arrays.asList(aExternallyProvided));
56 public List<ProvidedInterface> getProvidedInterfaces() {
57 return Collections.unmodifiableList(_externallyProvided);
61 public Object get(String aKey) {
62 return _properties.get(aKey);
66 public void put(String aKey, Object aValue) {
67 _properties.put(aKey, aValue);
71 public void addRuntime(Component aComponent, Object aRuntime) {
72 _runtimes.put(aComponent.getName(), aRuntime);
76 public Object getRuntime(Component aComponent) {
77 return _runtimes.get(aComponent.getName());
81 public Object getRuntime(String aName) {
82 return _runtimes.get(aName);
86 synchronized public void publishInterface(ProvidedInterface aInterface,
87 Object aImplementation) {
88 _provided.put(aInterface, new ProvidedInterfaceImplementation(aInterface,
93 public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
95 if ( aInterface == null ) {
98 ProvidedInterfaceImplementation provided = _provided.get(aInterface);
99 if (provided == null) {
100 for (Scope parent : _parents) {
101 T impl = parent.getInterfaceImplementation(aInterface, aType);
102 if ( impl != null ) {
108 return provided.getImplementation(aType);