Commits

tm_jee  committed ae6502f

XW-589 (Allow xwork.xml to have configurable parameter)

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1682e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 853c8c7
  • Branches xwork_1-2

Comments (0)

Files changed (9)

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

  * XWork configuration.
  *
  * @author Mike
+ * @author tmjee
+ * @version $Date$ $Id$
  */
 public interface Configuration {
 
+    /**
+     * Rebuild the {@link com.opensymphony.xwork.config.RuntimeConfiguration} of XWork's configuration.
+     */
     public void rebuildRuntimeConfiguration();
 
+    /**
+     * Return the configuration parameters in xwork.xml
+     * <pre>
+     *    <xwork>
+     *      <parameters>
+     *         <parameter name="..." value="..." />
+     *         ....
+     *      </parameters>
+     *      ....
+     *    </xwork>
+     * </pre>
+     * @return Map
+     */
+    Map getParameters();
+
+    /**
+     * Return the configuration parameter value for with parameter name as <code>name</code>.
+     * @param name parameter name
+     * @return String
+     */
+    String getParameter(String name);
+
+    /**
+     * Set the configuration parameter.
+     * @param name parameter name
+     * @param value parameter value
+     */
+    void setParameter(String name, String value);
+
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig} (package configuration) for
+     * package with named as <code>name</code>
+     * @param name package name
+     * @return {@link com.opensymphony.xwork.config.entities.PackageConfig}
+     */
     PackageConfig getPackageConfig(String name);
 
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig}s name as a {@link java.util.Set}.
+     * @return Set
+     */
     Set getPackageConfigNames();
 
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig} as a {@link java.util.Map} with
+     * the key as the package name and its value as the corresponding
+     * {@link com.opensymphony.xwork.config.entities.PackageConfig}
+     * @return Map
+     */
     Map getPackageConfigs();
 
     /**
      * The current runtime configuration. Currently, if changes have been made to the Configuration since the last
-     * time buildRuntimeConfiguration() was called, you'll need to make sure to.
+     * time buildRuntimeConfiguration() was called, you'll need to make sure to get it using this method.
      *
      * @return the current runtime configuration
      */
     RuntimeConfiguration getRuntimeConfiguration();
 
+    /**
+     * Add a {@link com.opensymphony.xwork.config.entities.PackageConfig} with package name specified as
+     * <code>name</code>
+     * 
+     * @param name
+     * @param packageConfig
+     */
     void addPackageConfig(String name, PackageConfig packageConfig);
 
     /**
      */
     void destroy();
 
+    /**
+     * Reload xwork configuration, once this is done, we need to grab the
+     * {@link com.opensymphony.xwork.config.RuntimeConfiguration} using {@link #getRuntimeConfiguration()} 
+     * @throws ConfigurationException
+     */
     void reload() throws ConfigurationException;
 
+    /**
+     * Remove the {@link com.opensymphony.xwork.config.entities.PackageConfig} for package with name as
+     * <code>name</code>.
+     * @param name package name to be removed
+     */
     void removePackageConfig(String name);
 }

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

  * DefaultConfiguration
  *
  * @author Jason Carreira
- *         Created Feb 24, 2003 7:38:06 AM
+ * @author tmjee
+ * @version $Date$ $Id$
  */
 public class DefaultConfiguration implements Configuration {
 
     private Map packageContexts = new LinkedHashMap();
     protected RuntimeConfiguration runtimeConfiguration;
 
+    private Map parameters = new LinkedHashMap();
+
 
     public DefaultConfiguration() {
     }
 
 
+    /**
+     * Return the configuration parameters in xwork.xml
+     * <pre>
+     *    <xwork>
+     *      <parameters>
+     *         <parameter name="..." value="..." />
+     *         ....
+     *      </parameters>
+     *      ....
+     *    </xwork>
+     * </pre>
+     * @return Map
+     */
+    public Map getParameters() {
+        return parameters;    
+    }
+
+    /**
+     * Return the configuration parameter value for with parameter name as <code>name</code>.
+     * @param name parameter name
+     * @return String
+     */
+    public String getParameter(String name) {
+        return (String) parameters.get(name);
+    }
+
+    /**
+     * Set the configuration parameter.
+     * @param name parameter name
+     * @param value parameter value
+     */
+    public void setParameter(String name, String value) {
+        parameters.put(name, value);
+    }
+
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig} (package configuration) for
+     * package with named as <code>name</code>
+     * @param name package name
+     * @return {@link com.opensymphony.xwork.config.entities.PackageConfig}
+     */
     public PackageConfig getPackageConfig(String name) {
         return (PackageConfig) packageContexts.get(name);
     }
 
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig}s name as a {@link java.util.Set}.
+     * @return Set
+     */
     public Set getPackageConfigNames() {
         return packageContexts.keySet();
     }
 
+    /**
+     * Return the {@link com.opensymphony.xwork.config.entities.PackageConfig} as a {@link java.util.Map} with
+     * the key as the package name and its value as the corresponding
+     * {@link com.opensymphony.xwork.config.entities.PackageConfig}
+     * @return Map
+     */
     public Map getPackageConfigs() {
         return packageContexts;
     }
 
+    /**
+     * The current runtime configuration. Currently, if changes have been made to the Configuration since the last
+     * time buildRuntimeConfiguration() was called, you'll need to make sure to get it using this method.
+     *
+     * @return the current runtime configuration
+     */
     public RuntimeConfiguration getRuntimeConfiguration() {
         return runtimeConfiguration;
     }
 
+    /**
+     * Add a {@link com.opensymphony.xwork.config.entities.PackageConfig} with package name specified as
+     * <code>name</code>
+     *
+     * @param name
+     * @param packageConfig
+     */
     public void addPackageConfig(String name, PackageConfig packageContext) {
       PackageConfig check = (PackageConfig) packageContexts.get(name);
         if (check != null) {
     public void destroy() {
     }
 
+    /**
+     * Reload xwork configuration, once this is done, we need to grab the
+     * {@link com.opensymphony.xwork.config.RuntimeConfiguration} using {@link #getRuntimeConfiguration()}
+     * @throws ConfigurationException
+     */
     public void rebuildRuntimeConfiguration() {
         runtimeConfiguration = buildRuntimeConfiguration();
     }
 
     /**
-     * Calls the ConfigurationProviderFactory.getConfig() to tell it to reload the configuration and then calls
-     * buildRuntimeConfiguration().
+     * Reload xwork configurations, by asking all of the {@link com.opensymphony.xwork.config.ConfigurationProvider}s
+     * to reinitialize this {@link com.opensymphony.xwork.config.impl.DefaultConfiguration} and then
+     * {@link #rebuildRuntimeConfiguration()}.
      *
      * @throws ConfigurationException
      */
         rebuildRuntimeConfiguration();
     }
 
+    /**
+     * Remove the {@link com.opensymphony.xwork.config.entities.PackageConfig} for package with name as
+     * <code>name</code>.
+     * @param name package name to be removed
+     */
     public void removePackageConfig(String name) {
         PackageConfig toBeRemoved = (PackageConfig) packageContexts.get(name);
 
         return new RuntimeConfigurationImpl(namespaceActionConfigs, namespaceConfigs);
     }
 
+    /**
+     * Set the default results for a particular   {@link com.opensymphony.xwork.config.entities.PackageConfig}
+     * @param results
+     * @param packageContext
+     */
     private void setDefaultResults(Map results, PackageConfig packageContext) {
         String defaultResult = packageContext.getFullDefaultResultType();
 
         return config;
     }
 
-
+    /**
+     * Represents the current runtime configuration of xwork.
+     */
     private class RuntimeConfigurationImpl implements RuntimeConfiguration {
+
         private Map namespaceActionConfigs;
         private Map namespaceConfigs;
 
+        /**
+         * Create a new instance of {@link com.opensymphony.xwork.config.RuntimeConfiguration}.
+         * @param namespaceActionConfigs
+         * @param namespaceConfigs
+         */
         public RuntimeConfigurationImpl(Map namespaceActionConfigs, Map namespaceConfigs) {
             this.namespaceActionConfigs = namespaceActionConfigs;
             this.namespaceConfigs = namespaceConfigs;
             return namespaceActionConfigs;
         }
 
+        /**
+         * String representation of this current {@link com.opensymphony.xwork.config.RuntimeConfiguration}
+         * listing all the namepsace and its {@link com.opensymphony.xwork.config.entities.ActionConfig}s.
+         * @return String
+         */
         public String toString() {
             StringBuffer buff = new StringBuffer("RuntimeConfiguration - actions are\n");
 

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

         }    
     }
 
+    protected void addParameters(Element parametersElement) throws ConfigurationException {
+
+        NodeList parameterNodeList = parametersElement.getElementsByTagName("parameter");
+        for (int a=0; a<parameterNodeList.getLength(); a++) {
+            Element element = (Element) parameterNodeList.item(a);
+            String parameterName = element.getAttribute("name");
+            String parameterValue = element.getAttribute("value");
+
+            if (parameterName != null && parameterValue != null && (parameterName.trim().length() > 0) &&
+                    (parameterValue.trim().length() > 0)) {
+                if (configuration.getParameter(parameterName) != null) { // Opps.. parameter already exists
+                    if (LOG.isWarnEnabled()) {
+                        LOG.warn("parameter name ["+parameterName+"] with value ["+configuration.getParameter(parameterName)+"] exists overriding it with value ["+parameterValue+"]");
+                    }
+                }
+                configuration.setParameter(parameterName, parameterValue);
+            }
+            else { // bad... either parameter name or value or both is null or empty
+                if (LOG.isWarnEnabled()) {
+                    LOG.warn("invalid parameter name value pair, either or both of them are null or empty, they will be ignored");
+                }
+            }
+        }
+    }
+
+
     /**
      * Create a PackageConfig from an XML element representing it.
      */
                 in.setSystemId(ClassLoaderUtil.getResource(fileName, getClass()).toString());
                 
                 Map dtdMappings = new HashMap();
+                dtdMappings.put("-//OpenSymphony Group//XWork 1.1.2//EN", "xwork-1.1.2.dtd");
                 dtdMappings.put("-//OpenSymphony Group//XWork 1.1.1//EN", "xwork-1.1.1.dtd");
                 dtdMappings.put("-//OpenSymphony Group//XWork 1.1//EN", "xwork-1.1.dtd");
                 dtdMappings.put("-//OpenSymphony Group//XWork 1.0//EN", "xwork-1.0.dtd");
                     } else if (nodeName.equals("include")) {
                         String includeFileName = child.getAttribute("file");
                         loadConfigurationFile(includeFileName, child);
+                    } else if ("parameters".equals(nodeName)) {
+                        addParameters(child);
                     }
                 }
             }

File src/java/com/opensymphony/xwork/mock/MockConfiguration.java

 import com.opensymphony.xwork.config.RuntimeConfiguration;
 import com.opensymphony.xwork.config.entities.PackageConfig;
 
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
 
  * Mock for an {@link Configuration}.
  *
  * @author Mike
+ * @author tmjee
+ * @version $Date$ $Id$
  */
 public class MockConfiguration implements Configuration {
 
-    private Map packages = new HashMap();
+    private Map packages = new LinkedHashMap();
+    private Map parameters = new LinkedHashMap();
 
+    public Map getParameters() {
+        return parameters;
+    }
+
+    public String getParameter(String name) {
+        return (String) parameters.get(name);
+    }
+
+    public void setParameter(String name, String value) {
+        parameters.put(name, value);
+    }
 
     public PackageConfig getPackageConfig(String name) {
         return (PackageConfig) packages.get(name);

File src/java/xwork-1.1.2.dtd

+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- START SNIPPET: xworkDtd -->
+
+<!--
+   XWork configuration DTD.
+   Use the following DOCTYPE
+
+   <!DOCTYPE xwork PUBLIC
+	"-//OpenSymphony Group//XWork 1.1.2//EN"
+	"http://www.opensymphony.com/xwork/xwork-1.1.2.dtd">
+-->
+
+<!ELEMENT xwork (parameters?, (package|include)*)>
+
+<!ELEMENT parameters (parameter*)>
+
+<!ELEMENT parameter (#PCDATA)>
+<!ATTLIST parameter
+    name CDATA #REQUIRED
+    value CDATA #REQUIRED
+>
+
+<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, global-results?, global-exception-mappings?, action*)>
+<!ATTLIST package
+    name CDATA #REQUIRED
+    extends CDATA #IMPLIED
+    namespace CDATA #IMPLIED
+    abstract CDATA #IMPLIED
+    externalReferenceResolver NMTOKEN #IMPLIED
+>
+
+<!ELEMENT result-types (result-type+)>
+
+<!ELEMENT result-type (param*)>
+<!ATTLIST result-type
+    name CDATA #REQUIRED
+    class CDATA #REQUIRED
+    default (true|false) "false"
+>
+
+<!ELEMENT interceptors (interceptor|interceptor-stack)+>
+
+<!ELEMENT interceptor (param*)>
+<!ATTLIST interceptor
+    name CDATA #REQUIRED
+    class CDATA #REQUIRED
+>
+
+<!ELEMENT interceptor-stack (interceptor-ref+)>
+<!ATTLIST interceptor-stack
+    name CDATA #REQUIRED
+>
+
+<!ELEMENT interceptor-ref (param*)>
+<!ATTLIST interceptor-ref
+    name CDATA #REQUIRED
+>
+
+<!ELEMENT default-interceptor-ref (param*)>
+<!ATTLIST default-interceptor-ref
+    name CDATA #REQUIRED
+>
+
+<!ELEMENT default-action-ref (param*)>
+<!ATTLIST default-action-ref
+    name CDATA #REQUIRED
+>
+
+<!ELEMENT external-ref (#PCDATA)>
+<!ATTLIST external-ref
+    name NMTOKEN #REQUIRED
+    required (true|false) "true"
+>
+
+<!ELEMENT global-results (result+)>
+
+<!ELEMENT global-exception-mappings (exception-mapping+)>
+
+<!ELEMENT action (param|result|interceptor-ref|exception-mapping|external-ref)*>
+<!ATTLIST action
+    name CDATA #REQUIRED
+    class CDATA #IMPLIED
+    method CDATA #IMPLIED
+    converter CDATA #IMPLIED
+>
+
+<!ELEMENT param (#PCDATA)>
+<!ATTLIST param
+    name CDATA #REQUIRED
+>
+
+<!ELEMENT result (#PCDATA|param)*>
+<!ATTLIST result
+    name CDATA #IMPLIED
+    type CDATA #IMPLIED
+>
+
+<!ELEMENT exception-mapping (#PCDATA|param)*>
+<!ATTLIST exception-mapping
+    name CDATA #IMPLIED
+    exception CDATA #REQUIRED
+    result CDATA #REQUIRED
+>
+
+<!ELEMENT include (#PCDATA)>
+<!ATTLIST include
+    file CDATA #REQUIRED
+>
+
+<!-- END SNIPPET: xworkDtd -->
+

File src/test/com/opensymphony/xwork/config/providers/ConfigurationTestBase.java

  * ConfigurationTestBase
  *
  * @author Jason Carreira
- *         Created Jun 9, 2003 7:42:12 AM
+ * @version $Date$ $Id$
  */
 public abstract class ConfigurationTestBase extends XWorkTestCase {
 

File src/test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderParametersTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.config.providers;
+
+import com.opensymphony.xwork.config.ConfigurationProvider;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class XmlConfigurationProviderParametersTest extends ConfigurationTestBase {
+
+    public void testBasic() throws Exception {
+
+        ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork/config/providers/xwork-test-parameters.xml");
+
+        provider.init(configuration);
+
+        assertEquals(configuration.getParameter("param1"), "value1");
+        assertEquals(configuration.getParameter("param2"), "value2");
+        assertEquals(configuration.getParameter("noSuchParameter"), null);
+        assertEquals(configuration.getParameter("noSuchParameterAgain"), null);
+    }
+
+    public void testOverride() throws Exception {
+
+        ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork/config/providers/xwork-test-parameters-override.xml");
+
+        provider.init(configuration);
+
+        assertEquals(configuration.getParameter("param1"), "value1");
+        assertEquals(configuration.getParameter("param2"), "value2");
+        assertEquals(configuration.getParameter("param3"), "overridenValue3");
+        assertEquals(configuration.getParameter("param4"), "overridenValue4");
+        assertEquals(configuration.getParameter("param5"), "value5");
+        assertEquals(configuration.getParameter("param6"), null);
+        assertEquals(configuration.getParameter("param7"), null);
+    }
+}
+

File src/test/com/opensymphony/xwork/config/providers/xwork-test-parameters-override.xml

+<!DOCTYPE xwork PUBLIC
+    "-//OpenSymphony Group//XWork 1.1.2//EN"
+    "http://www.opensymphony.com/xwork/xwork-1.1.2.dtd"
+ >
+
+<xwork>
+
+    <parameters>
+        <parameter name="param1" value="value1" />
+        <parameter name="param2" value="value2" />
+        <parameter name="param3" value="value3" />
+        <parameter name="param4" value="value4" />
+        <parameter name="param5" value="value5" />
+    </parameters>
+
+    <include file="com/opensymphony/xwork/config/providers/xwork-test-parameters-override2.xml" />
+
+</xwork>

File src/test/com/opensymphony/xwork/config/providers/xwork-test-parameters-override2.xml

+<!DOCTYPE xwork PUBLIC
+    "-//OpenSymphony Group//XWork 1.1.2//EN"
+    "http://www.opensymphony.com/xwork/xwork-1.1.2.dtd"
+ >
+
+<xwork>
+
+    <parameters>
+        <parameter name="param3" value="overridenValue3" />
+        <parameter name="param4" value="overridenValue4" />
+    </parameters>
+
+</xwork>