cziegeler 02/02/05 03:12:13
Modified: . changes.xml
src/java/org/apache/cocoon/transformation
SQLTransformer.java
Log:
Minor rewrite of the SQLTransformer. Removed several parameters tests
and the use of Properties objects - instead the Parameters object
is used directly.
Revision Changes Path
1.100 +6 -1 xml-cocoon2/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/xml-cocoon2/changes.xml,v
retrieving revision 1.99
retrieving revision 1.100
diff -u -r1.99 -r1.100
--- changes.xml 4 Feb 2002 14:18:35 -0000 1.99
+++ changes.xml 5 Feb 2002 11:12:12 -0000 1.100
@@ -4,7 +4,7 @@
<!--
History of Cocoon changes
- $Id: changes.xml,v 1.99 2002/02/04 14:18:35 vgritsenko Exp $
+ $Id: changes.xml,v 1.100 2002/02/05 11:12:12 cziegeler Exp $
-->
<changes title="History of Changes">
@@ -31,6 +31,11 @@
</devs>
<release version="@version@" date="@date@">
+ <action dev="CZ" type="update">
+ Minor rewrite of the SQLTransformer. Removed several parameters tests
+ and the use of Properties objects - instead the Parameters object
+ is used directly.
+ </action>
<action dev="VG" type="update" due-to="Konstantin Piroumian"
due-to-email="[EMAIL PROTECTED]">
Action LangSelect has been deprecated in favor of LocaleAction.
</action>
1.5 +103 -140
xml-cocoon2/src/java/org/apache/cocoon/transformation/SQLTransformer.java
Index: SQLTransformer.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/SQLTransformer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SQLTransformer.java 4 Feb 2002 15:29:57 -0000 1.4
+++ SQLTransformer.java 5 Feb 2002 11:12:13 -0000 1.5
@@ -78,18 +78,20 @@
/**
*
+ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Donald Ball</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
* (PWR Organisation & Entwicklung)
* @author <a href="mailto:[EMAIL PROTECTED]">Sven Beauprez</a>
- * @version CVS $Id: SQLTransformer.java,v 1.4 2002/02/04 15:29:57 cziegeler Exp $
+ * @version CVS $Id: SQLTransformer.java,v 1.5 2002/02/05 11:12:13 cziegeler Exp $
*/
-public class SQLTransformer extends AbstractTransformer implements Composable,
Recyclable, Disposable, Configurable {
+public class SQLTransformer
+ extends AbstractTransformer
+ implements Composable, Recyclable, Disposable, Configurable {
/** The SQL namespace **/
- public static final String my_uri = "http://apache.org/cocoon/SQL/2.0";
- public static final String my_name = "SQLTransformer";
+ public static final String NAMESPACE = "http://apache.org/cocoon/SQL/2.0";
/** The SQL namespace element names **/
public static final String MAGIC_EXECUTE_QUERY = "execute-query";
@@ -125,18 +127,15 @@
public static final String MAGIC_UPDATE_ATTRIBUTE = "isupdate";
/** The states we are allowed to be in **/
- public static final int STATE_OUTSIDE = 0;
- public static final int STATE_INSIDE_EXECUTE_QUERY_ELEMENT = 1;
- public static final int STATE_INSIDE_VALUE_ELEMENT = 2;
- public static final int STATE_INSIDE_QUERY_ELEMENT = 3;
- public static final int STATE_INSIDE_ANCESTOR_VALUE_ELEMENT = 4;
- public static final int STATE_INSIDE_SUBSTITUTE_VALUE_ELEMENT = 5;
- public static final int STATE_INSIDE_IN_PARAMETER_ELEMENT = 6;
- public static final int STATE_INSIDE_OUT_PARAMETER_ELEMENT = 7;
- public static final int STATE_INSIDE_ESCAPE_STRING = 8;
-
- /** Default parameters that might apply to all queries **/
- protected Properties default_properties;
+ protected static final int STATE_OUTSIDE = 0;
+ protected static final int STATE_INSIDE_EXECUTE_QUERY_ELEMENT = 1;
+ protected static final int STATE_INSIDE_VALUE_ELEMENT = 2;
+ protected static final int STATE_INSIDE_QUERY_ELEMENT = 3;
+ protected static final int STATE_INSIDE_ANCESTOR_VALUE_ELEMENT = 4;
+ protected static final int STATE_INSIDE_SUBSTITUTE_VALUE_ELEMENT = 5;
+ protected static final int STATE_INSIDE_IN_PARAMETER_ELEMENT = 6;
+ protected static final int STATE_INSIDE_OUT_PARAMETER_ELEMENT = 7;
+ protected static final int STATE_INSIDE_ESCAPE_STRING = 8;
/** The list of queries that we're currently working on **/
protected Vector queries;
@@ -154,7 +153,7 @@
protected StringBuffer current_value;
/** Check if nr of rows need to be written out. **/
- protected String showNrOfRows;
+ protected boolean showNrOfRows;
/** Is the old-driver turned on? (default is off) */
private boolean oldDriver = false;
@@ -168,13 +167,21 @@
/** The prefix of our namespace to listen to */
protected String inPrefix;
- protected ComponentSelector dbSelector = null;
+ /** The database selector */
+ protected ComponentSelector dbSelector;
+ /** The component manager */
protected ComponentManager manager;
+ /** The parameters of the current request */
protected Parameters parameters;
+ /** The current object model */
protected Map objectModel;
+ /**
+ * Composable
+ */
public void compose( ComponentManager manager ) {
+ this.queries = new Vector();
try {
this.manager = manager;
this.dbSelector = (ComponentSelector) manager.lookup(
DataSourceComponent.ROLE + "Selector" );
@@ -188,10 +195,14 @@
*/
public void recycle() {
super.recycle();
- this.queries = null;
+ this.queries.clear();
this.current_value = null;
this.current_name = null;
- this.default_properties = null;
+ this.parameters = null;
+ this.objectModel = null;
+ this.outUri = null;
+ this.inPrefix = null;
+ this.outPrefix = null;
}
/**
@@ -215,75 +226,31 @@
/**
* Setup for the current request
*/
-
public void setup( SourceResolver resolver, Map objectModel,
String source, Parameters parameters )
throws ProcessingException, SAXException, IOException {
// setup instance variables
- this.queries = new Vector();
this.current_value = new StringBuffer();
- this.current_name = null;
this.current_query_index = -1;
- this.default_properties = new Properties();
this.current_state = SQLTransformer.STATE_OUTSIDE;
this.parameters = parameters;
this.objectModel = objectModel;
- current_state = SQLTransformer.STATE_OUTSIDE;
-
- // Check for connection
- String parameter = parameters.getParameter(
SQLTransformer.MAGIC_CONNECTION, null );
- if ( parameter != null ) {
- getLogger().debug( "CONNECTION: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_CONNECTION,
parameter );
- } else {
- // Check the dburl
- parameter = parameters.getParameter( SQLTransformer.MAGIC_DBURL, null );
- if ( parameter != null ) {
- getLogger().debug( "DBURL: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_DBURL,
parameter );
- }
- // Check the username
- parameter = parameters.getParameter( SQLTransformer.MAGIC_USERNAME,
null );
- if ( parameter != null ) {
- getLogger().debug( "USERNAME: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_USERNAME,
parameter );
- }
-
- // Check the password
- parameter = parameters.getParameter( SQLTransformer.MAGIC_PASSWORD,
null );
- if ( parameter != null ) {
- default_properties.setProperty( SQLTransformer.MAGIC_PASSWORD,
parameter );
- }
- }
- showNrOfRows = parameters.getParameter( SQLTransformer.MAGIC_NR_OF_ROWS,
null );
-
- // Check for row-element and doc-element
- parameter = parameters.getParameter( SQLTransformer.MAGIC_DOC_ELEMENT, null
);
- if ( parameter != null ) {
- getLogger().debug( "DOC-ELEMENT: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_DOC_ELEMENT,
parameter );
- }
- parameter = parameters.getParameter( SQLTransformer.MAGIC_ROW_ELEMENT, null
);
- if ( parameter != null ) {
- getLogger().debug( "ROW-ELEMENT: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_ROW_ELEMENT,
parameter );
- }
-
- // Check namespace-prefix and namespace-uri
- parameter = parameters.getParameter( SQLTransformer.MAGIC_NS_URI_ELEMENT,
null );
- if ( parameter != null ) {
- getLogger().debug( "NS-URI: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_NS_URI_ELEMENT,
parameter );
- }
-
- parameter = parameters.getParameter(
SQLTransformer.MAGIC_NS_PREFIX_ELEMENT, null );
- if ( parameter != null ) {
- getLogger().debug( "NS-PREFIX: " + parameter );
- default_properties.setProperty( SQLTransformer.MAGIC_NS_PREFIX_ELEMENT,
parameter );
+ this.showNrOfRows = parameters.getParameterAsBoolean(
SQLTransformer.MAGIC_NR_OF_ROWS, false );
+ if ( this.getLogger().isDebugEnabled() ) {
+ if ( this.parameters.getParameter( SQLTransformer.MAGIC_CONNECTION ,
null ) != null ) {
+ this.getLogger().debug( "CONNECTION: " +
this.parameters.getParameter( SQLTransformer.MAGIC_CONNECTION , null ) );
+ } else {
+ this.getLogger().debug( "DBURL: " + parameters.getParameter(
SQLTransformer.MAGIC_DBURL, null ) );
+ this.getLogger().debug( "USERNAME: " + parameters.getParameter(
SQLTransformer.MAGIC_USERNAME, null ) );
+ }
+ this.getLogger().debug( "DOC-ELEMENT: " + parameters.getParameter(
SQLTransformer.MAGIC_DOC_ELEMENT, "rowset" ) );
+ this.getLogger().debug( "ROW-ELEMENT: " + parameters.getParameter(
SQLTransformer.MAGIC_ROW_ELEMENT, "row" ) );
+ this.getLogger().debug( "NS-URI: " + parameters.getParameter(
SQLTransformer.MAGIC_NS_URI_ELEMENT, NAMESPACE ) );
+ this.getLogger().debug( "NS-PREFIX: " + parameters.getParameter(
SQLTransformer.MAGIC_NS_PREFIX_ELEMENT, "" ) );
}
- }
+ }
/**
* This will be the meat of SQLTransformer, where the query is run.
@@ -291,23 +258,16 @@
protected void executeQuery( int index ) throws SAXException {
- // First set up the namespace handling
- if ( getCurrentQuery().properties.getProperty(
SQLTransformer.MAGIC_NS_URI_ELEMENT ) != null ) {
- outUri = getCurrentQuery().properties.getProperty(
SQLTransformer.MAGIC_NS_URI_ELEMENT );
- } else {
- outUri = my_uri;
- }
+ this.outUri = getCurrentQuery().properties.getParameter(
SQLTransformer.MAGIC_NS_URI_ELEMENT, NAMESPACE );
+ this.outPrefix = getCurrentQuery().properties.getParameter(
SQLTransformer.MAGIC_NS_PREFIX_ELEMENT, "" );
- if ( getCurrentQuery().properties.getProperty(
SQLTransformer.MAGIC_NS_PREFIX_ELEMENT ) != null ) {
- outPrefix = getCurrentQuery().properties.getProperty(
SQLTransformer.MAGIC_NS_PREFIX_ELEMENT );
- } else {
- outPrefix = "";
- }
- if ( !"".equals( outPrefix ) && !"".equals( outUri ) ) {
- this.contentHandler.startPrefixMapping( outPrefix, outUri );
+ if ( !"".equals( this.outPrefix ) && !"".equals( this.outUri ) ) {
+ this.contentHandler.startPrefixMapping( this.outPrefix, this.outUri );
}
- getLogger().debug( "SQLTransformer executing query nr " + index );
+ if ( this.getLogger().isDebugEnabled() ) {
+ this.getLogger().debug( "SQLTransformer executing query nr " + index );
+ }
AttributesImpl attr = new AttributesImpl();
Query query = (Query) queries.elementAt( index );
boolean query_failure = false;
@@ -326,13 +286,13 @@
}
if ( !query_failure ) {
- if ( showNrOfRows != null && showNrOfRows.equalsIgnoreCase( "true"
) ) {
- attr.addAttribute( my_uri, query.nr_of_rows, query.nr_of_rows,
"CDATA",
+ if ( this.showNrOfRows ) {
+ attr.addAttribute( NAMESPACE, query.nr_of_rows,
query.nr_of_rows, "CDATA",
String.valueOf( query.getNrOfRows() ) );
}
String name = query.getName();
if ( name != null ) {
- attr.addAttribute( my_uri, query.name_attribute,
query.name_attribute, "CDATA",
+ attr.addAttribute( NAMESPACE, query.name_attribute,
query.name_attribute, "CDATA",
name );
}
this.start( query.rowset_name, attr );
@@ -361,13 +321,13 @@
getLogger().warn( "SQLTransformer: Could not close JDBC
connection", e );
}
}
- if ( !"".equals( outPrefix ) && !"".equals( outUri ) ) {
- this.contentHandler.endPrefixMapping( outPrefix );
+ if ( !"".equals( this.outPrefix ) && !"".equals( this.outUri ) ) {
+ this.contentHandler.endPrefixMapping( this.outPrefix );
}
}
protected static void throwIllegalStateException( String message ) {
- throw new IllegalStateException( my_name + ": " + message );
+ throw new IllegalStateException( "SQLTransformer: " + message );
}
protected void startExecuteQueryElement() {
@@ -477,14 +437,14 @@
case SQLTransformer.STATE_INSIDE_QUERY_ELEMENT:
int level = 0;
try {
- level = Integer.parseInt( attributes.getValue( my_uri,
+ level = Integer.parseInt( attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_ANCESTOR_VALUE_LEVEL_ATTRIBUTE ) );
} catch ( Exception e ) {
getLogger().debug( "SQLTransformer", e );
throwIllegalStateException( "Ancestor value elements must have
a " +
SQLTransformer.MAGIC_ANCESTOR_VALUE_LEVEL_ATTRIBUTE + " attribute" );
}
- String name = attributes.getValue( my_uri,
+ String name = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_ANCESTOR_VALUE_NAME_ATTRIBUTE );
if ( name == null ) {
throwIllegalStateException( "Ancestor value elements must have
a " +
@@ -516,7 +476,7 @@
protected void startSubstituteValueElement( Attributes attributes ) {
switch ( current_state ) {
case SQLTransformer.STATE_INSIDE_QUERY_ELEMENT:
- String name = attributes.getValue( my_uri,
+ String name = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_SUBSTITUTE_VALUE_NAME_ATTRIBUTE );
if ( name == null ) {
throwIllegalStateException( "Substitute value elements must
have a " +
@@ -584,9 +544,9 @@
protected void startInParameterElement( Attributes attributes ) {
switch ( current_state ) {
case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
- String nr = attributes.getValue( my_uri,
+ String nr = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_IN_PARAMETER_NR_ATTRIBUTE );
- String value = attributes.getValue( my_uri,
+ String value = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_IN_PARAMETER_VALUE_ATTRIBUTE );
getLogger().debug( "IN PARAMETER NR " + nr + "; VALUE " + value );
int position = Integer.parseInt( nr );
@@ -605,11 +565,11 @@
protected void startOutParameterElement( Attributes attributes ) {
switch ( current_state ) {
case SQLTransformer.STATE_INSIDE_EXECUTE_QUERY_ELEMENT:
- String name = attributes.getValue( my_uri,
+ String name = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_OUT_PARAMETER_NAME_ATTRIBUTE );
- String nr = attributes.getValue( my_uri,
+ String nr = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_OUT_PARAMETER_NR_ATTRIBUTE );
- String type = attributes.getValue( my_uri,
+ String type = attributes.getValue( NAMESPACE,
SQLTransformer.MAGIC_OUT_PARAMETER_TYPE_ATTRIBUTE );
getLogger().debug( "OUT PARAMETER NAME" + name + ";NR " + nr + ";
TYPE " + type );
int position = Integer.parseInt( nr );
@@ -673,7 +633,7 @@
* ContentHandler method
*/
public void startPrefixMapping( String prefix, String uri ) throws SAXException
{
- if ( uri.equals( my_uri ) ) {
+ if ( uri.equals( NAMESPACE ) ) {
inPrefix = prefix;
} else {
super.contentHandler.startPrefixMapping( prefix, uri );
@@ -707,7 +667,7 @@
*/
public void startElement( String uri, String name, String raw,
Attributes attributes ) throws SAXException {
- if ( !uri.equals( my_uri ) ) {
+ if ( !uri.equals( NAMESPACE ) ) {
super.startElement( uri, name, raw, attributes );
return;
}
@@ -737,7 +697,7 @@
*/
public void endElement( String uri, String name,
String raw ) throws SAXException {
- if ( !uri.equals( my_uri ) ) {
+ if ( !uri.equals( NAMESPACE ) ) {
super.endElement( uri, name, raw );
return;
}
@@ -836,12 +796,12 @@
protected int query_index;
/** SQL configuration information **/
- protected Properties properties;
+ protected Parameters properties;
/** Dummy static variables for the moment **/
- protected String rowset_name = "rowset";
+ protected String rowset_name;
+ protected String row_name;
protected String nr_of_rows = "nrofrows";
- protected String row_name = "row";
protected String name_attribute = "name";
/** The connection, once opened **/
@@ -883,11 +843,12 @@
protected Query( SQLTransformer transformer, int query_index ) {
this.transformer = transformer;
this.query_index = query_index;
- this.properties = new Properties( transformer.default_properties );
+ this.properties = new Parameters();
+ this.properties.merge( transformer.parameters );
}
protected void setParameter( String name, String value ) {
- properties.setProperty( name, value );
+ properties.setParameter( name, value );
}
protected void setUpdate( boolean flag ) {
@@ -962,7 +923,7 @@
fieldName = type.substring( index + 1, type.length() );
} else {
transformer.getTheLogger().error( "Invalid SQLType: " + type,
null );
- throw new SQLException( "Wrong SQLType" );
+ throw new SQLException( "Invalid SQLType: " + type);
}
try {
clss = Class.forName( className );
@@ -980,28 +941,39 @@
protected Connection getConnection() throws SQLException {
Connection result = null;
- String connection = properties.getProperty(
SQLTransformer.MAGIC_CONNECTION );
- String dburl = properties.getProperty( SQLTransformer.MAGIC_DBURL );
- String username = properties.getProperty( SQLTransformer.MAGIC_USERNAME
);
- String password = properties.getProperty( SQLTransformer.MAGIC_PASSWORD
);
-
- DataSourceComponent datasource = null;
try {
+ final String connection = properties.getParameter(
SQLTransformer.MAGIC_CONNECTION, null );
if ( connection != null ) {
- datasource = (DataSourceComponent) dbSelector.select(
connection );
- while ( result == null ) {
+ if (this.transformer.dbSelector == null) {
+ transformer.getTheLogger().error( "No DBSelector found,
could not use connection: " + connection);
+ } else {
+ DataSourceComponent datasource = null;
+
try {
- result = datasource.getConnection();
- } catch ( Exception e ) {
- long waittime = 5000 * (long) Math.random();
- getTheLogger().debug( "SQLTransformer$Query: could not
acquire a Connection -- waiting " + waittime + " ms to try again." );
- try {
- Thread.sleep( waittime );
- } catch ( InterruptedException ie ) {
+ datasource = (DataSourceComponent)
this.transformer.dbSelector.select( connection );
+ while ( result == null ) {
+ try {
+ result = datasource.getConnection();
+ } catch ( Exception e ) {
+ long waittime = 5000 * (long) Math.random();
+ getTheLogger().debug( "SQLTransformer$Query:
could not acquire a Connection -- waiting " + waittime + " ms to try again." );
+ try {
+ Thread.sleep( waittime );
+ } catch ( InterruptedException ie ) {
+ }
+ }
}
+ } catch ( ComponentException cme ) {
+ transformer.getTheLogger().error( "Could not use
connection: " + connection, cme );
+ } finally {
+ if ( datasource != null )
this.transformer.dbSelector.release( datasource );
}
}
} else {
+ final String dburl = properties.getParameter(
SQLTransformer.MAGIC_DBURL, null );
+ final String username = properties.getParameter(
SQLTransformer.MAGIC_USERNAME, null );
+ final String password = properties.getParameter(
SQLTransformer.MAGIC_PASSWORD, null );
+
if ( username == null || password == null ) {
result = DriverManager.getConnection( dburl );
} else {
@@ -1009,26 +981,17 @@
password );
}
}
- } catch ( ComponentException cme ) {
- transformer.getTheLogger().error( "Could not use connection: " +
connection, cme );
} catch ( SQLException e ) {
transformer.getTheLogger().error( "Caught a SQLException", e );
throw e;
- } finally {
- if ( datasource != null ) dbSelector.release( datasource );
}
return result;
}
protected void execute() throws SQLException {
- if ( null != properties.getProperty( SQLTransformer.MAGIC_DOC_ELEMENT )
) {
- this.rowset_name = properties.getProperty(
SQLTransformer.MAGIC_DOC_ELEMENT );
- }
- if ( null != properties.getProperty( SQLTransformer.MAGIC_ROW_ELEMENT )
) {
- this.row_name = properties.getProperty(
SQLTransformer.MAGIC_ROW_ELEMENT );
- }
-
+ this.rowset_name = properties.getParameter(
SQLTransformer.MAGIC_DOC_ELEMENT, "rowset" );
+ this.row_name = properties.getParameter(
SQLTransformer.MAGIC_ROW_ELEMENT, "row" );
Enumeration enum = query_parts.elements();
StringBuffer sb = new StringBuffer();
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]