Also added addComponent methods for objects to defaultcontainer.
private Class _class;
private ConstructorConfiguration _constructorConfig;
- private SetterConfiguration _setterConfig;
+ private ObjectConfiguration _objectConfig;
/**
* Constructs the configuration. By default no constructor is selected and
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;
}
/**
* @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() {
public List<RequiredInterface> getRequiredInterfaces() {
List<RequiredInterface> result = new ArrayList<RequiredInterface>();
result.addAll(_constructorConfig.getRequiredInterfaces());
- result.addAll(_setterConfig.getRequiredInterfaces());
+ result.addAll(_objectConfig.getRequiredInterfaces());
return result;
}
}
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);
*/
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);
--- /dev/null
+/*
+ * 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);
+ }
+}
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);
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);
+ }
}
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);