Commits

jcarreira  committed 0858f41

Refactoring configuration a bit for migration and adding Preparable Interface and PrepareInterceptor

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

  • Participants
  • Parent commits f551599

Comments (0)

Files changed (6)

File src/java/com/opensymphony/xwork/Preparable.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork;
+
+
+/**
+ * Preparable Actions will have their prepare() method called if the PrepareInterceptor is applied to the ActionConfig
+ * @author Jason Carreira
+ * @see com.opensymphony.xwork.interceptor.PrepareInterceptor
+ * Date: Nov 5, 2003 2:28:47 AM
+ */
+public interface Preparable {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    /**
+     * This method is called to allow the action to prepare itself.
+     *
+     * @exception   java.lang.Exception  thrown if a system level exception occurs.
+     */
+    void prepare() throws Exception;
+}

File src/java/com/opensymphony/xwork/config/Configuration.java

     void addPackageConfig(String name, PackageConfig packageConfig);
 
     /**
-     * @throws ConfigurationException
-     */
-    void buildRuntimeConfiguration() throws ConfigurationException;
-
-    /**
      * allow the Configuration to clean up any resources that have been used
      */
     void destroy();

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

 package com.opensymphony.xwork.config;
 
 import com.opensymphony.util.FileManager;
+
 import com.opensymphony.xwork.config.impl.DefaultConfiguration;
 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public static void destroyConfiguration() {
-        configurationProviders = Collections.synchronizedList(new ArrayList());
-        configurationInstance = null;
+    public static void setConfiguration(Configuration configuration) {
+        configurationInstance = configuration;
     }
 
     /**
     public static Configuration getConfiguration() {
         if (configurationInstance == null) {
             configurationInstance = new DefaultConfiguration();
+            configurationInstance.reload();
         } else {
             conditionalReload();
         }
     /**
      * adds a configuration provider to the List of ConfigurationProviders.  a given ConfigurationProvider may be added
      * more than once
-     * @todo what does it mean to have a configuration provider added more than once?  should configurationProviders be a set instead?
      * @param provider the ConfigurationProvider to register
      */
     public static void addConfigurationProvider(ConfigurationProvider provider) {
-        configurationProviders.add(provider);
+        if (!configurationProviders.contains(provider)) {
+            configurationProviders.add(provider);
+        }
     }
 
     /**
      */
     public synchronized static void clearConfigurationProviders() {
         for (Iterator iterator = configurationProviders.iterator();
-             iterator.hasNext();) {
+                iterator.hasNext();) {
             ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
             provider.destroy();
         }
         configurationProviders.clear();
     }
 
+    public static void destroyConfiguration() {
+        configurationProviders = Collections.synchronizedList(new ArrayList());
+        configurationInstance = null;
+    }
+
     /**
      * reloads the Configuration files if the configuration files indicate that they need to be reloaded.
      *
             boolean reload = false;
 
             for (Iterator iterator = getConfigurationProviders().iterator();
-                 iterator.hasNext();) {
+                    iterator.hasNext();) {
                 ConfigurationProvider provider = (ConfigurationProvider) iterator.next();
 
                 if (provider.needsReload()) {

File src/java/com/opensymphony/xwork/config/impl/DefaultConfiguration.java

     //~ Constructors ///////////////////////////////////////////////////////////
 
     public DefaultConfiguration() {
-        try {
-            reload();
-        } catch (ConfigurationException e) {
-            String s = "Caught ConfigurationException while initializing ConfigurationProvider.";
-            LOG.fatal(s);
-            throw e;
-        }
     }
 
     //~ Methods ////////////////////////////////////////////////////////////////
     }
 
     /**
-    * This methodName builds the internal runtime configuration used by Xwork for finding and configuring Actions from the
-    * programmatic configuration data structures. All of the old runtime configuration will be discarded and rebuilt.
-    */
-    public synchronized void buildRuntimeConfiguration() throws ConfigurationException {
-        Map namespaceActionConfigs = new HashMap();
-
-        for (Iterator iterator = packageContexts.values().iterator();
-                iterator.hasNext();) {
-            PackageConfig packageContext = (PackageConfig) iterator.next();
-
-            if (!packageContext.isAbstract()) {
-                String namespace = packageContext.getNamespace();
-                Map configs = (Map) namespaceActionConfigs.get(namespace);
-
-                if (configs == null) {
-                    configs = new HashMap();
-                }
-
-                Map actionConfigs = packageContext.getAllActionConfigs();
-
-                for (Iterator actionIterator = actionConfigs.keySet().iterator();
-                        actionIterator.hasNext();) {
-                    String actionName = (String) actionIterator.next();
-                    ActionConfig baseConfig = (ActionConfig) actionConfigs.get(actionName);
-                    configs.put(actionName, buildFullActionConfig(packageContext, baseConfig));
-                }
-
-                namespaceActionConfigs.put(namespace, configs);
-            }
-        }
-
-        runtimeConfiguration = new RuntimeConfigurationImpl(namespaceActionConfigs);
-    }
-
-    /**
     * Allows the configuration to clean up any resources used
     */
     public void destroy() {
             provider.init(this);
         }
 
-        buildRuntimeConfiguration();
+        runtimeConfiguration = buildRuntimeConfiguration();
     }
 
     public void removePackageConfig(String name) {
         }
     }
 
+    /**
+    * This methodName builds the internal runtime configuration used by Xwork for finding and configuring Actions from the
+    * programmatic configuration data structures. All of the old runtime configuration will be discarded and rebuilt.
+    */
+    protected synchronized RuntimeConfiguration buildRuntimeConfiguration() throws ConfigurationException {
+        Map namespaceActionConfigs = new HashMap();
+
+        for (Iterator iterator = packageContexts.values().iterator();
+                iterator.hasNext();) {
+            PackageConfig packageContext = (PackageConfig) iterator.next();
+
+            if (!packageContext.isAbstract()) {
+                String namespace = packageContext.getNamespace();
+                Map configs = (Map) namespaceActionConfigs.get(namespace);
+
+                if (configs == null) {
+                    configs = new HashMap();
+                }
+
+                Map actionConfigs = packageContext.getAllActionConfigs();
+
+                for (Iterator actionIterator = actionConfigs.keySet().iterator();
+                        actionIterator.hasNext();) {
+                    String actionName = (String) actionIterator.next();
+                    ActionConfig baseConfig = (ActionConfig) actionConfigs.get(actionName);
+                    configs.put(actionName, buildFullActionConfig(packageContext, baseConfig));
+                }
+
+                namespaceActionConfigs.put(namespace, configs);
+            }
+        }
+
+        return new RuntimeConfigurationImpl(namespaceActionConfigs);
+    }
+
     private void setDefaultResults(Map results, PackageConfig packageContext) {
         String defaultResult = packageContext.getFullDefaultResultType();
 

File src/java/com/opensymphony/xwork/config/providers/XmlConfigurationProvider.java

     public void destroy() {
     }
 
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+
+        if (!(o instanceof XmlConfigurationProvider)) {
+            return false;
+        }
+
+        final XmlConfigurationProvider xmlConfigurationProvider = (XmlConfigurationProvider) o;
+
+        if ((configFileName != null) ? (!configFileName.equals(xmlConfigurationProvider.configFileName)) : (xmlConfigurationProvider.configFileName != null)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public int hashCode() {
+        return ((configFileName != null) ? configFileName.hashCode() : 0);
+    }
+
     public void init(Configuration configuration) {
         this.configuration = configuration;
         includedFileNames.clear();

File src/java/com/opensymphony/xwork/interceptor/PrepareInterceptor.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.interceptor;
+
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.Preparable;
+
+
+/**
+ * PrepareInterceptor calls prepare() on Actions which implement com.opensymphony.xwork.Preparable
+ * @author Jason Carreira
+ * @see com.opensymphony.xwork.Preparable
+ * Date: Nov 5, 2003 2:33:11 AM
+ */
+public class PrepareInterceptor extends AroundInterceptor {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    protected void after(ActionInvocation dispatcher, String result) throws Exception {
+    }
+
+    protected void before(ActionInvocation invocation) throws Exception {
+        Action action = invocation.getAction();
+
+        if (action instanceof Preparable) {
+            ((Preparable) action).prepare();
+        }
+    }
+}