package org.wamblee.system.container;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
* Required services by the container.
*/
public Container(String aName, Component[] aComponents,
- ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
+ List<ProvidedInterface> aProvided, List<RequiredInterface> aRequired) {
super(aName, aProvided, aRequired);
_components = new ArrayList<Component>();
addComponent(component);
}
}
+
+ /**
+ * Constructs the container
+ *
+ * @param aName
+ * Name of the container
+ * @param aComponents
+ * Components.
+ * @param aProvided
+ * Provided services of the container
+ * @param aRequired
+ * Required services by the container.
+ */
+ public Container(String aName, Component[] aComponents,
+ ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
+ this(aName, aComponents, Arrays.asList(aProvided), Arrays.asList(aRequired));
+ }
public Container(String aName) {
this(aName, new Component[0], new ProvidedInterface[0],
* @return Scope.
*/
public Scope start() {
- Scope scope = new DefaultScope(getProvidedInterfaces().toArray());
+ Scope scope = new DefaultScope(getProvidedInterfaces());
return super.start(scope);
}
protected Scope doStart(Scope aExternalScope) {
checkSealed();
validate();
- Scope scope = new DefaultScope(getProvidedInterfaces().toArray(), aExternalScope);
+ Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
ComponentGraph graph = doStartOptionalDryRun(scope, false);
exposeProvidedInterfaces(graph, aExternalScope, scope);
seal();
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
* @param aRequired
* Required services.
*/
- protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
- RequiredInterface[] aRequired) {
+ protected AbstractComponent(String aName, List<ProvidedInterface> aProvided,
+ List<RequiredInterface> aRequired) {
_remaining = new ThreadLocal<List<ProvidedInterface>>();
_context = null;
_name = aName;
- _provided = new ArrayList<ProvidedInterface>();
- _provided.addAll(Arrays.asList(aProvided));
- _required = new ArrayList<RequiredInterface>();
- _required.addAll(Arrays.asList(aRequired));
+ _provided = new ArrayList<ProvidedInterface>(aProvided);
+ _required = new ArrayList<RequiredInterface>(aRequired);
}
+ /**
+ * Constructs the subsystem.
+ *
+ * @param aName
+ * Name of the system.
+ * @param aProvided
+ * Provided services.
+ * @param aRequired
+ * Required services.
+ */
+ protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
+ RequiredInterface[] aRequired) {
+ this(aName, Arrays.asList(aProvided), Arrays.asList(aRequired));
+ }
+
protected AbstractComponent(String aName) {
this(aName, new ProvidedInterface[0], new RequiredInterface[0]);
}
- public AbstractComponent addProvidedInterface(ProvidedInterface aProvided) {
+ public AbstractComponent<Type> addProvidedInterface(ProvidedInterface aProvided) {
_provided.add(aProvided);
return this;
}
- public AbstractComponent addRequiredInterface(RequiredInterface aRequired) {
+ public AbstractComponent<Type> addRequiredInterface(RequiredInterface aRequired) {
_required.add(aRequired);
return this;
}
}
@Override
- public final ProvidedInterfaces getProvidedInterfaces() {
- return new ProvidedInterfaces(Collections.unmodifiableList(_provided));
+ public final List<ProvidedInterface> getProvidedInterfaces() {
+ return Collections.unmodifiableList(_provided);
}
@Override
- public final RequiredInterfaces getRequiredInterfaces() {
- return new RequiredInterfaces(Collections.unmodifiableList(_required));
+ public final List<RequiredInterface> getRequiredInterfaces() {
+ return Collections.unmodifiableList(_required);
}
@Override
public final Type start(Scope aScope) {
LOG.info("Initialization starting '" + getQualifiedName() + "'");
List<ProvidedInterface> oldRemaining = _remaining.get();
- _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces().toArray())));
+ _remaining.set(new ArrayList<ProvidedInterface>(getProvidedInterfaces()));
try {
Type runtime = doStart(aScope);
checkNotStartedInterfaces();
/**
* Must be implemented for initializing the subsystem. The implementation
- * must call {@link #addService(Service)} for each service that is started.
+ * must call {@link #addInterface(ProvidedInterface, Object, Scope)} for each service that is started.
*
* @return Returns the runtime of the component.
*/
*/
package org.wamblee.system.core;
+import java.util.List;
+
/**
* A component represents a part of a system that requires a
* number of interfaces and provides a number of interfaces.
* Gets a description of the provided interfaces.
* @return Provided interfaces.
*/
- ProvidedInterfaces getProvidedInterfaces();
+ List<ProvidedInterface> getProvidedInterfaces();
/**
* Gets a description of the required interfaces.
* @return Required interfaces.
*/
- RequiredInterfaces getRequiredInterfaces();
+ List<RequiredInterface> getRequiredInterfaces();
/**
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
private Map<String, Object> _properties;
private Map<String, Object> _runtimes;
private Map<ProvidedInterface, ProvidedInterfaceImplementation> _provided;
- private ProvidedInterface[] _externallyProvided;
+ private List<ProvidedInterface> _externallyProvided;
+
+ public DefaultScope(List<ProvidedInterface>aExternallyProvided) {
+ this(aExternallyProvided.toArray(new ProvidedInterface[0]));
+ }
public DefaultScope(ProvidedInterface[] aExternallyProvided) {
this(aExternallyProvided, new ArrayList<Scope>());
_properties = new HashMap<String, Object>();
_runtimes = new HashMap<String, Object>();
_provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
- _externallyProvided = aExternallyProvided;
+ _externallyProvided = new ArrayList<ProvidedInterface>();
+ _externallyProvided.addAll(Arrays.asList(aExternallyProvided));
}
@Override
- public ProvidedInterface[] getProvidedInterfaces() {
- return _externallyProvided;
+ public List<ProvidedInterface> getProvidedInterfaces() {
+ return Collections.unmodifiableList(_externallyProvided);
}
@Override
*/
package org.wamblee.system.core;
+import java.util.List;
+
/**
* A scope represents a set of running services and the runtime information for the
* started components and is (usually) the result of
* Gets the provided interfaces by this scope.
* @return Provided interfaces.
*/
- ProvidedInterface[] getProvidedInterfaces();
+ List<ProvidedInterface> getProvidedInterfaces();
/**
* Adds a key value pair to the scope.
* provided interfaces.
* @param aComponent Component to add.
*/
- public void addComponent(Component aComponent) {
+ public void addComponent(Component<?> aComponent) {
// Add required interfaces.
Node compNode = new ComponentNode(aComponent);
List<Node> requiredNodes = new ArrayList<Node>();
* 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.container;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
import org.wamblee.system.core.AbstractComponent;
import org.wamblee.system.core.DefaultRequiredInterface;
import org.wamblee.system.core.ProvidedInterface;
import org.wamblee.system.core.Scope;
import org.wamblee.test.EventTracker;
-public class Application extends AbstractComponent {
- public static RequiredInterface[] required(boolean aOptional, String aPrefix) {
- return
- new RequiredInterface[] {
- new DefaultRequiredInterface(aPrefix + "string", String.class, aOptional),
- new DefaultRequiredInterface(aPrefix + "integer", Integer.class, aOptional)
- };
- }
-
- public static RequiredInterface[] required(boolean aOptional) {
+public class Application extends AbstractComponent<Object> {
+ public static RequiredInterface[] required(boolean aOptional,
+ String aPrefix) {
+ return new RequiredInterface[] {
+ new DefaultRequiredInterface(aPrefix + "string", String.class,
+ aOptional),
+ new DefaultRequiredInterface(aPrefix + "integer",
+ Integer.class, aOptional) };
+ }
+
+ public static RequiredInterface[] required(boolean aOptional) {
return required(aOptional, "");
}
-
- private EventTracker<String> _tracker;
- private String _string;
- private Integer _integer;
- private double _random;
-
- public Application() {
- this("application");
- }
-
- public Application(String aName) {
- this(aName, "");
+ private EventTracker<String> _tracker;
+ private String _string;
+ private Integer _integer;
+ private double _random;
+
+ public Application() {
+ this("application");
+ }
+
+ public Application(String aName) {
+ this(aName, "");
}
-
- public Application(String aName, String aPrefix) {
- super(aName, new ProvidedInterface[0], required(false, aPrefix));
+
+ public Application(String aName, String aPrefix) {
+ super(aName, new ProvidedInterface[0], required(false,
+ aPrefix));
_random = Math.random();
}
-
- public Application(boolean aIsOptinal) {
- super("application", new ProvidedInterface[0], required(true, ""));
- }
-
- public Application(EventTracker<String> aTracker) {
- this();
- _tracker = aTracker;
- }
- @Override
- public Object doStart(Scope aScope) {
- track("start." + getName());
- _string = aScope.getInterfaceImplementation(getRequiredInterfaces().get(0).getProvider(), String.class);
- _integer = aScope.getInterfaceImplementation(getRequiredInterfaces().get(1).getProvider(), Integer.class);
- return _random;
- }
-
- public String getString() {
- return _string;
- }
-
- public Integer getInteger() {
- return _integer;
- }
-
- @Override
- public void doStop(Object aRuntime) {
- track("stop." + getName());
- if ( _random != (Double)aRuntime) {
- throw new IllegalArgumentException("Wrong runtime: expected " + _random + " but got " +
- aRuntime);
- }
- }
-
- private void track(String aString) {
- if ( _tracker == null ) {
- return;
- }
- _tracker.eventOccurred(aString);
- }
+ public Application(boolean aIsOptinal) {
+ super("application", new ProvidedInterface[0], required(true, ""));
+ }
+
+ public Application(EventTracker<String> aTracker) {
+ this();
+ _tracker = aTracker;
+ }
+
+ @Override
+ public Object doStart(Scope aScope) {
+ track("start." + getName());
+ _string = aScope.getInterfaceImplementation(getRequiredInterfaces()
+ .get(0).getProvider(), String.class);
+ _integer = aScope.getInterfaceImplementation(getRequiredInterfaces()
+ .get(1).getProvider(), Integer.class);
+ return _random;
+ }
+
+ public String getString() {
+ return _string;
+ }
+
+ public Integer getInteger() {
+ return _integer;
+ }
+
+ @Override
+ public void doStop(Object aRuntime) {
+ track("stop." + getName());
+ if (_random != (Double) aRuntime) {
+ throw new IllegalArgumentException("Wrong runtime: expected "
+ + _random + " but got " + aRuntime);
+ }
+ }
+
+ private void track(String aString) {
+ if (_tracker == null) {
+ return;
+ }
+ _tracker.eventOccurred(aString);
+ }
}
AssertionUtils.assertEquals(new String[] { "start.environment",
"start.application" }, _tracker.getEvents(
Thread.currentThread()).toArray(new String[0]));
- assertEquals(0, scope.getProvidedInterfaces().length);
+ assertEquals(0, scope.getProvidedInterfaces().size());
assertEquals(environment.getString(), application.getString());
assertEquals(environment.getInteger(), application.getInteger());
AssertionUtils.assertEquals(new String[] { "start.environment",
"start.application" }, _tracker.getEvents(
Thread.currentThread()).toArray(new String[0]));
- assertEquals(0, scope.getProvidedInterfaces().length);
+ assertEquals(0, scope.getProvidedInterfaces().size());
assertEquals(environment.getString(), application.getString());
assertEquals(environment.getInteger(), application.getInteger());
public void testApplicationEnvironment() {
try {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> environment = new Environment();
+ Component<?> application = new Application();
Container container = new Container("root", new Component[] {
application, environment }, new ProvidedInterface[0],
new RequiredInterface[0]);
}
public void testComposite() {
- Component environment = new Environment(_tracker);
- Component application = new Application(_tracker);
+ Component<?> environment = new Environment(_tracker);
+ Component<?> application = new Application(_tracker);
assertEquals(0, _tracker.getEventCount());
Container system = new Container("all", new Component[] { environment,
application }, new ProvidedInterface[0],
new RequiredInterface[0]);
Scope runtime = system.start();
- RequiredInterfaces required = system.getRequiredInterfaces();
+ List<RequiredInterface> required = system.getRequiredInterfaces();
assertEquals(0, required.size());
- ProvidedInterfaces provided = system.getProvidedInterfaces();
+ List<ProvidedInterface> provided = system.getProvidedInterfaces();
assertEquals(0, provided.size());
AssertionUtils.assertEquals(new String[] { "start.environment",
public void testCompositeWithWrongProvidedInfo() {
try {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> environment = new Environment();
+ Component<?> application = new Application();
Container system = new Container("all", new Component[] {
environment, application },
new ProvidedInterface[] { new DefaultProvidedInterface(
public void testCompositeRequiredInterfaceNotProvided() {
try {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> environment = new Environment();
+ Component<?> application = new Application();
Container system = new Container("all", new Component[] {
environment, application }, new ProvidedInterface[0],
new RequiredInterface[] { new DefaultRequiredInterface(
}
public void testCompositeWithSuperfluousRequiredInfo() {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> environment = new Environment();
+ Component<?> application = new Application();
Container system = new Container("all", new Component[] { environment,
application }, new ProvidedInterface[0],
new RequiredInterface[] { new DefaultRequiredInterface("float",
system.getRequiredInterfaces().get(0)
.setProvider(new DefaultProvidedInterface("hallo", Float.class));
system.start();
- RequiredInterfaces required = system.getRequiredInterfaces();
+ List<RequiredInterface> required = system.getRequiredInterfaces();
assertEquals(1, required.size());
- ProvidedInterfaces provided = system.getProvidedInterfaces();
+ List<ProvidedInterface> provided = system.getProvidedInterfaces();
assertEquals(0, provided.size());
}
public void testCompositeWithExternalDependencesNotProvided() {
try {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> application = new Application();
+
Container system = new Container("all",
new Component[] { application }, new ProvidedInterface[0],
- application.getRequiredInterfaces().toArray());
+ application.getRequiredInterfaces().toArray(new RequiredInterface[0]));
system.start();
} catch (SystemAssemblyException e) {
return;
public void testDuplicateComponent() {
try {
- Component comp1 = new Application();
- Component comp2 = new Application();
+ Component<?> comp1 = new Application();
+ Component<?> comp2 = new Application();
Container system = new Container("top");
system.addComponent(comp1).addComponent(comp2);
} catch (SystemAssemblyException e) {
public void testInconsistentHierarchy() {
try {
- Component comp = new Application();
+ Component<?> comp = new Application();
Container system = new Container("top").addComponent(comp);
Container system2 = new Container("top2").addComponent(comp);
} catch (SystemAssemblyException e) {
public void testCompositeWithExternalDependencesProvided() {
- Component environment = new Environment();
- Component application = new Application();
+ Component<?> environment = new Environment();
+ Component<?> application = new Application();
Container system = new Container("all",
new Component[] { application }, new ProvidedInterface[0],
- application.getRequiredInterfaces().toArray());
+ application.getRequiredInterfaces().toArray(new RequiredInterface[0]));
environment.start(new DefaultScope(new ProvidedInterface[0]));
system.getRequiredInterfaces().get(0).setProvider(environment
.getProvidedInterfaces().get(0));
.getProvidedInterfaces().get(1));
system.start();
- RequiredInterfaces required = system.getRequiredInterfaces();
+ List<RequiredInterface> required = system.getRequiredInterfaces();
assertEquals(2, required.size());
- ProvidedInterfaces provided = system.getProvidedInterfaces();
+ List<ProvidedInterface> provided = system.getProvidedInterfaces();
assertEquals(0, provided.size());
}
public void testAmbiguousInterfaces() {
try {
- Component environment1 = new Environment();
- Component environment2 = new Environment();
- Component application = new Application();
+ Component<?> environment1 = new Environment();
+ Component<?> environment2 = new Environment();
+ Component<?> application = new Application();
Container container = new Container("root", new Component[] {
environment1, environment2, application },
new ProvidedInterface[0], new RequiredInterface[0]);
public void testIncompleteRequirements() {
try {
- Component application = new Application();
+ Component<?> application = new Application();
Container system = new Container("all",
new Component[] { application }, new ProvidedInterface[0],
new RequiredInterface[0]);
.getProvidedInterfaces().get(0));
container.getRequiredInterfaces().get(1).setProvider(env
.getProvidedInterfaces().get(1));
- Scope external = new DefaultScope(env.getProvidedInterfaces().toArray());
+ Scope external = new DefaultScope(env.getProvidedInterfaces());
env.start(external);
container.start(external);
public void testNotAllInterfacesStarted() {
try {
- Component component = new AbstractComponent("xx",
+ Component<?> component = new AbstractComponent<Object>("xx",
new ProvidedInterface[] { new DefaultProvidedInterface(
"xxx", String.class) }, new RequiredInterface[0]) {
@Override
// Empty.
}
};
- component.start(new DefaultScope(component.getProvidedInterfaces().toArray()));
+ component.start(new DefaultScope(component.getProvidedInterfaces()));
} catch (SystemAssemblyException e) {
//e.printStackTrace();
return;
public void testUnexpectedServicesStarted() {
try {
- Component component = new AbstractComponent("xx",
+ Component<?> component = new AbstractComponent<Object>("xx",
new ProvidedInterface[0], new RequiredInterface[0]) {
@Override
protected Object doStart(Scope aScope) {
// Empty.
}
};
- component.start(new DefaultScope(component.getProvidedInterfaces().toArray()));
+ component.start(new DefaultScope(component.getProvidedInterfaces()));
} catch (SystemAssemblyException e) {
//e.printStackTrace();
return;
import org.wamblee.system.core.RequiredInterface;
import org.wamblee.test.EventTracker;
-public class Environment extends AbstractComponent {
+public class Environment extends AbstractComponent<Object> {
private static final ProvidedInterface[] provided(String aPrefix) {
return new ProvidedInterface[] {
import org.wamblee.system.core.RequiredInterface;
import org.wamblee.test.EventTracker;
-public class IntegerComponent extends AbstractComponent {
+public class IntegerComponent extends AbstractComponent<Object> {
private static final ProvidedInterface[] provided(String aPrefix) {
return new ProvidedInterface[] {
import org.wamblee.system.core.RequiredInterface;
import org.wamblee.test.EventTracker;
-public class StringComponent extends AbstractComponent {
+public class StringComponent extends AbstractComponent<Object> {
private static final ProvidedInterface[] provided(String aPrefix) {
return new ProvidedInterface[] {
public class ConnectExternalProvidedProvidedEdgeFilterTest extends TestCase {
private Container _container;
- private Component _internal;
+ private Component<Object> _internal;
private String _externalInterfaceName;
private String _internalComponentName;
private String _internalInterfaceName;
public class ConnectRequiredExternallyRequiredEdgeFilterTest extends TestCase {
- private Component _comp;
+ private Component<?> _comp;
private Container _container;
private Edge _edge;
private void compare(Boolean[] aExpected, EdgeFilter aRestriction) {
List<Boolean> result = new ArrayList<Boolean>();
+
+
// order will be:
// env1, app1
// env1, app2
*/
package org.wamblee.system.spring;
+import java.util.List;
+
import org.springframework.beans.factory.FactoryBean;
import org.wamblee.system.core.ProvidedInterface;
import org.wamblee.system.core.RequiredInterface;
* @param aId Id of the bean in the service registry.
*/
public RequiredServiceBean(String aId) {
- RequiredInterfaces required = SpringComponent.THIS.get().getRequiredInterfaces();
+ List<RequiredInterface> required = SpringComponent.THIS.get().getRequiredInterfaces();
for ( RequiredInterface intf: required) {
if ( intf.getName().equals(aId)) {
_required = intf;
SpringComponent old = THIS.get();
Scope oldScope = SCOPE.get();
THIS.set(this);
- Scope scope = new DefaultScope(getProvidedInterfaces().toArray(), aExternalScope);
+ Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
SCOPE.set(scope);
try {
GenericApplicationContext parentContext = new GenericApplicationContext();
import java.io.IOException;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
new HashMap<RequiredInterface, String>());
Scope runtime = system.start(_externalScope);
- assertEquals(0, _externalScope.getProvidedInterfaces().length);
+ assertEquals(0, _externalScope.getProvidedInterfaces().size());
system.stop(runtime);
}
new String[] { HELLO_SERVICE_SPRING_XML }, provided,
new HashMap<RequiredInterface, String>());
Scope runtime = system.start(_externalScope);
- ProvidedInterface[] services = runtime.getProvidedInterfaces();
+ List<ProvidedInterface> services = runtime.getProvidedInterfaces();
- assertEquals(1, services.length);
- Object service = runtime.getInterfaceImplementation(services[0],
+ assertEquals(1, services.size());
+ Object service = runtime.getInterfaceImplementation(services.get(0),
Object.class);
assertTrue(service instanceof HelloService);
// unprocessed property
// and another time with the processed property.
assertEquals(1, EVENT_TRACKER.getEventCount());
- ProvidedInterface[] services = scope.getProvidedInterfaces();
+ List<ProvidedInterface> services = scope.getProvidedInterfaces();
assertEquals("Property Value", scope.getInterfaceImplementation(
- services[0], HelloService.class).say());
+ services.get(0), HelloService.class).say());
}
public void testWithPropertiesAsBean() throws IOException {
Scope scope = system.start(_externalScope);
- ProvidedInterface[] services = scope.getProvidedInterfaces();
+ List<ProvidedInterface> services = scope.getProvidedInterfaces();
- Properties props2 = scope.getInterfaceImplementation(services[0],
+ Properties props2 = scope.getInterfaceImplementation(services.get(0),
HelloService2.class).getProperties();
assertEquals(props, props2);
}
scope.publishInterface(helloService, helloObject);
system.getRequiredInterfaces().get(0).setProvider(helloService);
Scope runtime = system.start(scope);
- ProvidedInterface started = runtime.getProvidedInterfaces()[0];
+ ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
Object impl = runtime.getInterfaceImplementation(started,
BlaService.class);
new HashMap<RequiredInterface, String>());
Scope runtime = system.start(_externalScope);
- ProvidedInterface[] services = runtime.getProvidedInterfaces();
+ List<ProvidedInterface> services = runtime.getProvidedInterfaces();
- assertEquals(2, services.length);
- Object service = runtime.getInterfaceImplementation(services[0],
+ assertEquals(2, services.size());
+ Object service = runtime.getInterfaceImplementation(services.get(0),
Object.class);
assertTrue(service instanceof HelloService);