<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-support</artifactId>
+ <type>test-jar</type>
+ </dependency>
</dependencies>
</project>
--- /dev/null
+package org.wamblee.system;
+
+import java.util.Arrays;
+
+/**
+ * Default implementation of a service descriptor.
+ */
+public abstract class AbstractServiceDescriptor implements ServiceDescriptor {
+
+ private String _name;
+ private Class[] _interfaces;
+
+ /**
+ * Constructs the descriptor.
+ * @param aInterface Type of service.
+ */
+ public AbstractServiceDescriptor(String aName, Class aInterface) {
+ _name = aName;
+ _interfaces = new Class[] { aInterface };
+ }
+
+ public AbstractServiceDescriptor(String aName, Class[] aInterfaces) {
+ _name = aName;
+ _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length);
+ }
+
+ @Override
+ public String getName() {
+ return _name;
+ }
+
+ @Override
+ public Class[] getInterfaceTypes() {
+ return _interfaces;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( !(obj instanceof AbstractServiceDescriptor)) {
+ return false;
+ }
+ AbstractServiceDescriptor descr = (AbstractServiceDescriptor)obj;
+ if ( _interfaces.length != descr._interfaces.length ) {
+ return false;
+ }
+ String[] interfaces1 = new String[_interfaces.length];
+ String[] interfaces2 = new String[_interfaces.length];
+ for (int i = 0; i < _interfaces.length; i++) {
+ interfaces1[i] = _interfaces[i].getName();
+ interfaces2[i] = descr._interfaces[i].getName();
+ }
+ Arrays.sort(interfaces1);
+ Arrays.sort(interfaces2);
+ return Arrays.equals(interfaces1, interfaces2);
+ }
+
+ @Override
+ public int hashCode() {
+ return _interfaces.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ for (Class intf: _interfaces) {
+ buf.append("." + intf.getName());
+ }
+ return buf.toString();
+ }
+}
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
}
@Override
- public final ServiceDescriptor[] getProvidedServices() {
- return _provided.toArray(new ServiceDescriptor[0]);
+ public final ProvidedServiceDescriptor[] getProvidedServices() {
+ return _provided.toArray(new ProvidedServiceDescriptor[0]);
}
@Override
- public final ServiceDescriptor[] getRequiredServices() {
- return _required.toArray(new ServiceDescriptor[0]);
+ public final RequiredServiceDescriptor[] getRequiredServices() {
+ return _required.toArray(new RequiredServiceDescriptor[0]);
}
@Override
* Service.
*/
protected final void addService(String aContext,
- ServiceDescriptor aDescriptor, Object aService) {
+ ProvidedServiceDescriptor aDescriptor, Object aService) {
LOG.info(aContext + ": service '" + aService + "' started.");
Service svc = getRegistry().register(aDescriptor, aService);
_running.put(svc.getDescriptor(), svc);
@Override
protected void doStart(String aContext, Service[] aRequiredServices) {
- List<ServiceDescriptor> descriptors = new ArrayList<ServiceDescriptor>();
+ List<ProvidedServiceDescriptor> descriptors = new ArrayList<ProvidedServiceDescriptor>();
for (Service service : aRequiredServices) {
descriptors.add(service.getDescriptor());
}
SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems,
- descriptors.toArray(new ServiceDescriptor[0]));
+ descriptors.toArray(new ProvidedServiceDescriptor[0]));
assembler.start(getRegistry(), aRequiredServices);
}
--- /dev/null
+package org.wamblee.system;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+
+public class DefaultProvidedServiceDescriptor extends AbstractServiceDescriptor implements
+ ProvidedServiceDescriptor {
+
+ public DefaultProvidedServiceDescriptor(String aName, Class aInterface) {
+ super(aName, aInterface);
+ }
+
+ public DefaultProvidedServiceDescriptor(String aName, Class[] aInterface) {
+ super(aName, aInterface);
+ }
+
+}
--- /dev/null
+package org.wamblee.system;
+
+public class DefaultRequiredServiceDescriptor extends AbstractServiceDescriptor
+ implements RequiredServiceDescriptor {
+
+ public DefaultRequiredServiceDescriptor(String aName, Class aInterface) {
+ super(aName, aInterface);
+ }
+
+ public DefaultRequiredServiceDescriptor(String aName, Class[] aInterfaces) {
+ super(aName, aInterfaces);
+ }
+
+ @Override
+ public boolean implementedBy(ProvidedServiceDescriptor aDescriptor) {
+ Class[] provided = aDescriptor.getInterfaceTypes();
+ for (Class required : getInterfaceTypes()) {
+ if ( !serviceProvided(required, provided)) {
+ return false;
+ }
+ }
+ // all required interfaces are provided.
+ return true;
+ }
+
+ /**
+ * Check if the required interface is implemented by one of the provided interfaces.
+ * @param aRequired required interface
+ * @param aProvided Provided interfaces.
+ * @return
+ */
+ private boolean serviceProvided(Class aRequired, Class[] aProvided) {
+ for (Class provided: aProvided) {
+ try {
+ provided.asSubclass(aRequired);
+ return true;
+ } catch (ClassCastException e) {
+ // No match, try the next one.
+ }
+ }
+ return false;
+ }
+
+}
public class DefaultService implements Service {
private String _id;
- private ServiceDescriptor _descriptor;
+ private ProvidedServiceDescriptor _descriptor;
private Object _service;
/**
* @param aService
* Service.
*/
- public DefaultService(String aId, ServiceDescriptor aDescriptor,
+ public DefaultService(String aId, ProvidedServiceDescriptor aDescriptor,
Object aService) {
_id = aId;
_descriptor = aDescriptor;
}
@Override
- public ServiceDescriptor getDescriptor() {
+ public ProvidedServiceDescriptor getDescriptor() {
return _descriptor;
}
+++ /dev/null
-package org.wamblee.system;
-
-/**
- * Default implementation of a service descriptor.
- */
-public class DefaultServiceDescriptor implements ServiceDescriptor {
-
- private Class _class;
-
- /**
- * Constructs the descriptor.
- * @param aClass Type of service.
- */
- public DefaultServiceDescriptor(Class aClass) {
- _class = aClass;
- }
-
- @Override
- public Class getInterfaceType() {
- return _class;
- }
-
- @Override
- public boolean equals(Object obj) {
- if ( !(obj instanceof DefaultServiceDescriptor)) {
- return false;
- }
- DefaultServiceDescriptor descr = (DefaultServiceDescriptor)obj;
- return _class.equals(descr._class);
- }
-
- @Override
- public int hashCode() {
- return _class.hashCode();
- }
-
- @Override
- public String toString() {
- return _class.getName().toString();
- }
-}
}
@Override
- public synchronized Service register(ServiceDescriptor aDescriptor,
+ public synchronized Service register(ProvidedServiceDescriptor aDescriptor,
Object aService) {
_count++;
String id = "" + _count;
--- /dev/null
+package org.wamblee.system;
+
+public interface RequiredServiceDescriptor extends ServiceDescriptor {
+
+ boolean implementedBy(ProvidedServiceDescriptor aDescriptor);
+}
* Gets the descriptor of the service.
* @return Descriptor.
*/
- ServiceDescriptor getDescriptor();
+ ProvidedServiceDescriptor getDescriptor();
/**
* Returns a reference to the running service.
* NOTE: The current implemention only stores the type of the
* descriptor but his can be extended towards more complex rules
* for matching services.
+ *
+ * Implementations must implement {@link #equals(Object)} and {@link #hashCode()}.
*/
public interface ServiceDescriptor {
+ /**
+ * Symbolic name for the service as used by the subsystem.
+ * @return Service name.
+ */
+ String getName();
+
/**
* Returns the service type.
* @return Service type.
*/
- Class getInterfaceType();
+ Class[] getInterfaceTypes();
}
public interface ServiceRegistry {
- Service register(ServiceDescriptor aDescriptor, Object aService);
+ Service register(ProvidedServiceDescriptor aDescriptor, Object aService);
void remove(Service aService);
* Gets a description of the provided interfaces.
* @return Provided interfaces.
*/
- ServiceDescriptor[] getProvidedServices();
+ ProvidedServiceDescriptor[] getProvidedServices();
/**
* Gets a description of the required interfaces.
* @return Required interfaces.
*/
- ServiceDescriptor[] getRequiredServices();
+ RequiredServiceDescriptor[] getRequiredServices();
/**
package org.wamblee.system;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.LogFactory;
/**
- * Assembler to control multiple subsystems. It makes sure that
- * all dependencies are met and controls the order in which systems
- * are initialized.
+ * Assembler to control multiple subsystems. It makes sure that all dependencies
+ * are met and controls the order in which systems are initialized.
*/
public class SystemAssembler {
private String _context;
private SubSystem[] _systems;
+ public static RequiredServiceDescriptor[] filterRequiredServices(
+ ProvidedServiceDescriptor aProvided,
+ Collection<RequiredServiceDescriptor> aDescriptors) {
+ List<RequiredServiceDescriptor> required = new ArrayList<RequiredServiceDescriptor>();
+ for (RequiredServiceDescriptor descriptor : aDescriptors) {
+ if (descriptor.implementedBy(aProvided)) {
+ required.add(descriptor);
+ }
+ }
+ return required.toArray(new RequiredServiceDescriptor[0]);
+ }
+
+ public static ProvidedServiceDescriptor[] filterProvidedServices(
+ RequiredServiceDescriptor aRequired,
+ Collection<ProvidedServiceDescriptor> aProvided) {
+ List<ProvidedServiceDescriptor> provided = new ArrayList<ProvidedServiceDescriptor>();
+ for (ProvidedServiceDescriptor descriptor : aProvided) {
+ if (aRequired.implementedBy(descriptor)) {
+ provided.add(descriptor);
+ }
+ }
+ return provided.toArray(new ProvidedServiceDescriptor[0]);
+ }
+
/**
- * Constructs the assembler.
- * @param aSystems Systems that must be assembled.
- * @param aAvailableServices Available services from other systems
- * outside of the systems that this assembler manages.
+ * Constructs the assembler.
+ *
+ * @param aSystems
+ * Systems that must be assembled.
+ * @param aAvailableServices
+ * Available services from other systems outside of the systems
+ * that this assembler manages.
*/
- public SystemAssembler(SubSystem[] aSystems, ServiceDescriptor[] aAvailableServices) {
+ public SystemAssembler(SubSystem[] aSystems,
+ ProvidedServiceDescriptor[] aAvailableServices) {
this(ROOT_CONTEXT_NAME, aSystems, aAvailableServices);
}
/**
- * Constructs the assembler.
- * @param aContext Context (unique name) of the assembler.
- * @param aSystems Systems that must be assembled.
- * @param aAvailableServices Available services from other systems
- * outside of the systems that this assembler manages.
+ * Constructs the assembler.
+ *
+ * @param aContext
+ * Context (unique name) of the assembler.
+ * @param aSystems
+ * Systems that must be assembled.
+ * @param aAvailableServices
+ * Available services from other systems outside of the systems
+ * that this assembler manages.
*/
- public SystemAssembler(String aContext, SubSystem[] aSystems,
- ServiceDescriptor[] aAvailableServices) {
+ public SystemAssembler(String aContext, SubSystem[] aSystems,
+ ProvidedServiceDescriptor[] aAvailableServices) {
_context = aContext;
_systems = aSystems;
validate(aAvailableServices);
* Determines if the systems are ordered appropriately so that all
* dependencies are met.
*/
- private void validate(ServiceDescriptor[] aDescriptors) throws SystemAssemblyException {
+ private void validate(ProvidedServiceDescriptor[] aDescriptors)
+ throws SystemAssemblyException {
- List<ServiceDescriptor> allProvided = new ArrayList<ServiceDescriptor>();
- for (ServiceDescriptor descriptor: aDescriptors) {
+ List<ProvidedServiceDescriptor> allProvided = new ArrayList<ProvidedServiceDescriptor>();
+ for (ProvidedServiceDescriptor descriptor : aDescriptors) {
allProvided.add(descriptor);
}
for (SubSystem system : _systems) {
// Check if all required services are already provided by earlier
// systems.
- ServiceDescriptor[] required = system.getRequiredServices();
- for (ServiceDescriptor descriptor : required) {
- if (!(allProvided.contains(descriptor))) {
+ RequiredServiceDescriptor[] required = system.getRequiredServices();
+
+ for (RequiredServiceDescriptor descriptor : required) {
+ ProvidedServiceDescriptor[] filtered = filterProvidedServices(
+ descriptor, allProvided);
+
+ if (filtered.length == 0) {
throw new SystemAssemblyException(
"Service '"
+ descriptor
+ system
+ "' is not provided by systems that are started earlier");
}
+ if (filtered.length > 1) {
+ throw new SystemAssemblyException(
+ "Service '"
+ + descriptor
+ + "' required by system '"
+ + system
+ + "' matches multiple services provided by other systems: " +
+ Arrays.asList(filtered));
+ }
}
// add all provided services
- ServiceDescriptor[] provided = system.getProvidedServices();
- for (ServiceDescriptor descriptor : provided) {
- allProvided.add(descriptor);
- }
+ ProvidedServiceDescriptor[] provided = system.getProvidedServices();
+ allProvided.addAll(Arrays.asList(provided));
}
}
/**
* Starts the subsystems.
- * @param aRegistry Service registry to which created services must be registered.
- * @param aRequiredServices Services that are available from
- * other systems that have been started before.
+ *
+ * @param aRegistry
+ * Service registry to which created services must be registered.
+ * @param aRequiredServices
+ * Services that are available from other systems that have been
+ * started before.
*/
public void start(ServiceRegistry aRegistry, Service[] aRequiredServices) {
LOG.info("Starting '" + _context + "'");
- Map<ServiceDescriptor, Service> allProvided = new HashMap<ServiceDescriptor, Service>();
+ Map<ProvidedServiceDescriptor, Service> allProvided = new HashMap<ProvidedServiceDescriptor, Service>();
for (Service service : aRequiredServices) {
allProvided.put(service.getDescriptor(), service);
}
for (SubSystem system : _systems) {
- ServiceDescriptor[] descriptors = system.getRequiredServices();
+
+ // Compose a list of the required services required for the subsystem.
+
+ RequiredServiceDescriptor[] descriptors = system
+ .getRequiredServices();
List<Service> services = new ArrayList<Service>();
- for (ServiceDescriptor descriptor : descriptors) {
- Service required = allProvided.get(descriptor);
- if (required == null) {
- throw new SystemAssemblyException("Service '" + descriptor
- + "' required by '" + system + "' is null.");
- }
- services.add(required);
+ for (RequiredServiceDescriptor descriptor : descriptors) {
+ ProvidedServiceDescriptor[] provided = filterProvidedServices(
+ descriptor, allProvided.keySet());
+ services.add(allProvided.get(provided[0]));
}
+
+ // Start the service.
Service[] provided = system.start(_context, services
.toArray(new Service[0]));
+
+ // Add started services to the map of started services.
for (Service service : provided) {
allProvided.put(service.getDescriptor(), service);
}
public class Application extends AbstractSubSystem {
private static final ServiceDescriptor[] REQUIRED =
new ServiceDescriptor[] {
- new DefaultServiceDescriptor(DataSource.class),
- new DefaultServiceDescriptor(Integer.class)
+ new DefaultRequiredServiceDescriptor("datasource", DataSource.class),
+ new DefaultRequiredServiceDescriptor("integer", Integer.class)
};
public Application(ServiceRegistry aRegistry) {
public class Environment extends AbstractSubSystem {
- private static final ServiceDescriptor[] PROVIDED =
- new ServiceDescriptor[] {
- new DefaultServiceDescriptor(DataSource.class),
- new DefaultServiceDescriptor(Integer.class)
+ private static final ProvidedServiceDescriptor[] PROVIDED =
+ new ProvidedServiceDescriptor[] {
+ new DefaultProvidedServiceDescriptor("datasource", DataSource.class),
+ new DefaultProvidedServiceDescriptor("integer", Integer.class)
};
public Environment(ServiceRegistry aRegistry) {
package org.wamblee.system;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.wamblee.test.AssertionUtils;
+
import junit.framework.TestCase;
public class SystemAssemblerTest extends TestCase {
-
- private ServiceRegistry _registry;
-
+
+ private ServiceRegistry _registry;
+
@Override
protected void setUp() throws Exception {
super.setUp();
- _registry = new DefaultServiceRegistry();
+ _registry = new DefaultServiceRegistry();
+ }
+
+ private static class MyMultiple implements Serializable, Runnable {
+ @Override
+ public void run() {
+ // Empty
+ }
+ }
+
+ public void testFilterProvided() {
+ RequiredServiceDescriptor req1 = new DefaultRequiredServiceDescriptor(
+ "name", Runnable.class);
+ RequiredServiceDescriptor req2 = new DefaultRequiredServiceDescriptor(
+ "name", Serializable.class);
+ ProvidedServiceDescriptor prov1 = new DefaultProvidedServiceDescriptor(
+ "name", Runnable.class);
+ ProvidedServiceDescriptor prov2 = new DefaultProvidedServiceDescriptor(
+ "name", Serializable.class);
+ ProvidedServiceDescriptor prov3 = new DefaultProvidedServiceDescriptor(
+ "name", MyMultiple.class);
+
+ AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1 },
+ SystemAssembler.filterRequiredServices(prov1, Arrays
+ .asList(new RequiredServiceDescriptor[] { req1 })));
+ AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1 },
+ SystemAssembler
+ .filterRequiredServices(prov1, Arrays
+ .asList(new RequiredServiceDescriptor[] { req1,
+ req2 })));
+ AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1,
+ req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays
+ .asList(new RequiredServiceDescriptor[] { req1, req2 })));
+
+ AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1 },
+ SystemAssembler.filterProvidedServices(req1, Arrays
+ .asList(new ProvidedServiceDescriptor[] { prov1 })));
+ AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1 },
+ SystemAssembler.filterProvidedServices(req1,
+ Arrays.asList(new ProvidedServiceDescriptor[] { prov1,
+ prov2 })));
+ AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1,
+ prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays
+ .asList(new ProvidedServiceDescriptor[] { prov1, prov3 })));
}
public void testEnvironmentApplication() {
SubSystem environment = new Environment(_registry);
SubSystem application = new Application(_registry);
SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
- environment, application }, new ServiceDescriptor[0]);
+ environment, application }, new ProvidedServiceDescriptor[0]);
assembler.start(_registry, new Service[0]);
Service[] envServices = environment.getRunningServices();
assertEquals(2, envServices.length);
Service[] appServices = environment.getRunningServices();
assertEquals(2, appServices.length);
assertEquals(2, _registry.listAllServices().length);
-
+
environment.stop();
assertEquals(0, _registry.listAllServices().length);
-
- application.stop();
+
+ application.stop();
assertEquals(0, _registry.listAllServices().length);
}
SubSystem environment = new Environment(_registry);
SubSystem application = new Application(_registry);
SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
- application, environment }, new ServiceDescriptor[0]);
+ application, environment },
+ new ProvidedServiceDescriptor[0]);
assembler.start(_registry, new Service[0]);
} catch (SystemAssemblyException e) {
// e.printStackTrace();
public void testComposite() {
SubSystem environment = new Environment(_registry);
SubSystem application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry, new SubSystem[] {
- environment, application }, new ServiceDescriptor[0],
- new ServiceDescriptor[0]);
+ CompositeSystem system = new CompositeSystem("all", _registry,
+ new SubSystem[] { environment, application },
+ new ServiceDescriptor[0], new ServiceDescriptor[0]);
system.start("root", new Service[0]);
ServiceDescriptor[] required = system.getRequiredServices();
assertEquals(0, required.length);
try {
SubSystem environment = new Environment(_registry);
SubSystem application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,
+ CompositeSystem system = new CompositeSystem(
+ "all",
+ _registry,
new SubSystem[] { environment, application },
- new ServiceDescriptor[] { new DefaultServiceDescriptor(
- String.class) }, new ServiceDescriptor[0]);
+ new ServiceDescriptor[] { new DefaultProvidedServiceDescriptor(
+ "string", String.class) },
+ new DefaultRequiredServiceDescriptor[0]);
} catch (SystemAssemblyException e) {
return;
}
public void testCompositeWithSuperfluousRequiredInfo() {
SubSystem environment = new Environment(_registry);
SubSystem application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,new SubSystem[] {
- environment, application }, new ServiceDescriptor[0],
- new ServiceDescriptor[] { new DefaultServiceDescriptor(
- String.class) });
+ CompositeSystem system = new CompositeSystem("all", _registry,
+ new SubSystem[] { environment, application },
+ new ServiceDescriptor[0],
+ new ServiceDescriptor[] { new DefaultRequiredServiceDescriptor(
+ "string", String.class) });
system.start("root", new Service[0]);
ServiceDescriptor[] required = system.getRequiredServices();
assertEquals(1, required.length);
}
+ public void testAmbiguousInterfaces() {
+ try {
+ SubSystem environment1 = new Environment(_registry);
+ SubSystem environment2 = new Environment(_registry);
+ SubSystem application = new Application(_registry);
+ SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
+ environment1, environment2, application },
+ new ProvidedServiceDescriptor[0]);
+ assembler.start(_registry, new Service[0]);
+ } catch (SystemAssemblyException e) {
+ return;
+ }
+ fail();
+ }
+
}
import org.springframework.context.support.GenericApplicationContext;
import org.wamblee.system.AbstractSubSystem;
import org.wamblee.system.CompositeSystem;
+import org.wamblee.system.ProvidedServiceDescriptor;
+import org.wamblee.system.RequiredServiceDescriptor;
import org.wamblee.system.Service;
import org.wamblee.system.ServiceDescriptor;
import org.wamblee.system.ServiceRegistry;
+import org.wamblee.system.SystemAssembler;
import org.wamblee.system.SystemAssemblyException;
/**
static ThreadLocal<ServiceRegistry> REGISTRY = new ThreadLocal<ServiceRegistry>();
private String[] _configFiles;
- private Map<String, ServiceDescriptor> _provided;
- private Map<ServiceDescriptor, String> _required;
+ private Map<String, ProvidedServiceDescriptor> _provided;
+ private Map<RequiredServiceDescriptor, String> _required;
/**
* Parent application context containing required services.
*/
* service.
*/
public SpringSystem(String aName, ServiceRegistry aRegistry, String[] aConfigFiles,
- Map<String, ServiceDescriptor> aProvided,
- Map<ServiceDescriptor, String> aRequired) {
+ Map<String, ProvidedServiceDescriptor> aProvided,
+ Map<RequiredServiceDescriptor, String> aRequired) {
super(aName, aRegistry, aProvided.values().toArray(new ServiceDescriptor[0]),
aRequired.keySet().toArray(new ServiceDescriptor[0]));
_configFiles = aConfigFiles;
for (Service svc: aRequiredServices) {
String id = svc.getId();
- ServiceDescriptor descriptor = svc.getDescriptor();
- String beanName = _required.get(descriptor);
- ConstructorArgumentValues cargs = new ConstructorArgumentValues();
- cargs.addGenericArgumentValue(id);
- BeanDefinition definition = new RootBeanDefinition(ProvidedServiceBean.class, cargs,
- new MutablePropertyValues());
- _parentContext.registerBeanDefinition(beanName, definition);
+ ProvidedServiceDescriptor descriptor = svc.getDescriptor();
+ RequiredServiceDescriptor[] requiredServices = SystemAssembler.filterRequiredServices(descriptor,
+ _required.keySet());
+ for (RequiredServiceDescriptor required: requiredServices) {
+ String beanName = _required.get(required);
+ ConstructorArgumentValues cargs = new ConstructorArgumentValues();
+ cargs.addGenericArgumentValue(id);
+ BeanDefinition definition = new RootBeanDefinition(ProvidedServiceBean.class, cargs,
+ new MutablePropertyValues());
+ _parentContext.registerBeanDefinition(beanName, definition);
+ }
}
_parentContext.refresh();
}
import junit.framework.TestCase;
-import org.wamblee.system.DefaultServiceDescriptor;
+import org.wamblee.system.AbstractServiceDescriptor;
+import org.wamblee.system.DefaultProvidedServiceDescriptor;
+import org.wamblee.system.DefaultRequiredServiceDescriptor;
import org.wamblee.system.DefaultServiceRegistry;
+import org.wamblee.system.ProvidedServiceDescriptor;
+import org.wamblee.system.RequiredServiceDescriptor;
import org.wamblee.system.Service;
import org.wamblee.system.ServiceDescriptor;
import org.wamblee.system.ServiceRegistry;
public void testBlackboxSystem() {
SpringSystem system = new SpringSystem("system", _registry,
new String[] { HELLO_SERVICE_SPRING_XML },
- new HashMap<String, ServiceDescriptor>(),
- new HashMap<ServiceDescriptor, String>());
+ new HashMap<String, ProvidedServiceDescriptor>(),
+ new HashMap<RequiredServiceDescriptor, String>());
system.start("Hello", new Service[0]);
Service[] services = system.getRunningServices();
assertEquals(0, services.length);
}
public void testOneProvidedService() {
- Map<String, ServiceDescriptor> provided = new HashMap<String, ServiceDescriptor>();
- provided.put("helloService", new DefaultServiceDescriptor(
- HelloService.class));
+ Map<String, ProvidedServiceDescriptor> provided = new HashMap<String, ProvidedServiceDescriptor>();
+ provided.put("helloService", new DefaultProvidedServiceDescriptor(
+ "hello", HelloService.class));
SpringSystem system = new SpringSystem("system", _registry,
new String[] { HELLO_SERVICE_SPRING_XML }, provided,
- new HashMap<ServiceDescriptor, String>());
+ new HashMap<RequiredServiceDescriptor, String>());
system.start("Hello", new Service[0]);
Service[] services = system.getRunningServices();
assertEquals(1, services.length);
try {
SpringSystem system = new SpringSystem("system", _registry,
new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
- new HashMap<String, ServiceDescriptor>(),
- new HashMap<ServiceDescriptor, String>());
+ new HashMap<String, ProvidedServiceDescriptor>(),
+ new HashMap<RequiredServiceDescriptor, String>());
system.start("Bla", new Service[0]);
} catch (SystemAssemblyException e) {
//e.printStackTrace();
}
public void testWithRequirement() {
- Map<ServiceDescriptor, String> required = new HashMap<ServiceDescriptor, String>();
- required.put(new DefaultServiceDescriptor(HelloService.class),
+ Map<RequiredServiceDescriptor, String> required = new HashMap<RequiredServiceDescriptor, String>();
+ required.put(new DefaultRequiredServiceDescriptor("hello", HelloService.class),
"helloService");
SpringSystem system = new SpringSystem("system", _registry,
new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
- new HashMap<String, ServiceDescriptor>(), required);
+ new HashMap<String, ProvidedServiceDescriptor>(), required);
HelloService helloObject = new HelloService("ladida");
- Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject);
+ Service helloService = _registry.register(new DefaultProvidedServiceDescriptor("hello", HelloService.class), helloObject);
system.start("Bla", new Service[] { helloService } );
system.stop();
}
public void testWithRequirementAndProvidedService() {
- Map<ServiceDescriptor, String> required = new HashMap<ServiceDescriptor, String>();
- required.put(new DefaultServiceDescriptor(HelloService.class),
+ Map<RequiredServiceDescriptor, String> required = new HashMap<RequiredServiceDescriptor, String>();
+ required.put(new DefaultRequiredServiceDescriptor("hello", HelloService.class),
"helloService");
- Map<String,ServiceDescriptor> provided = new HashMap<String, ServiceDescriptor>();
- provided.put("blaService", new DefaultServiceDescriptor(
+ Map<String,ProvidedServiceDescriptor> provided = new HashMap<String, ProvidedServiceDescriptor>();
+ provided.put("blaService", new DefaultProvidedServiceDescriptor("bla",
BlaService.class));
SpringSystem system = new SpringSystem("system", _registry,
provided, required);
HelloService helloObject = new HelloService("ladida");
- Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject);
+ Service helloService = _registry.register(new DefaultProvidedServiceDescriptor("hello", HelloService.class), helloObject);
Service[] services = system.start("Bla", new Service[] { helloService } );
assertEquals(1, services.length);