From 09f8a1b5012f1a1aaac785c22461af2cc7300d36 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Tue, 1 Sep 2009 14:29:46 +0000 Subject: [PATCH] ApplicationContextHelper allows getting of contexts using different configurations - If the configuration changes, the existing context will be shutdown - Can re-use ApplicationContextHelper for unit tests even if different contexts are needed git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@16014 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../util/ApplicationContextHelper.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/source/java/org/alfresco/util/ApplicationContextHelper.java b/source/java/org/alfresco/util/ApplicationContextHelper.java index 7bc4984f17..9a3854c87e 100644 --- a/source/java/org/alfresco/util/ApplicationContextHelper.java +++ b/source/java/org/alfresco/util/ApplicationContextHelper.java @@ -24,6 +24,8 @@ */ package org.alfresco.util; +import java.util.Arrays; + import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -36,23 +38,51 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextHelper { private static ClassPathXmlApplicationContext instance; + private static String[] usedConfiguration; /** location of required configuration files */ public static final String[] CONFIG_LOCATIONS = new String[] { "classpath:alfresco/application-context.xml" }; /** - * Provides a static, single instance of the application context. This method can be + * Provides a static, single instance of the default Alfresco application context. This method can be * called repeatedly. + *

+ * If the configuration requested differs from one used previously, then the previously-created + * context is shut down. * - * @return Returns a new application context + * @return Returns an application context for the default Alfresco configuration */ public synchronized static ConfigurableApplicationContext getApplicationContext() { - if (instance != null) + return getApplicationContext(CONFIG_LOCATIONS); + } + + /** + * Provides a static, single instance of the application context. This method can be + * called repeatedly. + *

+ * If the configuration requested differs from one used previously, then the previously-created + * context is shut down. + * + * @return Returns an application context for the given configuration + */ + public synchronized static ConfigurableApplicationContext getApplicationContext(String[] configLocations) + { + if (configLocations == null) { + throw new IllegalArgumentException("configLocations argument is mandatory."); + } + if (usedConfiguration != null && Arrays.deepEquals(configLocations, usedConfiguration)) + { + // The configuration was used to create the current context return instance; } - instance = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS); + // The config has changed so close the current context (if any) + closeApplicationContext(); + + instance = new ClassPathXmlApplicationContext(configLocations); + usedConfiguration = configLocations; + return instance; } @@ -69,6 +99,7 @@ public class ApplicationContextHelper } instance.close(); instance = null; + usedConfiguration = null; } public static void main(String ... args)