diff --git a/impl/src/main/java/org/dspace/servicemanager/DSpaceServiceManager.java b/impl/src/main/java/org/dspace/servicemanager/DSpaceServiceManager.java index d6e453f74a..3c9a3b9dfd 100644 --- a/impl/src/main/java/org/dspace/servicemanager/DSpaceServiceManager.java +++ b/impl/src/main/java/org/dspace/servicemanager/DSpaceServiceManager.java @@ -119,35 +119,17 @@ public class DSpaceServiceManager implements ServiceManagerSystem { * Registers the activators using this service manager */ private void registerActivators() { - // get out the list of all activators - List allConfigs = configurationService.getConfiguration(); - List configs = new ArrayList(); - for (DSpaceConfig config : allConfigs) { - if (config.isActivator()) { - configs.add(config); - } - } - // now startup and register all the activators - for (DSpaceConfig config : configs) { - String activatorClassName = config.getActivatorClassName(); - Activator activator = null; - try { - Class c = ServiceMixinManager.getClassByName(activatorClassName); - Object o = ReflectUtils.getInstance().constructClass(c); - activator = (Activator) o; - } catch (Exception e) { - System.err.println("ERROR: Failed to find and create activator with className ("+activatorClassName+"): " + e); - } - if (activator != null) { - // succeeded creating the activator + + for (Activator activator : this.getServicesByType(Activator.class)) + { + // succeeded creating the activator try { activator.start(this); activators.add(activator); - log.info("Started and registered activator: " + activatorClassName); + log.info("Started and registered activator: " + activator.getClass().getName()); } catch (Exception e1) { - log.error("ERROR: Failed to start activator ("+activatorClassName+"): " + e1, e1); + log.error("ERROR: Failed to start activator ("+ activator.getClass().getName() +"): " + e1, e1); } - } } } @@ -175,7 +157,7 @@ public class DSpaceServiceManager implements ServiceManagerSystem { * finds out all the interfaces that are implemented by this service * for future lookup, handles the service change listener * - * @param name the name of the service + * @param serviceName the name of the service * @param service the service object */ public void registerServiceAPIs(String serviceName, Object service) { @@ -198,7 +180,7 @@ public class DSpaceServiceManager implements ServiceManagerSystem { /** * Clears out any existing mixin registration and handles the service change listener - * @param name the name of the service + * @param serviceName the name of the service */ public void unregisterServiceAPIs(String serviceName) { checkRunning(); diff --git a/impl/src/main/java/org/dspace/servicemanager/spring/DSpaceBeanFactoryPostProcessor.java b/impl/src/main/java/org/dspace/servicemanager/spring/DSpaceBeanFactoryPostProcessor.java index 4f79f19bfd..a14d48efeb 100644 --- a/impl/src/main/java/org/dspace/servicemanager/spring/DSpaceBeanFactoryPostProcessor.java +++ b/impl/src/main/java/org/dspace/servicemanager/spring/DSpaceBeanFactoryPostProcessor.java @@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory; /** * This will allow us to put the configuration into beans as they are being created, * it also handles activator classes from the configuration - * + * * @author Aaron Zeckoski (azeckoski @ gmail.com) */ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @@ -41,8 +41,8 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor private ServiceManagerSystem parent; private boolean testMode = false; - public DSpaceBeanFactoryPostProcessor(ServiceManagerSystem parent, - DSpaceConfigurationService configurationService, boolean testMode) { + public DSpaceBeanFactoryPostProcessor(ServiceManagerSystem parent, + DSpaceConfigurationService configurationService, boolean testMode) { if (parent == null || configurationService == null) { throw new IllegalArgumentException("parent and configuration service cannot be null"); } @@ -54,6 +54,7 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor /* (non-Javadoc) * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) */ + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // force config service to be registered first beanFactory.registerSingleton(ConfigurationService.class.getName(), configurationService); @@ -70,30 +71,30 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor } } - if (testMode) { - log.info("Spring Service Manager running in test mode, no activators will be started"); - } else { - // now register all autowire configured beans - for (DSpaceConfig config : configs) { - try { - Class c = ServiceMixinManager.getClassByName(config.getActivatorClassName()); - String autowire = config.getActivatorAutowire(); - int autowireSpring = AbstractBeanDefinition.AUTOWIRE_AUTODETECT; - if ("none".equals(autowire)) { - autowireSpring = AbstractBeanDefinition.AUTOWIRE_NO; - } else if ("constructor".equals(autowire)) { - autowireSpring = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR; - } else if ("setter".equals(autowire)) { - autowireSpring = AbstractBeanDefinition.AUTOWIRE_BY_TYPE; - } - RootBeanDefinition beanDef = new RootBeanDefinition(c, autowireSpring); - beanDef.setScope(AbstractBeanDefinition.SCOPE_SINGLETON); - registry.registerBeanDefinition(config.getActivatorName(), beanDef); - } catch (Exception e) { - log.error("Failed to register activator class from config: " + config + " :" + e, e); + // now register all autowire configured beans + for (DSpaceConfig config : configs) { + try { + Class c = ServiceMixinManager.getClassByName(config.getActivatorClassName()); + + String autowire = config.getActivatorAutowire(); + int autowireSpring = AbstractBeanDefinition.AUTOWIRE_AUTODETECT; + if ("none".equals(autowire)) { + autowireSpring = AbstractBeanDefinition.AUTOWIRE_NO; + } else if ("constructor".equals(autowire)) { + autowireSpring = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR; + } else if ("setter".equals(autowire)) { + autowireSpring = AbstractBeanDefinition.AUTOWIRE_BY_TYPE; } + + RootBeanDefinition beanDef = new RootBeanDefinition(c, autowireSpring); + beanDef.setScope(AbstractBeanDefinition.SCOPE_SINGLETON); + registry.registerBeanDefinition(config.getActivatorName(), beanDef); + + } catch (Exception e) { + log.error("Failed to register activator class from config: " + config + " :" + e, e); } } + // System.out.println("Registered beans: " + registry.getBeanDefinitionCount()); // String[] bns = registry.getBeanDefinitionNames(); // for (String bn : bns) { diff --git a/impl/src/test/java/org/dspace/activators/FakeActivator.java b/impl/src/test/java/org/dspace/activators/FakeActivator.java new file mode 100644 index 0000000000..8ce7ac0543 --- /dev/null +++ b/impl/src/test/java/org/dspace/activators/FakeActivator.java @@ -0,0 +1,24 @@ +package org.dspace.activators; + +import org.dspace.kernel.Activator; +import org.dspace.kernel.ServiceManager; + +/** + * Created by IntelliJ IDEA. + * User: mdiggory + * Date: May 13, 2010 + * Time: 7:59:39 AM + * To change this template use File | Settings | File Templates. + */ +public class FakeActivator implements Activator { + + public String status = "Not Started"; + + public void start(ServiceManager serviceManager) { + status = "Started"; + } + + public void stop(ServiceManager serviceManager) { + status = "Stopped"; + } +} diff --git a/impl/src/test/java/org/dspace/activators/FakeActivator2.java b/impl/src/test/java/org/dspace/activators/FakeActivator2.java new file mode 100644 index 0000000000..8e9286e8a5 --- /dev/null +++ b/impl/src/test/java/org/dspace/activators/FakeActivator2.java @@ -0,0 +1,24 @@ +package org.dspace.activators; + +import org.dspace.kernel.Activator; +import org.dspace.kernel.ServiceManager; + +/** + * Created by IntelliJ IDEA. + * User: mdiggory + * Date: May 13, 2010 + * Time: 7:59:39 AM + * To change this template use File | Settings | File Templates. + */ +public class FakeActivator2 implements Activator { + + public String status = "Not Started"; + + public void start(ServiceManager serviceManager) { + status = "Started"; + } + + public void stop(ServiceManager serviceManager) { + status = "Stopped"; + } +} \ No newline at end of file diff --git a/impl/src/test/java/org/dspace/servicemanager/DSpaceServiceManagerTest.java b/impl/src/test/java/org/dspace/servicemanager/DSpaceServiceManagerTest.java index 8123667932..7b76a472c5 100644 --- a/impl/src/test/java/org/dspace/servicemanager/DSpaceServiceManagerTest.java +++ b/impl/src/test/java/org/dspace/servicemanager/DSpaceServiceManagerTest.java @@ -11,11 +11,14 @@ package org.dspace.servicemanager; import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.dspace.activators.FakeActivator; +import org.dspace.activators.FakeActivator2; import org.dspace.kernel.mixins.InitializedService; import org.dspace.kernel.mixins.ShutdownService; import org.dspace.servicemanager.config.DSpaceConfigurationService; @@ -67,7 +70,7 @@ public class DSpaceServiceManagerTest { } /** - * Test method for {@link org.dspace.servicemanager.DSpaceServiceManager#startup(java.util.List, ConfigurationService)}. + * Test method for {@link org.dspace.servicemanager.DSpaceServiceManager#startup()}. */ @Test public void testStartup() { @@ -305,6 +308,20 @@ public class DSpaceServiceManagerTest { assertEquals(5, service.getTriggers()); } + @Test + public void testActivator(){ + dsm.startup(); + + { + FakeActivator activator = dsm.getServiceByName(FakeActivator.class.getName(),FakeActivator.class); + assertEquals("Started", activator.status); + } + + { + FakeActivator2 activator2 = dsm.getServiceByName(FakeActivator2.class.getName(),FakeActivator2.class); + assertEquals("Started", activator2.status); + } + } public static class TestService implements InitializedService, ShutdownService { diff --git a/impl/src/test/java/org/dspace/servicemanager/spring/TestSpringServiceManager.java b/impl/src/test/java/org/dspace/servicemanager/spring/TestSpringServiceManager.java index 5eb938a03f..c1e61b0ce9 100644 --- a/impl/src/test/java/org/dspace/servicemanager/spring/TestSpringServiceManager.java +++ b/impl/src/test/java/org/dspace/servicemanager/spring/TestSpringServiceManager.java @@ -59,7 +59,7 @@ public class TestSpringServiceManager { } /** - * Test method for {@link org.dspace.servicemanager.spring.SpringServiceManager#startup(java.util.List, ConfigurationService)}. + * Test method for {@link org.dspace.servicemanager.spring.SpringServiceManager#startup()}. */ @Test public void testStartup() { diff --git a/impl/src/test/resources/local.properties b/impl/src/test/resources/local.properties index f5494a8ff1..74302e7d5f 100644 --- a/impl/src/test/resources/local.properties +++ b/impl/src/test/resources/local.properties @@ -28,3 +28,7 @@ jdbc.database.type = HSQLDB # jdbc.username = user1 # jdbc.password = user1 + +## Example Fake Activator initialized from propertes to use in testing +activator.class.org.dspace.activators.FakeActivator2 = org.dspace.activators.FakeActivator2 + diff --git a/impl/src/test/resources/spring/spring-test-services.xml b/impl/src/test/resources/spring/spring-test-services.xml index 5829188250..885088ab85 100644 --- a/impl/src/test/resources/spring/spring-test-services.xml +++ b/impl/src/test/resources/spring/spring-test-services.xml @@ -32,6 +32,13 @@ - + + + + \ No newline at end of file