You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@locus.apache.org on 2000/12/27 01:16:04 UTC
cvs commit: jakarta-struts/web/example/WEB-INF struts-config.xml
craigmcc 00/12/26 16:16:03
Modified: src/conf struts-config_1_0.dtd
src/share/org/apache/struts/action Action.java
ActionResources.properties ActionServlet.java
web/example/WEB-INF struts-config.xml
Log:
Make it possible to configure more than one data source in a single
instance of the Struts controller servlet. You specify the servlet
context attribute name under which to store each data source using the
"key" attribute of the <data-source> element. By default, the value
returned by Action.DATA_SOURCE_KEY is used.
Anyone using the current data source support will need to modify their
struts-config.xml file, and enclose the <data-source> element in a new
<data-sources> element. Also, you will need to change any call to
servlet.getDataSource()
to the following:
servlet.findDataSource(null);
NOTE: The current GenericDataSource implementation still does not
implement the getConnection() method that takes a username and password
as paramters.
Submitted by: Johan Compagner <jc...@j-com.nl>
Revision Changes Path
1.6 +16 -2 jakarta-struts/src/conf/struts-config_1_0.dtd
Index: struts-config_1_0.dtd
===================================================================
RCS file: /home/cvs/jakarta-struts/src/conf/struts-config_1_0.dtd,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- struts-config_1_0.dtd 2000/11/30 20:12:44 1.5
+++ struts-config_1_0.dtd 2000/12/27 00:16:02 1.6
@@ -11,7 +11,7 @@
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
- $Id: struts-config_1_0.dtd,v 1.5 2000/11/30 20:12:44 craigmcc Exp $
+ $Id: struts-config_1_0.dtd,v 1.6 2000/12/27 00:16:02 craigmcc Exp $
-->
@@ -79,10 +79,19 @@
hierarchy, and contains nested elements for all of the other
configuration settings.
-->
-<!ELEMENT struts-config (data-source?, form-beans?, global-forwards?, action-mappings?)>
+<!ELEMENT struts-config (data-sources?, form-beans?, global-forwards?, action-mappings?)>
<!ATTLIST struts-config id ID #IMPLIED>
+<!-- The "data-sources" element describes a set of JDBC 2.0 Standard Extension
+ data source objects which will be configured according to the nested
+ "data-source" elements found inside.
+-->
+
+<!ELEMENT data-sources (data-source*)>
+<!ATTLIST data-sources id ID #IMPLIED>
+
+
<!-- The "data-source" element describes a JDBC 2.0 Standard Extension data
source object (that implements javax.sql.DataSource) which will be
configured according to the properties and nested elements found here,
@@ -104,6 +113,10 @@
driverClass The Java class name of the JDBC driver to be used.
[REQUIRED]
+ key Servlet context attribute key under which this data
+ source will be stored. Default is the value specified
+ by string constant Action.DATA_SOURCE_KEY.
+
loginTimeout The maximum number of seconds to wait for a connection
to be created or returned.
@@ -127,6 +140,7 @@
<!ATTLIST data-source autoCommit %Boolean; "true">
<!ATTLIST data-source description CDATA #IMPLIED>
<!ATTLIST data-source driverClass %ClassName; #REQUIRED>
+<!ATTLIST data-source key %BeanName; #IMPLIED>
<!ATTLIST data-source loginTimeout %Integer; #IMPLIED>
<!ATTLIST data-source maxCount %Integer; "2">
<!ATTLIST data-source minCount %Integer; "1">
1.11 +7 -6 jakarta-struts/src/share/org/apache/struts/action/Action.java
Index: Action.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Action.java 2000/12/06 19:24:03 1.10
+++ Action.java 2000/12/27 00:16:02 1.11
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v 1.10 2000/12/06 19:24:03 craigmcc Exp $
- * $Revision: 1.10 $
- * $Date: 2000/12/06 19:24:03 $
+ * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v 1.11 2000/12/27 00:16:02 craigmcc Exp $
+ * $Revision: 1.11 $
+ * $Date: 2000/12/27 00:16:02 $
*
* ====================================================================
*
@@ -105,7 +105,7 @@
* by this Action.
*
* @author Craig R. McClanahan
- * @version $Revision: 1.10 $ $Date: 2000/12/06 19:24:03 $
+ * @version $Revision: 1.11 $ $Date: 2000/12/27 00:16:02 $
*/
public class Action {
@@ -115,8 +115,9 @@
/**
- * The context attribute key under which our configured data source
- * (which must implement <code>javax.sql.DataSource</code>) is stored,
+ * The context attribute key under which our <strong>default</strong>
+ * configured data source (which must implement
+ * <code>javax.sql.DataSource</code>) is stored,
* if one is configured for this application.
*/
public static final String DATA_SOURCE_KEY =
1.6 +4 -4 jakarta-struts/src/share/org/apache/struts/action/ActionResources.properties
Index: ActionResources.properties
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionResources.properties,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ActionResources.properties 2000/11/26 05:11:30 1.5
+++ ActionResources.properties 2000/12/27 00:16:02 1.6
@@ -6,11 +6,11 @@
configMapping=Configuring mapping for path {0}
configMissing=Missing configuration resource for path {0}
configParse=Parsing error processing resource path {0}
-dataSource.destroy=Finalizing application data source
-dataSource.init=Initializing application data source
-destroyDataSource=Exception destroying application data source
+dataSource.destroy=Finalizing application data source {0}
+dataSource.init=Initializing application data source {0}
+destroyDataSource=Exception destroying application data source {0}
finalizing=Finalizing this controller servlet
-initDataSource=Exception initializing application data source
+initDataSource=Exception initializing application data source {0}
noInput=No input attribute for mapping path {0}
processInvalid=Invalid path {0} was requested
processPath=No process path included in this request
1.43 +137 -63 jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java
Index: ActionServlet.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- ActionServlet.java 2000/12/15 03:08:09 1.42
+++ ActionServlet.java 2000/12/27 00:16:03 1.43
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v 1.42 2000/12/15 03:08:09 craigmcc Exp $
- * $Revision: 1.42 $
- * $Date: 2000/12/15 03:08:09 $
+ * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v 1.43 2000/12/27 00:16:03 craigmcc Exp $
+ * $Revision: 1.43 $
+ * $Date: 2000/12/27 00:16:03 $
*
* ====================================================================
*
@@ -69,6 +69,8 @@
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Vector;
@@ -81,12 +83,14 @@
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import org.apache.struts.digester.Digester;
+import org.apache.struts.digester.Rule;
import org.apache.struts.taglib.form.Constants;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.GenericDataSource;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.ServletContextWriter;
+import org.xml.sax.AttributeList;
import org.xml.sax.SAXException;
@@ -208,7 +212,7 @@
* </ul>
*
* @author Craig R. McClanahan
- * @version $Revision: 1.42 $ $Date: 2000/12/15 03:08:09 $
+ * @version $Revision: 1.43 $ $Date: 2000/12/27 00:16:03 $
*/
public class ActionServlet
@@ -245,10 +249,11 @@
/**
- * The JDBC data source that has been configured for this application,
- * if any.
+ * The JDBC data sources that has been configured for this application,
+ * if any, keyed by the servlet context attribute under which they are
+ * stored.
*/
- protected DataSource dataSource = null;
+ protected HashMap dataSources = new HashMap();
/**
@@ -394,7 +399,7 @@
destroyActions();
destroyApplication();
- destroyDataSource();
+ destroyDataSources();
destroyInternal();
}
@@ -419,7 +424,7 @@
(internal.getMessage("configIO", config));
}
initUpload();
- initDataSource();
+ initDataSources();
initOther();
}
@@ -465,6 +470,24 @@
/**
+ * Add a data source object to be used by this application.
+ *
+ * @param key The servlet context attribute key under which to store
+ * this data source, or <code>null</code> for the default
+ * @param dataSource The data source to be used
+ */
+ public void addDataSource(String key, DataSource dataSource) {
+
+ if (key == null)
+ key = Action.DATA_SOURCE_KEY;
+ synchronized (dataSources) {
+ dataSources.put(key, dataSource);
+ }
+
+ }
+
+
+ /**
* Register a form bean definition to the set configured for this servlet.
*
* @param formBean The form bean definition to be added
@@ -501,6 +524,24 @@
/**
+ * Return a JDBC data source associated with this application, if any.
+ *
+ * @param key The servlet context attribute key under which this data
+ * source is stored, or <code>null</code> for the default.
+ */
+ public DataSource findDataSource(String key) {
+
+ synchronized (dataSources) {
+ if (key == null)
+ return ((DataSource) dataSources.get(Action.DATA_SOURCE_KEY));
+ else
+ return ((DataSource) dataSources.get(key));
+ }
+
+ }
+
+
+ /**
* Return the form bean definition associated with the specified
* logical name, if any; otherwise return <code>null</code>.
*
@@ -552,7 +593,6 @@
}
-
/**
* Return the debugging detail level for this servlet.
*/
@@ -564,16 +604,6 @@
/**
- * Return the JDBC data source associated with this application, if any.
- */
- public DataSource getDataSource() {
-
- return (this.dataSource);
-
- }
-
-
- /**
* Return the Java class name of the class used to instantiate
* <code>ActionFormBean</code> objects.
*/
@@ -740,18 +770,6 @@
/**
- * Set the data source object to be used by this application.
- *
- * @param dataSource The data source to be used
- */
- public void setDataSource(DataSource dataSource) {
-
- this.dataSource = dataSource;
-
- }
-
-
- /**
* Set the Java class name of the class used to instantiate
* <code>ActionFormBean</code> objects.
*
@@ -860,20 +878,24 @@
* Gracefully terminate use of the data source associated with this
* application (if any).
*/
- protected void destroyDataSource() {
+ protected void destroyDataSources() {
- if (dataSource == null)
- return;
- if (dataSource instanceof GenericDataSource) {
- if (debug >= 1)
- log(internal.getMessage("dataSource.destroy"));
- try {
- ((GenericDataSource) dataSource).close();
- } catch (SQLException e) {
- log(internal.getMessage("destroyDataSource"), e);
+ synchronized (dataSources) {
+ Iterator keys = dataSources.keySet().iterator();
+ while (keys.hasNext()) {
+ String key = (String) keys.next();
+ getServletContext().removeAttribute(key);
+ DataSource dataSource = findDataSource(key);
+ if (dataSource instanceof GenericDataSource) {
+ if (debug >= 1)
+ log(internal.getMessage("dataSource.destroy", key));
+ try {
+ ((GenericDataSource) dataSource).close();
+ } catch (SQLException e) {
+ log(internal.getMessage("destroyDataSource", key), e);
+ }
+ }
}
- dataSource = null;
- getServletContext().removeAttribute(Action.DATA_SOURCE_KEY);
}
}
@@ -933,23 +955,27 @@
/**
- * Initialize use of the data source associated with this
+ * Initialize use of the data sources associated with this
* application (if any).
*/
- protected void initDataSource() {
+ protected void initDataSources() {
- if (dataSource == null)
- return;
- if (dataSource instanceof GenericDataSource) {
- if (debug >= 1)
- log(internal.getMessage("dataSource.init"));
- try {
- ((GenericDataSource) dataSource).open();
- } catch (SQLException e) {
- log(internal.getMessage("initDataSource"), e);
+ synchronized (dataSources) {
+ Iterator keys = dataSources.keySet().iterator();
+ while (keys.hasNext()) {
+ String key = (String) keys.next();
+ DataSource dataSource = findDataSource(key);
+ if (dataSource instanceof GenericDataSource) {
+ if (debug >= 1)
+ log(internal.getMessage("dataSource.init", key));
+ try {
+ ((GenericDataSource) dataSource).open();
+ } catch (SQLException e) {
+ log(internal.getMessage("initDataSource", key), e);
+ }
+ }
+ getServletContext().setAttribute(key, dataSource);
}
- getServletContext().setAttribute(Action.DATA_SOURCE_KEY,
- dataSource);
}
}
@@ -994,14 +1020,15 @@
// FIXME "struts-config/action-mappings" type attribute
- digester.addObjectCreate("struts-config/data-source",
+ digester.addObjectCreate("struts-config/data-sources/data-source",
"org.apache.struts.util.GenericDataSource",
"type");
- digester.addSetProperties("struts-config/data-source");
- digester.addSetNext("struts-config/data-source",
- "setDataSource", "javax.sql.DataSource");
- digester.addSetProperty("struts-config/data-source/set-property",
- "property", "value");
+ digester.addSetProperties("struts-config/data-sources/data-source");
+ digester.addRule("struts-config/data-sources/data-source",
+ new AddDataSourceRule(digester));
+ digester.addSetProperty
+ ("struts-config/data-sources/data-source/set-property",
+ "property", "value");
digester.addObjectCreate("struts-config/action-mappings/action",
mappingClass, "className");
@@ -1805,5 +1832,52 @@
}
+
+}
+
+
+// ------------------------------------------------------------ Private Classes
+
+
+/**
+ * Private digester <code>Rule</code> that adds a data source to the underlying
+ * <code>ActionServlet</code> instance. The servlet context attributes key
+ * is specified by the "key" attribute on the data source element, and defaults
+ * to the value of <code>Action.DATA_SOURCE_KEY</code> if not specified.
+ */
+
+final class AddDataSourceRule extends Rule {
+
+
+ public AddDataSourceRule(Digester digester) {
+
+ super(digester);
+
+ }
+
+
+ public void begin(AttributeList attributes) throws Exception {
+
+ // Acquire the key under which this data source will be stored
+ String key = null;
+ for (int i = 0; i < attributes.getLength(); i++) {
+ if ("key".equals(attributes.getName(i))) {
+ key = attributes.getValue(i);
+ break;
+ }
+ }
+ if (key == null)
+ key = Action.DATA_SOURCE_KEY;
+
+ // Pass the data source at the top of the stack to the action servlet
+ // at the next-to-top position
+ DataSource child = (DataSource) digester.peek(0);
+ ActionServlet parent = (ActionServlet) digester.peek(1);
+ if (digester.getDebug() >= 1)
+ digester.log("Call " + parent.getClass().getName() +
+ ".addDataSource(" + key + ", " + child + ")");
+ parent.addDataSource(key, child);
+
+ }
}
1.6 +12 -10 jakarta-struts/web/example/WEB-INF/struts-config.xml
Index: struts-config.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/web/example/WEB-INF/struts-config.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- struts-config.xml 2000/11/30 20:13:01 1.5
+++ struts-config.xml 2000/12/27 00:16:03 1.6
@@ -21,16 +21,18 @@
<!-- ========== Data Source Configuration =============================== -->
<!--
- <data-source
- autoCommit="false"
- description="Example Data Source Configuration"
- driverClass="org.postgresql.Driver"
- maxCount="4"
- minCount="2"
- password="mypassword"
- url="jdbc:postgresql://localhost/mydatabase"
- user="myusername"
- />
+ <data-sources>
+ <data-source
+ autoCommit="false"
+ description="Example Data Source Configuration"
+ driverClass="org.postgresql.Driver"
+ maxCount="4"
+ minCount="2"
+ password="mypassword"
+ url="jdbc:postgresql://localhost/mydatabase"
+ user="myusername"
+ />
+ </data-sources>
-->
<!-- ========== Form Bean Definitions =================================== -->