struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benjamin Hood" <h...@adesso.de>
Subject [PATCH] Multiple Struts Configs references in the web.xml
Date Wed, 21 May 2003 17:58:19 GMT
Using multiple struts config references in the web.xml was annoying,
because it is only a comma delimited list of struts-config urls, defeating
the point of XML.

Realizing that using a group of values under a common parameter name
doesn't validate to the web-app_2_3.dtd, I decided to declare each
struts-config url as a separate paramater, and by convention, give the
parameter name the suffix "struts-config", so that it will be correctly
parsed by my patch.

For example:

<init-param>
<param-name>FOO-struts-config</param-name>
<param-value>/WEB-INF/FOO/struts-config.xml</param-value>
</init-param>

<init-param>
<param-name>BAR-struts-config</param-name>
<param-value>/WEB-INF/BAR/struts-config.xml</param-value>
</init-param>

Perhaps not the most nicest solution, but you would have to change the
web.xml dtd (but who am I telling this to).

Cheers,

Ben

PS I ran this patch against Revision 1.148
__________

--- ActionServlet_cvs.java    2003-05-21 19:41:06.000000000 +0200
+++ ActionServlet.java  2003-05-21 19:31:20.000000000 +0200
@@ -72,6 +72,7 @@
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.MissingResourceException;

@@ -322,6 +323,21 @@
      * resource(s) for the default module.
      */
     protected String config = "/WEB-INF/struts-config.xml";
+
+     /**
+      * Suffix for the name of an init parameter that references a
+      * struts-config resource.
+      * This is a workaround for the problem that a the web.xml
+      * schema does not allow a list of parameters, in this case
+      * a list of struts-config files.
+      */
+     protected String configNameSuffix = "struts-config";
+
+     /**
+      * HashSet containing references to multiple struts-config resources.
+      */
+     protected HashSet strutsConfigResources;
+


     /**
@@ -934,24 +950,45 @@
         Digester digester = initConfigDigester();

         // Process each specified resource path
-        while (paths.length() > 0) {
-            digester.push(config);
-            String path = null;
-            int comma = paths.indexOf(',');
-            if (comma >= 0) {
-                path = paths.substring(0, comma).trim();
-                paths = paths.substring(comma + 1);
-            } else {
-                path = paths.trim();
-                paths = "";
-            }
-
-            if (path.length() < 1) {
-                break;
-            }
-
-            this.parseModuleConfigFile(prefix, paths, config, digester,
path);
-        }
+           //          Check whether the comma delimited method or the
multiple
+           // param method was used
+
+           if (strutsConfigResources != null &&
!strutsConfigResources.isEmpty()) {
+
+                 // Multiple params approach
+           Iterator it = strutsConfigResources.iterator();
+           String path = null;
+                 // The paths attribute is probably not set
+                 if (paths == null || paths.length() == 0) paths
= "Multiple Params";
+           while(it.hasNext()) {
+                       digester.push(config);
+                 path = (String) it.next();
+                       this.parseModuleConfigFile(prefix, paths, config,
digester, path);
+           }
+           }
+           else {
+
+                 // Comma delimited approach
+
+             while (paths.length() > 0) {
+                 digester.push(config);
+                 String path = null;
+                 int comma = paths.indexOf(',');
+                 if (comma >= 0) {
+                     path = paths.substring(0, comma).trim();
+                     paths = paths.substring(comma + 1);
+                 } else {
+                     path = paths.trim();
+                     paths = "";
+                 }
+
+                 if (path.length() < 1) {
+                     break;
+                 }
+
+                 this.parseModuleConfigFile(prefix, paths, config,
digester, path);
+             }
+           }

         // Force creation and registration of DynaActionFormClass
instances
         // for all dynamic form beans we wil be using
@@ -1344,9 +1381,38 @@

         String value = null;
         value = getServletConfig().getInitParameter("config");
-        if (value != null) {
-            config = value;
-        }
+
+           // Get all params that end in struts-config
+           Enumeration paramNames = getServletConfig
().getInitParameterNames();
+           if (paramNames != null) {
+                 String paramName = null, paramValue = null;;
+                 while(paramNames.hasMoreElements()) {
+                       paramName = (String) paramNames.nextElement();
+
+                       // Test the suffix of the parameter name
+                       if(paramName != null &&
paramName.endsWith(configNameSuffix)) {
+                             paramValue = getServletConfig
().getInitParameter(paramName);
+
+                             // Test the parameter value
+                             // TODO Test whether this is a real url or
not
+                             if (paramValue == null || paramValue.length()
== 0) continue;
+
+                             // Instantiate the resource set if needs be
+                             if (strutsConfigResources == null)
strutsConfigResources = new HashSet();
+
+                             strutsConfigResources.add(paramValue);
+                       }
+                 }
+           }
+
+           if (value != null) {
+                 // Test wether this just a duplicate of a parameter
+                 // whose name ends in struts-config
+                 if (strutsConfigResources != null &&
!strutsConfigResources.isEmpty()) {
+                       strutsConfigResources.add(value);
+                 }
+                 config = value;
+           }

         value = getServletConfig().getInitParameter("debug");
         if (value != null) {


Mime
View raw message