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
This commit is contained in:
Derek Hulley
2009-09-01 14:29:46 +00:00
parent 2dac555f00
commit 09f8a1b501

View File

@@ -24,6 +24,8 @@
*/ */
package org.alfresco.util; package org.alfresco.util;
import java.util.Arrays;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -36,23 +38,51 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextHelper public class ApplicationContextHelper
{ {
private static ClassPathXmlApplicationContext instance; private static ClassPathXmlApplicationContext instance;
private static String[] usedConfiguration;
/** location of required configuration files */ /** location of required configuration files */
public static final String[] CONFIG_LOCATIONS = new String[] { "classpath:alfresco/application-context.xml" }; 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. * called repeatedly.
* <p/>
* 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() 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.
* <p/>
* 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; 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; return instance;
} }
@@ -69,6 +99,7 @@ public class ApplicationContextHelper
} }
instance.close(); instance.close();
instance = null; instance = null;
usedConfiguration = null;
} }
public static void main(String ... args) public static void main(String ... args)