Commits

Anonymous committed c7eea1f

WW-444: Making configuration loading / reloading / reading synchronized

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@177e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (1)

src/java/com/opensymphony/xwork/config/ConfigurationManager.java

 
     protected static final Log LOG = LogFactory.getLog(ConfigurationManager.class);
     protected static Configuration configurationInstance;
-    private static List configurationProviders;
+    private static List configurationProviders = new ArrayList();
 
     static {
         destroyConfiguration();
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public static void setConfiguration(Configuration configuration) {
+    public static synchronized void setConfiguration(Configuration configuration) {
         configurationInstance = configuration;
     }
 
     /**
-     * Get the current XWork configuration object.  By default an instance of DefaultConfiguration will be returned
-     * @see com.opensymphony.xwork.config.impl.DefaultConfiguration
-     */
-    public static Configuration getConfiguration() {
+* Get the current XWork configuration object.  By default an instance of DefaultConfiguration will be returned
+* @see com.opensymphony.xwork.config.impl.DefaultConfiguration
+*/
+    public static synchronized Configuration getConfiguration() {
         if (configurationInstance == null) {
             configurationInstance = new DefaultConfiguration();
             configurationInstance.reload();
     }
 
     /**
-     * <p>
-     * get the current list of ConfigurationProviders.
-     * </p>
-     * <p>
-     * if no custom ConfigurationProviders have been added, this method
-     * will return a list containing only the default ConfigurationProvider, XMLConfigurationProvider.  if a custom
-     * ConfigurationProvider has been added, then the XmlConfigurationProvider must be added by hand.
-     * </p>
-     * @todo the lazy instantiation of XmlConfigurationProvider should be refactored to be elsewhere.  the behavior described above seems unintuitive.
-     * @return the list of registered ConfigurationProvider objects
-     * @see com.opensymphony.xwork.config.ConfigurationProvider
-     */
+* <p>
+* get the current list of ConfigurationProviders.
+* </p>
+* <p>
+* if no custom ConfigurationProviders have been added, this method
+* will return a list containing only the default ConfigurationProvider, XMLConfigurationProvider.  if a custom
+* ConfigurationProvider has been added, then the XmlConfigurationProvider must be added by hand.
+* </p>
+* @todo the lazy instantiation of XmlConfigurationProvider should be refactored to be elsewhere.  the behavior described above seems unintuitive.
+* @return the list of registered ConfigurationProvider objects
+* @see com.opensymphony.xwork.config.ConfigurationProvider
+*/
     public static List getConfigurationProviders() {
-        if (configurationProviders.size() == 0) {
-            configurationProviders.add(new XmlConfigurationProvider());
-        }
+        synchronized (configurationProviders) {
+            if (configurationProviders.size() == 0) {
+                configurationProviders.add(new XmlConfigurationProvider());
+            }
 
-        return configurationProviders;
+            return configurationProviders;
+        }
     }
 
     /**
-     * adds a configuration provider to the List of ConfigurationProviders.  a given ConfigurationProvider may be added
-     * more than once
-     * @param provider the ConfigurationProvider to register
-     */
+* adds a configuration provider to the List of ConfigurationProviders.  a given ConfigurationProvider may be added
+* more than once
+* @param provider the ConfigurationProvider to register
+*/
     public static void addConfigurationProvider(ConfigurationProvider provider) {
-        if (!configurationProviders.contains(provider)) {
-            configurationProviders.add(provider);
+        synchronized (configurationProviders) {
+            if (!configurationProviders.contains(provider)) {
+                configurationProviders.add(provider);
+            }
         }
     }
 
     /**
-     * clears the registered ConfigurationProviders.  this method will call destroy() on each of the registered
-     * ConfigurationProviders
-     * @see com.opensymphony.xwork.config.ConfigurationProvider#destroy
-     */
-    public synchronized static void clearConfigurationProviders() {
-        for (Iterator iterator = configurationProviders.iterator();
-                iterator.hasNext();) {
-            ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
-            provider.destroy();
-        }
+* clears the registered ConfigurationProviders.  this method will call destroy() on each of the registered
+* ConfigurationProviders
+* @see com.opensymphony.xwork.config.ConfigurationProvider#destroy
+*/
+    public static void clearConfigurationProviders() {
+        synchronized (configurationProviders) {
+            for (Iterator iterator = configurationProviders.iterator();
+                    iterator.hasNext();) {
+                ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
+                provider.destroy();
+            }
 
-        configurationProviders.clear();
+            configurationProviders.clear();
+        }
     }
 
-    public static void destroyConfiguration() {
-        configurationProviders = Collections.synchronizedList(new ArrayList());
-        configurationInstance = null;
+    public static synchronized void destroyConfiguration() {
+        synchronized (configurationProviders) {
+            configurationProviders = new ArrayList();
+            configurationInstance = null;
+        }
     }
 
     /**
-     * reloads the Configuration files if the configuration files indicate that they need to be reloaded.
-     *
-     * @todo as FileManager.setReloadingConfigs never appears to be set anywhere, will this ever do anything?
-     * @todo it currently appears that the reload strategy is to check on each call to getConfiguration().  this seems extremely burdensome.  a caching mechanism should be implemented
-     */
-    private static void conditionalReload() {
+* reloads the Configuration files if the configuration files indicate that they need to be reloaded.
+*
+* @todo as FileManager.setReloadingConfigs never appears to be set anywhere, will this ever do anything?
+* @todo it currently appears that the reload strategy is to check on each call to getConfiguration().  this seems extremely burdensome.  a caching mechanism should be implemented
+*/
+    private static synchronized void conditionalReload() {
         if (FileManager.isReloadingConfigs()) {
-            LOG.debug("Checking ConfigurationProviders for reload.");
+            boolean reload;
 
-            boolean reload = false;
+            synchronized (configurationProviders) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Checking ConfigurationProviders for reload.");
+                }
 
-            for (Iterator iterator = getConfigurationProviders().iterator();
-                    iterator.hasNext();) {
-                ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
+                reload = false;
+
+                for (Iterator iterator = getConfigurationProviders().iterator();
+                        iterator.hasNext();) {
+                    ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
 
-                if (provider.needsReload()) {
-                    reload = true;
+                    if (provider.needsReload()) {
+                        reload = true;
 
-                    break;
+                        break;
+                    }
                 }
             }