You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ge...@apache.org on 2004/12/15 15:25:43 UTC

svn commit: r111970 - /struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java /struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java

Author: germuska
Date: Wed Dec 15 06:25:41 2004
New Revision: 111970

URL: http://svn.apache.org/viewcvs?view=rev&rev=111970
Log:
add support for a map of arbitrary properties to ActionConfig, and adjust ConfigRuleSet to understand the new config.
Modified:
   struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java
   struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java

Modified: struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java
Url: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java?view=diff&rev=111970&p1=struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java&r1=111969&p2=struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java&r2=111970
==============================================================================
--- struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java	(original)
+++ struts/core/trunk/src/share/org/apache/struts/config/ActionConfig.java	Wed Dec 15 06:25:41 2004
@@ -21,6 +21,7 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Properties;
 
 
 /**
@@ -56,6 +57,12 @@
      */
     protected HashMap forwards = new HashMap();
 
+    /**
+     * A map of arbitrary properties configured for this action mapping.
+     * @since Struts 1.3
+     */
+    protected Properties properties = new Properties();
+
 
     // ------------------------------------------------------------- Properties
 
@@ -567,6 +574,56 @@
 
     }
 
+    /**
+     * <p>Set an arbitary key/value pair which can be retrieved by the action
+     * executed for this mapping.  This facility should eliminate many use cases
+     * for subclassing <code>ActionConfig</code> or <code>ActionMapping</code> by
+     * providing more than just the single <code>parameter</code> property for passing
+     * arbitrary configuration information into an action.</p>
+     * 
+     * <p>This method must not be called after configuration is complete, or an
+     * <code>IllegalStateException</code> will be thrown.  Rather than calling it in Java code, 
+     * it is used by editing the <code>struts-config</code> file.  Specifically, these values can 
+     * be set by using a <code>&lt;set-property&gt;</code> element nested within the <code>&lt;action&gt;</code>
+     * element.  The element should use the <code>key</code> attribute, not the <code>name</code>
+     * attribute: the <code>name</code> attribute is for setting bean properties on a custom subclass
+     * of <code>ActionConfig</code>.
+     * </p>
+     * 
+     * <p><b>Example</b>
+     * <code><pre>
+     * &lt;action path="/example" type="com.example.MyAction"&gt;
+     *    &lt;set-property key="foo" property="bar" /&gt;
+     * &lt;/action&gt;
+     * </pre></code>
+     * </p>
+     * 
+     * @param key the key by which this value will be retrieved
+     * @param value the value which should be returned when <code>getProperty(key)</code> is
+     * called with the corresponding <code>key</code>.
+     * @since Struts 1.3
+     * @exception IllegalStateException if this module configuration
+     *  has been frozen
+     */
+    public void setProperty(String key, String value) {
+
+        if (configured) {
+            throw new IllegalStateException("Configuration is frozen");
+        }
+        properties.setProperty(key,value);
+
+    }
+
+    /**
+     * Return the property-value for the specified key, if any;
+     * otherwise return <code>null</code>.
+     *
+     * @param key a key specified in the <code>struts-config</code> file.
+     * @since Struts 1.3
+     */
+    public String getProperty(String key) {
+        return properties.getProperty(key);
+    }
 
     /**
      * Return the exception configuration for the specified type, if any;

Modified: struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java
Url: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java?view=diff&rev=111970&p1=struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java&r1=111969&p2=struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java&r2=111970
==============================================================================
--- struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java	(original)
+++ struts/core/trunk/src/share/org/apache/struts/config/ConfigRuleSet.java	Wed Dec 15 06:25:41 2004
@@ -26,6 +26,7 @@
 import org.apache.commons.digester.RuleSetBase;
 import org.apache.struts.util.RequestUtils;
 import org.xml.sax.Attributes;
+import org.apache.commons.digester.SetPropertyRule;
 
 
 /**
@@ -84,9 +85,9 @@
              "addActionConfig",
              "org.apache.struts.config.ActionConfig");
 
-        digester.addSetProperty
+        digester.addRule
             ("struts-config/action-mappings/action/set-property",
-             "property", "value");
+             new ActionConfigSetPropertyRule());
 
         digester.addObjectCreate
             ("struts-config/action-mappings/action/exception",
@@ -294,6 +295,43 @@
 
 }
 
+/**
+ * A variant of the standard Digester <code>SetPropertyRule</code> which
+ * accepts one of two "naming" attributes.  If the element being processed
+ * has an attribute whose name matches <code>nameAttrName</code>, then the 
+ * standard <code>SetPropertyRule</code> behavior is invoked, and the value will
+ * be used to set a bean property on the object on top of the Digester stack.  
+ * However, if there is an attribute whose name matches <code>keyAttrName</code>,
+ * then the value will be used to call <code>setProperty(key,value)</code> on the object
+ * on top of the stack, which will be assumed to be of type <code>ActionConfig</code>.
+ */
+final class ActionConfigSetPropertyRule extends SetPropertyRule {
+
+    public ActionConfigSetPropertyRule() {
+        super("name", "value");
+    }
+
+    public void begin(Attributes attributes) throws Exception {
+
+        if (attributes.getIndex("key") == -1) { 
+            super.begin(attributes);
+            return;
+        }
+
+        if (attributes.getIndex("name") != -1) {
+            throw new IllegalArgumentException("<set-property> inside <action> accepts only one of 'key' or 'name' attributes.");
+        }
+
+        ActionConfig actionConfig = (ActionConfig) digester.peek();
+        actionConfig.setProperty(attributes.getValue("key"),
+                                 attributes.getValue("value"));
+
+
+    }
+
+
+}
+
 
 /**
  * An object creation factory which creates action form bean instances, taking
@@ -473,3 +511,4 @@
     }
 
 }
+

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org