Allow ApplicationContextHelper to support both Lazy and NoAutoStart, where previously it only one at a time could be used.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@18844 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-02-25 14:17:51 +00:00
parent 0a48463756
commit cfc0643817
4 changed files with 50 additions and 15 deletions

View File

@@ -26,6 +26,8 @@ package org.alfresco.util;
import java.util.Arrays;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -82,10 +84,8 @@ public class ApplicationContextHelper
// The config has changed so close the current context (if any)
closeApplicationContext();
if(useLazyLoading) {
instance = new LazyClassPathXmlApplicationContext(configLocations);
} else if(noAutoStart) {
instance = new NoAutoStartClassPathXmlApplicationContext(configLocations);
if(useLazyLoading || noAutoStart) {
instance = new VariableFeatureClassPathXmlApplicationContext(configLocations);
} else {
instance = new ClassPathXmlApplicationContext(configLocations);
}
@@ -119,9 +119,6 @@ public class ApplicationContextHelper
* to reduce startup times when using a small, cut down context.
*/
public static void setUseLazyLoading(boolean lazyLoading) {
if(lazyLoading && noAutoStart) {
throw new IllegalStateException("You must choose between LazyLoading and NoAutoStart");
}
useLazyLoading = lazyLoading;
}
/**
@@ -142,13 +139,10 @@ public class ApplicationContextHelper
* you can use this to prevent the auto start.
*/
public static void setNoAutoStart(boolean noAutoStart) {
if(useLazyLoading && noAutoStart) {
throw new IllegalStateException("You must choose between LazyLoading and NoAutoStart");
}
ApplicationContextHelper.noAutoStart = noAutoStart;
}
/**
* Will subsystems with the autoStart=true property set
* Will Subsystems with the autoStart=true property set
* on them be allowed to auto start? The default is to
* honour the spring configuration and allow them to,
* but they can be prevented if required.
@@ -166,4 +160,27 @@ public class ApplicationContextHelper
}
ctx.close();
}
/**
* A wrapper around {@link ClassPathXmlApplicationContext} which
* allows us to enable lazy loading or prevent Subsystem
* autostart as requested.
*/
protected static class VariableFeatureClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
protected VariableFeatureClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
super(configLocations);
}
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
if(useLazyLoading) {
LazyClassPathXmlApplicationContext.postInitBeanDefinitionReader(reader);
}
if(noAutoStart) {
NoAutoStartClassPathXmlApplicationContext.postInitBeanDefinitionReader(reader);
}
}
}
}

View File

@@ -67,6 +67,7 @@ public class ContextDependencyLister
{
public static final String[] DEFAULT_CONFIG_LOCATIONS = new String[] {
"classpath:alfresco/application-context.xml"
// "classpath:alfresco/minimal-context.xml"
// "classpath:test/alfresco/fake-context/application-context.xml"
};
private String[] configLocations;

View File

@@ -52,6 +52,13 @@ public class LazyClassPathXmlApplicationContext extends
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
postInitBeanDefinitionReader(reader);
}
/**
* Does the work of enabling Lazy Init on the xml bean reader
*/
protected static void postInitBeanDefinitionReader(XmlBeanDefinitionReader reader) {
reader.setDocumentReaderClass(AlwaysLazyInitBeanDefinitionDocumentReader.class);
}

View File

@@ -35,8 +35,8 @@ import org.w3c.dom.Element;
/**
* A wrapper around {@link ClassPathXmlApplicationContext} which
* stops abstractPropertyBackedBean based beans from being
* AutoStarted by tweaking their property definitions.
* stops Alfresco Subsystem (abstractPropertyBackedBean based)
* beans from being AutoStarted by tweaking their property definitions.
* You shouldn't do this in production, but it can be handy with
* unit tests, as it allows a quicker startup by preventing
* subsystems from starting up
@@ -54,6 +54,15 @@ public class NoAutoStartClassPathXmlApplicationContext extends
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
postInitBeanDefinitionReader(reader);
}
/**
* Does the work of disabling the autostart of the
* Subsystem (abstractPropertyBackedBean) beans
* on the xml bean reader
*/
protected static void postInitBeanDefinitionReader(XmlBeanDefinitionReader reader) {
reader.setDocumentReaderClass(NoAutoStartBeanDefinitionDocumentReader.class);
}
@@ -77,7 +86,8 @@ public class NoAutoStartClassPathXmlApplicationContext extends
String propertyName = ele.getAttribute("name");
if("autoStart".equals(propertyName)) {
if("abstractPropertyBackedBean".equals(bd.getParentName())) {
System.out.println("Preventing the autostart of " + bd.getBeanClassName());
String id = ele.getParentNode().getAttributes().getNamedItem("id").getTextContent();
System.out.println("Preventing the autostart of Subsystem " + id);
return;
}
}