From cfc06438178ac706e8690e247107f45dc67551bb Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 25 Feb 2010 14:17:51 +0000 Subject: [PATCH] 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 --- .../util/ApplicationContextHelper.java | 39 +++++++++++++------ .../util/ContextDependencyLister.java | 3 +- .../LazyClassPathXmlApplicationContext.java | 7 ++++ ...toStartClassPathXmlApplicationContext.java | 16 ++++++-- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/source/java/org/alfresco/util/ApplicationContextHelper.java b/source/java/org/alfresco/util/ApplicationContextHelper.java index 3218d6dcd0..97f24dd628 100644 --- a/source/java/org/alfresco/util/ApplicationContextHelper.java +++ b/source/java/org/alfresco/util/ApplicationContextHelper.java @@ -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); + } + } + } } diff --git a/source/java/org/alfresco/util/ContextDependencyLister.java b/source/java/org/alfresco/util/ContextDependencyLister.java index 003fb90bf4..466c429bd9 100644 --- a/source/java/org/alfresco/util/ContextDependencyLister.java +++ b/source/java/org/alfresco/util/ContextDependencyLister.java @@ -66,7 +66,8 @@ import com.sun.star.io.IOException; public class ContextDependencyLister { public static final String[] DEFAULT_CONFIG_LOCATIONS = new String[] { - "classpath:alfresco/application-context.xml" + "classpath:alfresco/application-context.xml" +// "classpath:alfresco/minimal-context.xml" // "classpath:test/alfresco/fake-context/application-context.xml" }; private String[] configLocations; diff --git a/source/java/org/alfresco/util/LazyClassPathXmlApplicationContext.java b/source/java/org/alfresco/util/LazyClassPathXmlApplicationContext.java index 97128f16a4..bf169142eb 100644 --- a/source/java/org/alfresco/util/LazyClassPathXmlApplicationContext.java +++ b/source/java/org/alfresco/util/LazyClassPathXmlApplicationContext.java @@ -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); } diff --git a/source/java/org/alfresco/util/NoAutoStartClassPathXmlApplicationContext.java b/source/java/org/alfresco/util/NoAutoStartClassPathXmlApplicationContext.java index 942a5a197b..54ac887ca8 100644 --- a/source/java/org/alfresco/util/NoAutoStartClassPathXmlApplicationContext.java +++ b/source/java/org/alfresco/util/NoAutoStartClassPathXmlApplicationContext.java @@ -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; } }