Added ObjectConfiguration as a holder for setter configuration (there will be more...
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 12 May 2008 11:21:44 +0000 (11:21 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 12 May 2008 11:21:44 +0000 (11:21 +0000)
Also added addComponent methods for objects to defaultcontainer.

trunk/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java
trunk/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java
trunk/system/general/src/main/java/org/wamblee/system/adapters/ObjectAdapter.java
trunk/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java [new file with mode: 0644]
trunk/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java
trunk/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java
trunk/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java

index cff3cd042db281fd63668084bb338dfa1256cce1..0fee7aca4d37a4eee5564c5226611dbdfb19cec3 100644 (file)
@@ -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<ProvidedInterface> getProvidedInterfaces() {
@@ -82,7 +82,7 @@ public class ClassConfiguration {
        public List<RequiredInterface> getRequiredInterfaces() { 
            List<RequiredInterface> result = new ArrayList<RequiredInterface>();
            result.addAll(_constructorConfig.getRequiredInterfaces());
-           result.addAll(_setterConfig.getRequiredInterfaces());
+           result.addAll(_objectConfig.getRequiredInterfaces());
            return result; 
        }
 }
index c54206ea23ca3e8dd1f72b543958cda254511676..c8e8f58a0722b8691d0fb5582e1f39659e8771c9 100644 (file)
@@ -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);
index 91b10b334cf0bfc22a4b846b752bae148a13cc96..ff2fa0a253f8436881de66f5e3aa456e96b9cf08 100644 (file)
@@ -28,21 +28,21 @@ import org.wamblee.system.core.Scope;
  */
 public class ObjectAdapter extends AbstractComponent<Object> {
 
-       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/trunk/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java b/trunk/system/general/src/main/java/org/wamblee/system/adapters/ObjectConfiguration.java
new file mode 100644 (file)
index 0000000..e6f647a
--- /dev/null
@@ -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<RequiredInterface> getRequiredInterfaces() {
+        List<RequiredInterface> result = new ArrayList<RequiredInterface>();
+        result.addAll(_setterConfig.getRequiredInterfaces());
+        return result;    
+    }
+
+    public boolean appliesTo(Object aObject) {
+        return _class.isInstance(aObject);
+    }
+}
index 411b86f8ac0e31f3a3895ff00e107f7e185b0b86..c0fc039815aab0d7963c30ce055128686a941d20 100644 (file)
@@ -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);
index 913e39700e04a0dda6a8a72869825ca5fe17b567..07143e3eeee14371cf7f5f31ff3e7aade2c288fc 100644 (file)
@@ -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);
+    }
 }
index 572629fc3440b936a9acba6d64a0145d981f30bf..2a06375e4855e2127e57d04161c9c5ae9704adc4 100644 (file)
@@ -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);