[DSRV-7] Adjust Service Activators to be able to start from Spring configuration

git-svn-id: http://scm.dspace.org/svn/repo/modules/dspace-services/trunk@4939 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Mark Diggory
2010-05-14 00:44:41 +00:00
parent e2983ec06c
commit d87ea87030
8 changed files with 112 additions and 53 deletions

View File

@@ -119,35 +119,17 @@ public class DSpaceServiceManager implements ServiceManagerSystem {
* Registers the activators using this service manager * Registers the activators using this service manager
*/ */
private void registerActivators() { private void registerActivators() {
// get out the list of all activators
List<DSpaceConfig> allConfigs = configurationService.getConfiguration(); for (Activator activator : this.getServicesByType(Activator.class))
List<DSpaceConfig> configs = new ArrayList<DSpaceConfig>(); {
for (DSpaceConfig config : allConfigs) { // succeeded creating the activator
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
try { try {
activator.start(this); activator.start(this);
activators.add(activator); activators.add(activator);
log.info("Started and registered activator: " + activatorClassName); log.info("Started and registered activator: " + activator.getClass().getName());
} catch (Exception e1) { } 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 * finds out all the interfaces that are implemented by this service
* for future lookup, handles the service change listener * 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 * @param service the service object
*/ */
public void registerServiceAPIs(String serviceName, Object service) { 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 * 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) { public void unregisterServiceAPIs(String serviceName) {
checkRunning(); checkRunning();

View File

@@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
/** /**
* This will allow us to put the configuration into beans as they are being created, * This will allow us to put the configuration into beans as they are being created,
* it also handles activator classes from the configuration * it also handles activator classes from the configuration
* *
* @author Aaron Zeckoski (azeckoski @ gmail.com) * @author Aaron Zeckoski (azeckoski @ gmail.com)
*/ */
public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@@ -41,8 +41,8 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor
private ServiceManagerSystem parent; private ServiceManagerSystem parent;
private boolean testMode = false; private boolean testMode = false;
public DSpaceBeanFactoryPostProcessor(ServiceManagerSystem parent, public DSpaceBeanFactoryPostProcessor(ServiceManagerSystem parent,
DSpaceConfigurationService configurationService, boolean testMode) { DSpaceConfigurationService configurationService, boolean testMode) {
if (parent == null || configurationService == null) { if (parent == null || configurationService == null) {
throw new IllegalArgumentException("parent and configuration service cannot be null"); throw new IllegalArgumentException("parent and configuration service cannot be null");
} }
@@ -54,6 +54,7 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
*/ */
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// force config service to be registered first // force config service to be registered first
beanFactory.registerSingleton(ConfigurationService.class.getName(), configurationService); beanFactory.registerSingleton(ConfigurationService.class.getName(), configurationService);
@@ -70,30 +71,30 @@ public class DSpaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor
} }
} }
if (testMode) { // now register all autowire configured beans
log.info("Spring Service Manager running in test mode, no activators will be started"); for (DSpaceConfig config : configs) {
} else { try {
// now register all autowire configured beans Class<?> c = ServiceMixinManager.getClassByName(config.getActivatorClassName());
for (DSpaceConfig config : configs) {
try { String autowire = config.getActivatorAutowire();
Class<?> c = ServiceMixinManager.getClassByName(config.getActivatorClassName()); int autowireSpring = AbstractBeanDefinition.AUTOWIRE_AUTODETECT;
String autowire = config.getActivatorAutowire(); if ("none".equals(autowire)) {
int autowireSpring = AbstractBeanDefinition.AUTOWIRE_AUTODETECT; autowireSpring = AbstractBeanDefinition.AUTOWIRE_NO;
if ("none".equals(autowire)) { } else if ("constructor".equals(autowire)) {
autowireSpring = AbstractBeanDefinition.AUTOWIRE_NO; autowireSpring = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR;
} else if ("constructor".equals(autowire)) { } else if ("setter".equals(autowire)) {
autowireSpring = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR; autowireSpring = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
} 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);
} }
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()); // System.out.println("Registered beans: " + registry.getBeanDefinitionCount());
// String[] bns = registry.getBeanDefinitionNames(); // String[] bns = registry.getBeanDefinitionNames();
// for (String bn : bns) { // for (String bn : bns) {

View File

@@ -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";
}
}

View File

@@ -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";
}
}

View File

@@ -11,11 +11,14 @@
package org.dspace.servicemanager; package org.dspace.servicemanager;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.InitializedService;
import org.dspace.kernel.mixins.ShutdownService; import org.dspace.kernel.mixins.ShutdownService;
import org.dspace.servicemanager.config.DSpaceConfigurationService; 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 @Test
public void testStartup() { public void testStartup() {
@@ -305,6 +308,20 @@ public class DSpaceServiceManagerTest {
assertEquals(5, service.getTriggers()); 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 { public static class TestService implements InitializedService, ShutdownService {

View File

@@ -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 @Test
public void testStartup() { public void testStartup() {

View File

@@ -28,3 +28,7 @@ jdbc.database.type = HSQLDB
# jdbc.username = user1 # jdbc.username = user1
# jdbc.password = user1 # jdbc.password = user1
## Example Fake Activator initialized from propertes to use in testing
activator.class.org.dspace.activators.FakeActivator2 = org.dspace.activators.FakeActivator2

View File

@@ -32,6 +32,13 @@
<bean id="org.dspace.servicemanager.spring.SpringAnnotationBean" <bean id="org.dspace.servicemanager.spring.SpringAnnotationBean"
class="org.dspace.servicemanager.spring.SpringAnnotationBean" /> class="org.dspace.servicemanager.spring.SpringAnnotationBean" />
<!-- NOTE: do not change this unless you know what you are doing -AZ -->
<!--
Example Activator created in Spring Core Intialization config but not
activated until the Activation Stage
-->
<bean id="org.dspace.activators.FakeActivator"
class="org.dspace.activators.FakeActivator" />
</beans> </beans>