[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
*/
private void registerActivators() {
// get out the list of all activators
List<DSpaceConfig> allConfigs = configurationService.getConfiguration();
List<DSpaceConfig> configs = new ArrayList<DSpaceConfig>();
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();

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,
* 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) {

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;
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 {

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

View File

@@ -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

View File

@@ -32,6 +32,13 @@
<bean id="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>