You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by lu...@apache.org on 2002/04/24 03:16:31 UTC
cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/sql QueryTag.java UpdateTag.java
luehe 02/04/23 18:16:31
Modified: standard/src/org/apache/taglibs/standard/tag/common/sql
DataSourceUtil.java QueryTagSupport.java
SetDataSourceTagSupport.java
TransactionTagSupport.java UpdateTagSupport.java
standard/src/org/apache/taglibs/standard/tag/el/sql
QueryTag.java UpdateTag.java
standard/src/org/apache/taglibs/standard/tag/rt/sql
QueryTag.java UpdateTag.java
Log:
Fixed 8319: sql:transaction dataSource attribute not taking precedence
Also replaced this sequence of calls:
DataSourceUtil dsUtil = new DataSourceUtil();
dsUtil.setDataSource(rawDataSource, pageContext);
DateSource dataSource = dsUtil.getDataSource();
with
DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
pageContext);
Revision Changes Path
1.8 +27 -33 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/DataSourceUtil.java
Index: DataSourceUtil.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/DataSourceUtil.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DataSourceUtil.java 19 Apr 2002 18:02:41 -0000 1.7
+++ DataSourceUtil.java 24 Apr 2002 01:16:31 -0000 1.8
@@ -74,45 +74,33 @@
*/
public class DataSourceUtil {
- private static String ESCAPE = "\\";
- private static String TOKEN = ",";
- private DataSource dataSource;
- private boolean hasDataSourceAttribute = false;
-
- public DataSource getDataSource() {
- return dataSource;
- }
+ private static final String ESCAPE = "\\";
+ private static final String TOKEN = ",";
/**
- * Useful for Transaction nesting check
- */
- public boolean hasDataSourceAttribute() {
- return hasDataSourceAttribute;
- }
-
- /**
- * If dataSource is a String first do JNDI lookup
+ * If dataSource is a String first do JNDI lookup.
* If lookup fails parse String like it was a set of JDBC parameters
* Otherwise check to see if dataSource is a DataSource object and use as
* is
*/
- public void setDataSource(Object rawDataSource,
- PageContext pageContext) throws JspException {
+ static DataSource getDataSource(Object rawDataSource, PageContext pc)
+ throws JspException
+ {
+ DataSource dataSource = null;
if (rawDataSource == null) {
- rawDataSource = Config.find(pageContext, Config.SQL_DATASOURCE);
- hasDataSourceAttribute = false;
- } else {
- hasDataSourceAttribute = true;
+ rawDataSource = Config.find(pc, Config.SQL_DATASOURCE);
}
if (rawDataSource == null) {
- return;
+ return null;
}
- // If the 'dataSource' attribute's value resolves to a String
- // after rtexpr/EL evaluation, use the string as JNDI path to
- // a DataSource
+ /*
+ * If the 'dataSource' attribute's value resolves to a String
+ * after rtexpr/EL evaluation, use the string as JNDI path to
+ * a DataSource
+ */
if (rawDataSource instanceof String) {
try {
Context ctx = new InitialContext();
@@ -120,7 +108,7 @@
Context envCtx = (Context) ctx.lookup("java:comp/env");
dataSource = (DataSource) envCtx.lookup((String) rawDataSource);
} catch (NamingException ex) {
- setUsingParams((String) rawDataSource);
+ dataSource = getDataSource((String) rawDataSource);
}
} else if (rawDataSource instanceof DataSource) {
dataSource = (DataSource) rawDataSource;
@@ -128,13 +116,17 @@
throw new JspException(
Resources.getMessage("SQL_DATASOURCE_INVALID"));
}
+
+ return dataSource;
}
/**
* Parse JDBC parameters and setup dataSource appropriately
*/
- private void setUsingParams(String params) throws JspException {
- dataSource = new DataSourceWrapper();
+ private static DataSource getDataSource(String params)
+ throws JspException
+ {
+ DataSourceWrapper dataSource = new DataSourceWrapper();
String[] paramString = new String[4];
int escCount = 0;
@@ -163,12 +155,12 @@
paramString[aryCount] = params.substring(begin);
// use the JDBC URL from the parameter string
- ((DataSourceWrapper) dataSource).setJdbcURL(paramString[0]);
+ dataSource.setJdbcURL(paramString[0]);
// try to load a driver if it's present
if (paramString[1] != null) {
try {
- ((DataSourceWrapper) dataSource).setDriverClassName(paramString[1]);
+ dataSource.setDriverClassName(paramString[1]);
} catch (Exception ex) {
throw new JspTagException(
Resources.getMessage("DRIVER_INVALID_CLASS",
@@ -177,8 +169,10 @@
}
// set the username and password
- ((DataSourceWrapper) dataSource).setUserName(paramString[2]);
- ((DataSourceWrapper) dataSource).setPassword(paramString[3]);
+ dataSource.setUserName(paramString[2]);
+ dataSource.setPassword(paramString[3]);
+
+ return dataSource;
}
}
1.26 +20 -25 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/QueryTagSupport.java
Index: QueryTagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/QueryTagSupport.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- QueryTagSupport.java 23 Apr 2002 20:51:22 -0000 1.25
+++ QueryTagSupport.java 24 Apr 2002 01:16:31 -0000 1.26
@@ -83,14 +83,13 @@
private String var;
private int scope;
-
/*
* The following properties take expression values, so the
* setter methods are implemented by the expression type
* specific subclasses.
*/
protected Object rawDataSource;
- protected DataSource dataSource;
+ protected boolean dataSourceSpecified;
protected String sql;
protected int maxRows;
protected boolean maxRowsSpecified;
@@ -102,10 +101,10 @@
private Connection conn;
private List parameters;
private boolean isPartOfTransaction;
- private DataSourceUtil dsUtil;
+
//*********************************************************************
- // Constructor
+ // Constructor and initialization
public QueryTagSupport() {
super();
@@ -116,7 +115,7 @@
scope = PageContext.PAGE_SCOPE;
startRow = 0;
maxRows = -1;
- maxRowsSpecified = false;
+ maxRowsSpecified = dataSourceSpecified = false;
}
@@ -126,7 +125,6 @@
/**
* Setter method for the name of the variable to hold the
* result.
- *
*/
public void setVar(String var) {
this.var = var;
@@ -135,7 +133,6 @@
/**
* Setter method for the scope of the variable to hold the
* result.
- *
*/
public void setScope(String scopeName) {
scope = Util.getScope(scopeName);
@@ -163,6 +160,13 @@
* getting the <code>Connection</code>
*/
public int doStartTag() throws JspException {
+
+ // Reset the per-invokation state.
+ parameters = null;
+ isPartOfTransaction = false;
+ conn = null;
+ rawDataSource = null;
+
if (!maxRowsSpecified) {
Object obj = Config.find(pageContext, Config.SQL_MAXROWS);
if (obj != null) {
@@ -184,15 +188,12 @@
}
}
- dsUtil = new DataSourceUtil();
- dsUtil.setDataSource(rawDataSource, pageContext);
- dataSource = dsUtil.getDataSource();
try {
conn = getConnection();
- }
- catch (SQLException e) {
+ } catch (SQLException e) {
throw new JspException(sql + ": " + e.getMessage(), e);
}
+
return EVAL_BODY_BUFFERED;
}
@@ -269,19 +270,11 @@
if (conn != null && !isPartOfTransaction) {
try {
conn.close();
- }
- catch (SQLException e) {} // Not much we can do
+ } catch (SQLException e) {} // Not much we can do
}
- /*
- * Reset the per-invokation state.
- */
- parameters = null;
- isPartOfTransaction = false;
- conn = null;
- dataSource = null;
- rawDataSource = null;
}
+
//*********************************************************************
// Private utility methods
@@ -291,14 +284,15 @@
TransactionTagSupport parent = (TransactionTagSupport)
findAncestorWithClass(this, TransactionTagSupport.class);
if (parent != null) {
- if (dsUtil.hasDataSourceAttribute()) {
+ if (dataSourceSpecified) {
throw new JspTagException(
Resources.getMessage("ERROR_NESTED_DATASOURCE"));
}
conn = parent.getSharedConnection();
isPartOfTransaction = true;
- }
- else {
+ } else {
+ DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
+ pageContext);
try {
conn = dataSource.getConnection();
} catch (Exception ex) {
@@ -306,6 +300,7 @@
Resources.getMessage("DATASOURCE_INVALID"));
}
}
+
return conn;
}
1.8 +4 -7 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/SetDataSourceTagSupport.java
Index: SetDataSourceTagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/SetDataSourceTagSupport.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SetDataSourceTagSupport.java 19 Apr 2002 22:52:01 -0000 1.7
+++ SetDataSourceTagSupport.java 24 Apr 2002 01:16:31 -0000 1.8
@@ -107,11 +107,8 @@
DataSource ds;
if (dataSource != null) {
- DataSourceUtil dsUtil = new DataSourceUtil();
- dsUtil.setDataSource(dataSource, pageContext);
- ds = dsUtil.getDataSource();
- }
- else {
+ ds = DataSourceUtil.getDataSource(dataSource, pageContext);
+ } else {
DataSourceWrapper dsw = new DataSourceWrapper();
try {
// set driver class iff provided by the tag
@@ -132,10 +129,10 @@
if (var != null) {
pageContext.setAttribute(var, ds, scope);
- }
- else {
+ } else {
Config.set(pageContext, Config.SQL_DATASOURCE, ds, scope);
}
+
return SKIP_BODY;
}
}
1.10 +1 -6 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/TransactionTagSupport.java
Index: TransactionTagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/TransactionTagSupport.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TransactionTagSupport.java 23 Apr 2002 02:07:15 -0000 1.9
+++ TransactionTagSupport.java 24 Apr 2002 01:16:31 -0000 1.10
@@ -102,7 +102,6 @@
// Private state
private Connection conn;
- private DataSourceUtil dsUtil;
private int isolation;
private int origIsolation;
@@ -117,7 +116,6 @@
private void init() {
conn = null;
- dsUtil = null;
isolation = Connection.TRANSACTION_NONE;
}
@@ -132,10 +130,7 @@
*/
public int doStartTag() throws JspException {
- dsUtil = new DataSourceUtil();
- dsUtil.setDataSource(rawDataSource, pageContext);
-
- dataSource = dsUtil.getDataSource();
+ dataSource = DataSourceUtil.getDataSource(rawDataSource, pageContext);
try {
conn = getConnection();
1.17 +47 -38 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/UpdateTagSupport.java
Index: UpdateTagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/UpdateTagSupport.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- UpdateTagSupport.java 16 Apr 2002 00:01:34 -0000 1.16
+++ UpdateTagSupport.java 24 Apr 2002 01:16:31 -0000 1.17
@@ -80,7 +80,7 @@
implements TryCatchFinally, SQLExecutionTag {
private String var;
- private int scope = PageContext.PAGE_SCOPE;
+ private int scope;
/*
* The following properties take expression values, so the
@@ -88,7 +88,7 @@
* specific subclasses.
*/
protected Object rawDataSource;
- protected DataSource dataSource;
+ protected boolean dataSourceSpecified;
protected String sql;
/*
@@ -97,7 +97,21 @@
private Connection conn;
private List parameters;
private boolean isPartOfTransaction;
- private DataSourceUtil dsUtil;
+
+
+ //*********************************************************************
+ // Constructor and initialization
+
+ public UpdateTagSupport() {
+ super();
+ init();
+ }
+
+ private void init() {
+ scope = PageContext.PAGE_SCOPE;
+ dataSourceSpecified = false;
+ }
+
//*********************************************************************
// Accessor methods
@@ -105,7 +119,6 @@
/**
* Setter method for the name of the variable to hold the
* result.
- *
*/
public void setVar(String var) {
this.var = var;
@@ -114,25 +127,11 @@
/**
* Setter method for the scope of the variable to hold the
* result.
- *
*/
public void setScope(String scopeName) {
scope = Util.getScope(scopeName);
}
- //*********************************************************************
- // Public utility methods
-
- /**
- * Called by nested parameter elements to add PreparedStatement
- * parameter values.
- */
- public void addSQLParameter(Object o) {
- if (parameters == null) {
- parameters = new ArrayList();
- }
- parameters.add(o);
- }
//*********************************************************************
// Tag logic
@@ -143,17 +142,19 @@
*/
public int doStartTag() throws JspException {
- dsUtil = new DataSourceUtil();
- dsUtil.setDataSource(rawDataSource, pageContext);
-
- dataSource = dsUtil.getDataSource();
+ // Reset the per-invokation state.
+ parameters = null;
+ isPartOfTransaction = false;
+ conn = null;
+ bodyContent = null;
+ rawDataSource = null;
try {
conn = getConnection();
- }
- catch (SQLException e) {
+ } catch (SQLException e) {
throw new JspException(sql + ": " + e.getMessage(), e);
}
+
return EVAL_BODY_BUFFERED;
}
@@ -216,20 +217,26 @@
if (conn != null && !isPartOfTransaction) {
try {
conn.close();
- }
- catch (SQLException e) {} // Not much we can do
+ } catch (SQLException e) {} // Not much we can do
}
- /*
- * Reset the per-invokation state.
- */
- parameters = null;
- isPartOfTransaction = false;
- conn = null;
- dataSource = null;
- bodyContent = null;
- rawDataSource = null;
}
+
+ //*********************************************************************
+ // Public utility methods
+
+ /**
+ * Called by nested parameter elements to add PreparedStatement
+ * parameter values.
+ */
+ public void addSQLParameter(Object o) {
+ if (parameters == null) {
+ parameters = new ArrayList();
+ }
+ parameters.add(o);
+ }
+
+
//*********************************************************************
// Private utility methods
@@ -239,14 +246,15 @@
TransactionTagSupport parent = (TransactionTagSupport)
findAncestorWithClass(this, TransactionTagSupport.class);
if (parent != null) {
- if (dsUtil.hasDataSourceAttribute()) {
+ if (dataSourceSpecified) {
throw new JspTagException(
Resources.getMessage("ERROR_NESTED_DATASOURCE"));
}
conn = parent.getSharedConnection();
isPartOfTransaction = true;
- }
- else {
+ } else {
+ DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
+ pageContext);
try {
conn = dataSource.getConnection();
} catch (Exception ex) {
@@ -254,6 +262,7 @@
Resources.getMessage("DATASOURCE_INVALID"));
}
}
+
return conn;
}
1.6 +6 -4 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/sql/QueryTag.java
Index: QueryTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/sql/QueryTag.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- QueryTag.java 17 Apr 2002 18:11:12 -0000 1.5
+++ QueryTag.java 24 Apr 2002 01:16:31 -0000 1.6
@@ -90,6 +90,7 @@
public void setDataSource(String dataSourceEL) {
this.dataSourceEL = dataSourceEL;
+ this.dataSourceSpecified = true;
}
/**
@@ -132,10 +133,10 @@
Object tempInt = null;
if (dataSourceEL != null) {
- rawDataSource = (Object)
- ExpressionEvaluatorManager.evaluate("dataSource",
- dataSourceEL, Object.class, this, pageContext);
+ rawDataSource = (Object) ExpressionEvaluatorManager.evaluate(
+ "dataSource", dataSourceEL, Object.class, this, pageContext);
}
+
if (sqlEL != null) {
sql = (String) ExpressionEvaluatorManager.evaluate("sql", sqlEL,
String.class, this, pageContext);
@@ -156,7 +157,8 @@
maxRows = ((Integer) tempInt).intValue();
}
} catch (Exception ex) {
- throw new JspException(Resources.getMessage("PARAM_BAD_VALUE"), ex);
+ throw new JspException(Resources.getMessage("PARAM_BAD_VALUE"),
+ ex);
}
}
}
1.3 +1 -0 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/sql/UpdateTag.java
Index: UpdateTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/sql/UpdateTag.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- UpdateTag.java 16 Jan 2002 02:22:20 -0000 1.2
+++ UpdateTag.java 24 Apr 2002 01:16:31 -0000 1.3
@@ -71,6 +71,7 @@
public void setDataSource(String dataSourceEL) {
this.dataSourceEL = dataSourceEL;
+ this.dataSourceSpecified = true;
}
/**
1.4 +1 -0 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/sql/QueryTag.java
Index: QueryTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/sql/QueryTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- QueryTag.java 17 Apr 2002 18:11:12 -0000 1.3
+++ QueryTag.java 24 Apr 2002 01:16:31 -0000 1.4
@@ -84,6 +84,7 @@
public void setDataSource(Object dataSource) {
this.rawDataSource = dataSource;
+ this.dataSourceSpecified = true;
}
/**
1.3 +1 -0 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/sql/UpdateTag.java
Index: UpdateTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/sql/UpdateTag.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- UpdateTag.java 4 Apr 2002 02:21:14 -0000 1.2
+++ UpdateTag.java 24 Apr 2002 01:16:31 -0000 1.3
@@ -76,6 +76,7 @@
*/
public void setDataSource(Object dataSource) {
this.rawDataSource = dataSource;
+ this.dataSourceSpecified = true;
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>