From f0181a0f88c8e0cccd71024d0eddd6a74589d890 Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 12 May 2008 11:21:44 +0000 Subject: [PATCH] Added ObjectConfiguration as a holder for setter configuration (there will be more configuration for objects in the future that is unrelated to setters). Also added addComponent methods for objects to defaultcontainer. --- .../system/adapters/ClassConfiguration.java | 12 ++-- .../system/adapters/DefaultContainer.java | 14 +++++ .../system/adapters/ObjectAdapter.java | 10 ++-- .../system/adapters/ObjectConfiguration.java | 56 +++++++++++++++++++ .../adapters/ClassConfigurationTest.java | 2 +- .../system/adapters/DefaultContainerTest.java | 34 +++++++++++ .../system/adapters/ObjectAdapterTest.java | 2 +- 7 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java diff --git a/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java index cff3cd04..0fee7aca 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java @@ -33,7 +33,7 @@ public class ClassConfiguration { private Class _class; private ConstructorConfiguration _constructorConfig; - private SetterConfiguration _setterConfig; + private ObjectConfiguration _objectConfig; /** * Constructs the configuration. By default no constructor is selected and @@ -44,15 +44,15 @@ public class ClassConfiguration { public ClassConfiguration(Class aClass) { _class = aClass; _constructorConfig = new ConstructorConfiguration(aClass); - _setterConfig = new SetterConfiguration(aClass); + _objectConfig = new ObjectConfiguration(aClass); } public ConstructorConfiguration getConstructorConfig() { return _constructorConfig; } - public SetterConfiguration getSetterConfiguration() { - return _setterConfig; + public ObjectConfiguration getObjectConfig() { + return _objectConfig; } /** @@ -70,7 +70,7 @@ public class ClassConfiguration { * @param aScope Scope in which injection takes place. */ public void inject(Scope aScope, Object aObject) { - _setterConfig.inject(aScope, aObject); + _objectConfig.inject(aScope, aObject); } public List getProvidedInterfaces() { @@ -82,7 +82,7 @@ public class ClassConfiguration { public List getRequiredInterfaces() { List result = new ArrayList(); result.addAll(_constructorConfig.getRequiredInterfaces()); - result.addAll(_setterConfig.getRequiredInterfaces()); + result.addAll(_objectConfig.getRequiredInterfaces()); return result; } } diff --git a/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java b/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java index c54206ea..c8e8f58a 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java @@ -41,6 +41,20 @@ public class DefaultContainer extends Container { return addComponent(new ClassAdapter(aName, aConfiguration)); } + public DefaultContainer addComponent(String aName, Object aObject) { + return addComponent(new ObjectAdapter(aName, aObject, new ObjectConfiguration( + aObject.getClass()))); + } + + public DefaultContainer addComponent(String aName, Object aObject, ObjectConfiguration aConfiguration) { + if ( !aConfiguration.appliesTo(aObject) ) { + throw new IllegalArgumentException("Configuration '" + aConfiguration + "' does nto applu to '" + + aObject + "'"); + } + return addComponent(new ObjectAdapter(aName, aObject, aConfiguration)); + } + + @Override public DefaultContainer addRequiredInterface(RequiredInterface aRequired) { super.addRequiredInterface(aRequired); diff --git a/system/general/src/main/java/org/wamblee/system/adapters/ObjectAdapter.java b/system/general/src/main/java/org/wamblee/system/adapters/ObjectAdapter.java index 91b10b33..ff2fa0a2 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/ObjectAdapter.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/ObjectAdapter.java @@ -28,21 +28,21 @@ import org.wamblee.system.core.Scope; */ public class ObjectAdapter extends AbstractComponent { - private SetterConfiguration _setterConfig; + private ObjectConfiguration _objectConfig; private Object _object; - public ObjectAdapter(String aName, Object aObject, SetterConfiguration aSetterConfig) { + public ObjectAdapter(String aName, Object aObject, ObjectConfiguration aObjectConfig) { super(aName, new ProvidedInterface[] { new DefaultProvidedInterface(aName, aObject.getClass()) }, - aSetterConfig.getRequiredInterfaces().toArray(new RequiredInterface[0])); - _setterConfig = aSetterConfig; + aObjectConfig.getRequiredInterfaces().toArray(new RequiredInterface[0])); + _objectConfig = aObjectConfig; _object = aObject; } @Override protected Object doStart(Scope aScope) { - _setterConfig.inject(aScope, _object); + _objectConfig.inject(aScope, _object); for (ProvidedInterface provided: getProvidedInterfaces()) { addInterface(provided, _object, aScope); diff --git a/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java new file mode 100644 index 00000000..e6f647a0 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java @@ -0,0 +1,56 @@ +/* + * 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.adapters; + +import java.util.ArrayList; +import java.util.List; + +import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.core.Scope; + +/** + * General configuration for an instantiated object. + * + * @author Erik Brakkee + */ +public class ObjectConfiguration { + + private Class _class; + private SetterConfiguration _setterConfig; + + public ObjectConfiguration(Class aClass) { + _class = aClass; + _setterConfig = new SetterConfiguration(aClass); + } + + public void inject(Scope aScope, Object aObject) { + _setterConfig.inject(aScope, aObject); + } + + public SetterConfiguration getSetterConfig() { + return _setterConfig; + } + + public List getRequiredInterfaces() { + List result = new ArrayList(); + result.addAll(_setterConfig.getRequiredInterfaces()); + return result; + } + + public boolean appliesTo(Object aObject) { + return _class.isInstance(aObject); + } +} diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java index 411b86f8..c0fc0398 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java @@ -52,7 +52,7 @@ public class ClassConfigurationTest extends AdapterTestCase { ClassConfiguration classConfig = new ClassConfiguration(X7.class); classConfig.getConstructorConfig().select(Boolean.class); - classConfig.getSetterConfiguration().values("port").setValue(0, 10); + classConfig.getObjectConfig().getSetterConfig().values("port").setValue(0, 10); ProvidedInterface providedBoolean = new DefaultProvidedInterface("boolean", Boolean.class); diff --git a/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java b/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java index 913e3970..07143e3e 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java @@ -36,4 +36,38 @@ public class DefaultContainerTest extends AdapterTestCase { obj = scope.getRuntime("x4"); assertTrue(obj instanceof X4); } + + public void testConstructorInjectionAndSetterInjection() { + ClassConfiguration x1Config = new ClassConfiguration(X1.class); + x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); + + X8 x8 = new X8(null); + EVENT_TRACKER.clear(); + + DefaultContainer container = new DefaultContainer("top").addComponent( + "x1", x1Config).addComponent("x4", X4.class).addComponent("x8", x8); + + Scope scope = container.start(); + AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", + "x8.setX4(x4)"}, + EVENT_TRACKER.getEvents(Thread.currentThread()).toArray()); + + Object obj1 = scope.getRuntime("x1"); + assertTrue(obj1 instanceof X1); + Object obj4 = scope.getRuntime("x4"); + assertTrue(obj4 instanceof X4); + Object obj8 = scope.getRuntime("x8"); + assertSame(x8, obj8); + assertSame(obj4, x8.getX4()); + } + + public void testWrongObjectType() { + final DefaultContainer container = new DefaultContainer("top"); + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + @Override + public void run() throws Exception { + container.addComponent("x", "y", new ObjectConfiguration(Integer.class)); + } + }, IllegalArgumentException.class); + } } diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java index 572629fc..2a06375e 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java @@ -35,7 +35,7 @@ public class ObjectAdapterTest extends AdapterTestCase { x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); ClassConfiguration x4Config = new ClassConfiguration(X4.class); - SetterConfiguration x8Config = new SetterConfiguration(X8.class); + ObjectConfiguration x8Config = new ObjectConfiguration(X8.class); X1 x1 = new X1(); X8 x8 = new X8(x1); -- 2.31.1