+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.system.core;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+public class DefaultScope implements Scope {
+
+ private List<Scope> _parents;
+ private int _count;
+ private Map<String, Object> _properties;
+ private Map<String, Object> _runtimes;
+ private Map<String, ProvidedInterfaceImplementation> _provided;
+ private ProvidedInterface[] _externallyProvided;
+
+ public DefaultScope(ProvidedInterface[] aExternallyProvided) {
+ this(aExternallyProvided, new ArrayList<Scope>());
+ }
+
+ public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
+ this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
+ }
+
+ public DefaultScope(ProvidedInterface[] aExternallyProvided,
+ List<Scope> aParent) {
+ _parents = new ArrayList<Scope>(aParent);
+ _count = 0;
+ _properties = new TreeMap<String, Object>();
+ _runtimes = new TreeMap<String, Object>();
+ _provided = new TreeMap<String, ProvidedInterfaceImplementation>();
+ _externallyProvided = aExternallyProvided;
+ }
+
+ @Override
+ public ProvidedInterface[] getProvidedInterfaces() {
+ return _externallyProvided;
+ }
+
+ @Override
+ public Object get(String aKey) {
+ return _properties.get(aKey);
+ }
+
+ @Override
+ public void put(String aKey, Object aValue) {
+ _properties.put(aKey, aValue);
+ }
+
+ @Override
+ public void addRuntime(Component aComponent, Object aRuntime) {
+ _runtimes.put(aComponent.getQualifiedName(), aRuntime);
+ }
+
+ @Override
+ public Object getRuntime(Component aComponent) {
+ return _runtimes.get(aComponent.getQualifiedName());
+ }
+
+ @Override
+ synchronized public void publishInterface(ProvidedInterface aInterface,
+ Object aImplementation) {
+ String id = "" + _count++;
+ _provided.put(id, new ProvidedInterfaceImplementation(aInterface,
+ aImplementation));
+ aInterface.setUniqueId(id);
+ }
+
+ @Override
+ public <T> T retrieveInterfaceImplementation(ProvidedInterface aInterface,
+ Class<T> aType) {
+ if ( aInterface == null ) {
+ return null;
+ }
+ String id = aInterface.getUniqueId();
+ if ( id == null ) {
+ // optional interface that was not published.
+ return null;
+ }
+ ProvidedInterfaceImplementation provided = _provided.get(id);
+ if (provided == null) {
+ for (Scope parent : _parents) {
+ T impl = parent.retrieveInterfaceImplementation(aInterface, aType);
+ if ( impl != null ) {
+ return impl;
+ }
+ }
+ return null;
+ } else {
+ return provided.getImplementation(aType);
+ }
+ }
+}