Author: pbenedict
Date: Sun Jul  8 17:25:54 2007
New Revision: 554496

URL: http://svn.apache.org/viewvc?view=rev&rev=554496
Log:
STR-3067: Add singleton/prototype instantiation policy for actions

Modified:
    
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
    
struts/struts1/trunk/core/src/main/java/org/apache/struts/config/ActionConfig.java
    
struts/struts1/trunk/core/src/main/resources/org/apache/struts/resources/struts-config_1_4.dtd

Modified: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java?view=diff&rev=554496&r1=554495&r2=554496
==============================================================================
--- 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
 (original)
+++ 
struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
 Sun Jul  8 17:25:54 2007
@@ -65,29 +65,31 @@
 
         Action action = null;
 
-        synchronized (actions) {
-            action = (Action) actions.get(type);
-
-            if (action == null) {
-                try {
-                    action = createAction(context, type);
-                } catch (Exception e) {
-                    log.error(actionServlet.getInternal().getMessage(
-                            "actionCreate", actionConfig.getPath(), 
-                            actionConfig.toString()), e);
-                    throw e;
+        try {
+            if (actionConfig.isSingleton()) {
+                synchronized (actions) {
+                    action = (Action) actions.get(type);
+                    if (action == null) {
+                        action = createAction(context, type);
+                        actions.put(type, action);
+                    }
                 }
-                actions.put(type, action);
+            } else {
+                action = createAction(context, type);
             }
+        } catch (Exception e) {
+            log.error(actionServlet.getInternal().getMessage(
+                    "actionCreate", actionConfig.getPath(), 
+                    actionConfig.toString()), e);
+            throw e;
         }
-
+        
         if (action.getServlet() == null) {
             action.setServlet(actionServlet);
         }
 
         return (action);
     }
-
     
     /**
      * <p>Invoked by <code>getAction</code> when the <code>Action</code> 

Modified: 
struts/struts1/trunk/core/src/main/java/org/apache/struts/config/ActionConfig.java
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/config/ActionConfig.java?view=diff&rev=554496&r1=554495&r2=554496
==============================================================================
--- 
struts/struts1/trunk/core/src/main/java/org/apache/struts/config/ActionConfig.java
 (original)
+++ 
struts/struts1/trunk/core/src/main/java/org/apache/struts/config/ActionConfig.java
 Sun Jul  8 17:25:54 2007
@@ -193,6 +193,12 @@
      * form bean is accessed, if any. </p>
      */
     protected String scope = "session";
+    
+    /**
+     * <p> Should this action be instantiated once per module (singleton)
+     * or once per request (prototype)? </p>
+     */
+    protected boolean singleton = true; 
 
     /**
      * <p>Identifies conditions for automatic form reset.</p>
@@ -715,6 +721,30 @@
 
         this.populate= populate;
     }
+    
+    /**
+     * Determines whether this action is a singleton (one per module) 
+     * or a prototype (one per request). Actions are defaulted to
+     * singletons unless otherwise specified.
+     * 
+     * @return <code>true</code> for singleton; otherwise prototype
+     * @see #setSingleton(boolean)
+     * @since Struts 1.4
+     */
+    public boolean isSingleton() {
+        return this.singleton;
+    }
+    
+    /**
+     * Stores whether this action is a singleton.
+     * 
+     * @param singleton <code>true</code> for singleton; otherwise prototype
+     * @see #isSingleton()
+     * @since Struts 1.4
+     */
+    public void setSingleton(boolean singleton) {
+        this.singleton = singleton;
+    }
 
     /**
      * <p> Return suffix used to match request parameter names to form bean
@@ -1373,6 +1403,9 @@
             sb.append(",populate=");
             sb.append(populate);
         }
+
+        sb.append(",singleton=");
+        sb.append(singleton);
 
         if (suffix != null) {
             sb.append(",suffix=");

Modified: 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/resources/struts-config_1_4.dtd
URL: 
http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/resources/org/apache/struts/resources/struts-config_1_4.dtd?view=diff&rev=554496&r1=554495&r2=554496
==============================================================================
--- 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/resources/struts-config_1_4.dtd
 (original)
+++ 
struts/struts1/trunk/core/src/main/resources/org/apache/struts/resources/struts-config_1_4.dtd
 Sun Jul  8 17:25:54 2007
@@ -491,6 +491,7 @@
 <!ATTLIST action         reset          %PopulateStrategy; #IMPLIED>
 <!ATTLIST action         roles          CDATA           #IMPLIED>
 <!ATTLIST action         scope          %RequestScope;  #IMPLIED>
+<!ATTLIST action         singleton      %Boolean;       #IMPLIED>
 <!ATTLIST action         suffix         CDATA           #IMPLIED>
 <!ATTLIST action         type           %ClassName;     #IMPLIED>
 <!ATTLIST action         unknown        %Boolean;       #IMPLIED>


Reply via email to