bloritsch 2003/02/27 07:04:12
Modified: datasource build.xml
datasource/src/java/org/apache/avalon/excalibur/datasource
InformixDataSource.java
Log:
make Informix strictly a runtime dependency. Requires ugly reflection to do the
work, but hey it is better than requiring a third party commercial jar to get a full
distro
Revision Changes Path
1.33 +0 -9 avalon-excalibur/datasource/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/avalon-excalibur/datasource/build.xml,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -r1.32 -r1.33
--- build.xml 20 Feb 2003 14:23:30 -0000 1.32
+++ build.xml 27 Feb 2003 15:04:12 -0000 1.33
@@ -16,8 +16,6 @@
<pathelement location="${jdbc.jar}"/>
<pathelement location="${mysql.jar}"/>
<pathelement location="${hsqldb.jar}"/>
- <pathelement location="${informix.jar}"/>
- <pathelement location="${informix-extra.jar}"/>
<pathelement location="${avalon-framework.jar}"/>
<pathelement location="${avalon-logkit.jar}"/> <!-- deprecated -->
<pathelement location="${logkit.jar}"/>
@@ -79,9 +77,6 @@
<available property="jdbc3.present" classname="java.sql.Savepoint">
<classpath refid="project.class.path"/>
</available>
- <available property="informix.present"
classname="com.informix.jdbc.IfxDriver">
- <classpath refid="project.class.path"/>
- </available>
</target>
<target name="check-datasource" depends="check-environment"
if="datasource.present">
@@ -142,8 +137,6 @@
<exclude
name="org/apache/avalon/excalibur/datasource/J2eeDataSource.java"
unless="j2ee.present"/>
- <exclude
name="org/apache/avalon/excalibur/datasource/InformixDataSource.java"
- unless="informix.present"/>
<exclude
name="org/apache/avalon/excalibur/datasource/Jdbc3Connection.java"
unless="jdbc3.present"/>
@@ -299,8 +292,6 @@
<exclude
name="org/apache/avalon/excalibur/datasource/Jdbc2Connection.java"/>
<exclude
name="org/apache/avalon/excalibur/datasource/J2eeDataSource.java"
unless="j2ee.present"/>
- <exclude
name="org/apache/avalon/excalibur/datasource/InformixDataSource.java"
- unless="informix.present"/>
</fileset>
</copy>
1.13 +234 -200
avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/InformixDataSource.java
Index: InformixDataSource.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/datasource/src/java/org/apache/avalon/excalibur/datasource/InformixDataSource.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- InformixDataSource.java 25 Feb 2003 16:28:37 -0000 1.12
+++ InformixDataSource.java 27 Feb 2003 15:04:12 -0000 1.13
@@ -1,200 +1,234 @@
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE.txt file.
- */
-package org.apache.avalon.excalibur.datasource;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.logger.LogKitLogger;
-import org.apache.avalon.framework.logger.Loggable;
-
-/**
- * The Informix implementation for DataSources in Excalibur. This uses the
- * <code>com.informix.jdbcx.IfxConnectionPoolDataSource</code> object. It uses
- * the following format for configuration (italics mark information you change):
- *
- * <pre>
- * <informix>
- * <pool-controller init="<i>5</i>" min="<i>5</i>" max="<i>10</i>"/>
- * <dbname><i>dbname</i></dbname>
- * <servername><i>servername</i></servername>
- * <host port="<i>2000</i>"><i>host</i></host>
- * <user><i>user</i></user>
- * <password><i>password</i></password>
- * <tracing>
- * <jdbc> file="<i>filename</i>" level="<i>level</i>"</jdbc>
- * <sqli> file="<i>filename</i>" level="<i>level</i>"</sqli>
- * </tracing>
- * <informix>
- * </pre>
- *
- * <p>
- * Informix doesn't like the JdbcDataSource Component, so we gave it it's own.
- * Do not use this datasource if you are planning on using your J2EE server's
- * connection pooling.
- * <p>
- *
- * <p>
- * You must have Informix's JDBC 2.2 or higher jar file, as well as the
- * extensions jar file (<code>ifxjdbc.jar</code> and <code>ifxjdbcx.jar</code>).
- * Also, this DataSource requires the Avalon Cadastre package because it uses
- * the MemoryContext.
- * </p>
- *
- * <p>
- * The <i>tracing</i> settings optionally enable Informix's tracing support
- * within the jdbc driver. <strong>Note</strong>, for this to work, the
- * <code>ifxjdbc-g.jar</code> and <code>ifxjdbcx-g.jar</code> jar files are
- * required (the options have no effect when using the non -g jar files).
- * </p>
- *
- * <p>
- * <i>jdbc tracing</i> enables general logging information about the driver
- * itself. <i>sqli tracing</i> enables logging of native sqli messages sent
- * between the jdbc driver and the database server.
- * </p>
- *
- * <p>
- * The attribute <code>file</code> specifies where to write tracing information
- * to, and <code>level</code> specifies the tracing level to be used, as
- * documented in the Informix JDBC programmers guide.
- * </p>
- *
- * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a>
- * @version CVS $Revision$ $Date$
- * @since 4.0
- */
-public class InformixDataSource
- extends AbstractLogEnabled
- implements DataSourceComponent, Loggable
-{
- private IfxDataSource m_dataSource;
- private boolean m_autocommit;
- private static boolean INIT_FACTORY = false;
-
- /**
- * Set up the system property for the context factory if it hasn't been
- * done already. This is not done in a static initializer due to the
- * existence of the J2eeDataSource.
- */
- public InformixDataSource()
- {
- if( !InformixDataSource.INIT_FACTORY )
- {
- System.setProperty( Context.INITIAL_CONTEXT_FACTORY,
-
"org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory" );
- }
- }
-
- public void setLogger( final org.apache.log.Logger logger )
- {
- enableLogging( new LogKitLogger( logger ) );
- }
-
- /**
- * Return an Informix Connection object
- */
- public Connection getConnection() throws SQLException
- {
- Connection conn = m_dataSource.getConnection();
-
- if( conn.getAutoCommit() != m_autocommit )
- {
- conn.setAutoCommit( m_autocommit );
- }
-
- return conn;
- }
-
- /**
- * Set up the Informix driver for direct use.
- */
- public void configure( Configuration conf ) throws ConfigurationException
- {
- Configuration poolController = conf.getChild( "pool-controller" );
- String dbname = conf.getChild( "dbname" ).getValue( "ifx" );
- IfxConnectionPoolDataSource pooledDataSource = new
IfxConnectionPoolDataSource();
- m_autocommit = conf.getChild( "autocommit" ).getValueAsBoolean( true );
-
- pooledDataSource.setIfxCPMInitPoolSize(
poolController.getAttributeAsInteger( "init", 5 ) );
- pooledDataSource.setIfxCPMMinPoolSize(
poolController.getAttributeAsInteger( "min", 5 ) );
- pooledDataSource.setIfxCPMMaxPoolSize(
poolController.getAttributeAsInteger( "max", 10 ) );
- pooledDataSource.setIfxCPMServiceInterval( 100 );
- pooledDataSource.setServerName( conf.getChild( "servername" ).getValue() );
- pooledDataSource.setDatabaseName( conf.getChild( "dbname" ).getValue() );
- pooledDataSource.setIfxIFXHOST( conf.getChild( "host" ).getValue() );
- pooledDataSource.setPortNumber( conf.getChild( "host"
).getAttributeAsInteger( "port" ) );
- pooledDataSource.setUser( conf.getChild( "user" ).getValue() );
- pooledDataSource.setPassword( conf.getChild( "password" ).getValue() );
-
- try
- {
- Context context = new InitialContext();
-
- context.bind( dbname + "pool", pooledDataSource );
-
- m_dataSource = new IfxDataSource();
- m_dataSource.setDataSourceName( dbname + "pool" );
- m_dataSource.setServerName( conf.getChild( "servername" ).getValue() );
- m_dataSource.setDatabaseName( conf.getChild( "dbname" ).getValue() );
- m_dataSource.setIfxIFXHOST( conf.getChild( "host" ).getValue() );
- m_dataSource.setPortNumber( conf.getChild( "host"
).getAttributeAsInteger( "port" ) );
- m_dataSource.setUser( conf.getChild( "user" ).getValue() );
- m_dataSource.setPassword( conf.getChild( "password" ).getValue() );
- configureTracing( conf.getChild( "tracing", false ) );
-
- context.bind( dbname, m_dataSource );
- }
- catch( Exception e )
- {
- if( getLogger().isErrorEnabled() )
- {
- getLogger().error( "There was an error trying to bind the
connection pool", e );
- }
- throw new ConfigurationException( "There was an error trying to bind
the connection pool", e );
- }
- }
-
- /**
- * Helper method to enable tracing support in the Informix driver.
- *
- * @param config a <code>Configuration</code> value
- * @exception ConfigurationException if an error occurs
- */
- private void configureTracing( final Configuration config )
- throws ConfigurationException
- {
- if ( config != null )
- {
- Configuration child = config.getChild( "jdbc", false );
-
- if (child != null)
- {
- // enables tracing on the jdbc driver itself
- m_dataSource.setIfxTRACE( child.getAttributeAsInteger( "level" ) );
- m_dataSource.setIfxTRACEFILE( child.getAttribute( "file" ) );
- }
-
- child = config.getChild( "sqli", false );
-
- if (child != null)
- {
- // enables sqli message tracing
- m_dataSource.setIfxPROTOCOLTRACE( child.getAttributeAsInteger(
"level" ) );
- m_dataSource.setIfxPROTOCOLTRACEFILE( child.getAttribute( "file" )
);
- }
- }
- }
-}
+/*
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
+ *
+ * This software is published under the terms of the Apache Software License
+ * version 1.1, a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ */
+package org.apache.avalon.excalibur.datasource;
+
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+import javax.sql.ConnectionPoolDataSource;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+
+/**
+ * The Informix implementation for DataSources in Excalibur. This uses the
+ * <code>com.informix.jdbcx.IfxConnectionPoolDataSource</code> object. It uses
+ * the following format for configuration (italics mark information you change):
+ *
+ * <pre>
+ * <informix>
+ * <pool-controller init="<i>5</i>" min="<i>5</i>" max="<i>10</i>"/>
+ * <dbname><i>dbname</i></dbname>
+ * <servername><i>servername</i></servername>
+ * <host port="<i>2000</i>"><i>host</i></host>
+ * <user><i>user</i></user>
+ * <password><i>password</i></password>
+ * <tracing>
+ * <jdbc> file="<i>filename</i>" level="<i>level</i>"</jdbc>
+ * <sqli> file="<i>filename</i>" level="<i>level</i>"</sqli>
+ * </tracing>
+ * <informix>
+ * </pre>
+ *
+ * <p>
+ * Informix doesn't like the JdbcDataSource Component, so we gave it it's own.
+ * Do not use this datasource if you are planning on using your J2EE server's
+ * connection pooling.
+ * <p>
+ *
+ * <p>
+ * You must have Informix's JDBC 2.2 or higher jar file, as well as the
+ * extensions jar file (<code>ifxjdbc.jar</code> and <code>ifxjdbcx.jar</code>).
+ * Also, this DataSource requires the Avalon Cadastre package because it uses
+ * the MemoryContext.
+ * </p>
+ *
+ * <p>
+ * The <i>tracing</i> settings optionally enable Informix's tracing support
+ * within the jdbc driver. <strong>Note</strong>, for this to work, the
+ * <code>ifxjdbc-g.jar</code> and <code>ifxjdbcx-g.jar</code> jar files are
+ * required (the options have no effect when using the non -g jar files).
+ * </p>
+ *
+ * <p>
+ * <i>jdbc tracing</i> enables general logging information about the driver
+ * itself. <i>sqli tracing</i> enables logging of native sqli messages sent
+ * between the jdbc driver and the database server.
+ * </p>
+ *
+ * <p>
+ * The attribute <code>file</code> specifies where to write tracing information
+ * to, and <code>level</code> specifies the tracing level to be used, as
+ * documented in the Informix JDBC programmers guide.
+ * </p>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a>
+ * @version CVS $Revision$ $Date$
+ * @since 4.0
+ */
+public class InformixDataSource
+ extends AbstractLogEnabled
+ implements DataSourceComponent
+{
+ private DataSource m_dataSource;
+ private boolean m_autocommit;
+ private static boolean INIT_FACTORY = false;
+
+ /**
+ * Set up the system property for the context factory if it hasn't been
+ * done already. This is not done in a static initializer due to the
+ * existence of the J2eeDataSource.
+ */
+ public InformixDataSource()
+ {
+ if( !InformixDataSource.INIT_FACTORY )
+ {
+ System.setProperty( Context.INITIAL_CONTEXT_FACTORY,
+
"org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory" );
+ }
+ }
+
+ /**
+ * Return an Informix Connection object
+ */
+ public Connection getConnection() throws SQLException
+ {
+ Connection conn = m_dataSource.getConnection();
+
+ if( conn.getAutoCommit() != m_autocommit )
+ {
+ conn.setAutoCommit( m_autocommit );
+ }
+
+ return conn;
+ }
+
+ /**
+ * Set up the Informix driver for direct use.
+ */
+ public void configure( Configuration conf ) throws ConfigurationException
+ {
+ Configuration poolController = conf.getChild( "pool-controller" );
+ String dbname = conf.getChild( "dbname" ).getValue( "ifx" );
+ ConnectionPoolDataSource pooledDataSource = (ConnectionPoolDataSource)
getInstance( "com.informix.jdbcx.IfxConnectionPoolDataSource" );
+ m_autocommit = conf.getChild( "autocommit" ).getValueAsBoolean( true );
+
+ setProperty(pooledDataSource, "IfxCPMInitPoolSize", new
Integer(poolController.getAttributeAsInteger( "init", 5 ) ) );
+ setProperty(pooledDataSource, "IfxCPMMinPoolSize", new
Integer(poolController.getAttributeAsInteger( "min", 5 ) ) );
+ setProperty(pooledDataSource, "IfxCPMMaxPoolSize", new
Integer(poolController.getAttributeAsInteger( "max", 10 ) ) );
+ setProperty(pooledDataSource, "IfxCPMServiceInterval", new Integer( 100 ) );
+ setProperty(pooledDataSource, "ServerName", conf.getChild( "servername"
).getValue() );
+ setProperty(pooledDataSource, "DatabaseName", conf.getChild( "dbname"
).getValue() );
+ setProperty(pooledDataSource, "IfxIFXHOST", conf.getChild( "host"
).getValue() );
+ setProperty(pooledDataSource, "PortNumber", new Integer( conf.getChild(
"host" ).getAttributeAsInteger( "port" ) ) );
+ setProperty(pooledDataSource, "User", conf.getChild( "user" ).getValue() );
+ setProperty(pooledDataSource, "Password", conf.getChild( "password"
).getValue() );
+
+ try
+ {
+ Context context = new InitialContext();
+
+ context.bind( dbname + "pool", pooledDataSource );
+
+ m_dataSource = (DataSource) getInstance(
"com.informix.jdbcx.IfxDataSource" );
+ setProperty(m_dataSource, "DataSourceName", dbname + "pool" );
+ setProperty(m_dataSource, "ServerName", conf.getChild( "servername"
).getValue() );
+ setProperty(m_dataSource, "DatabaseName", conf.getChild( "dbname"
).getValue() );
+ setProperty(m_dataSource, "IfxIFXHOST", conf.getChild( "host"
).getValue() );
+ setProperty(m_dataSource, "PortNumber", new Integer( conf.getChild(
"host" ).getAttributeAsInteger( "port" ) ) );
+ setProperty(m_dataSource, "User", conf.getChild( "user" ).getValue() );
+ setProperty(m_dataSource, "Password", conf.getChild( "password"
).getValue() );
+ configureTracing( conf.getChild( "tracing", false ) );
+
+ context.bind( dbname, m_dataSource );
+ }
+ catch( Exception e )
+ {
+ if( getLogger().isErrorEnabled() )
+ {
+ getLogger().error( "There was an error trying to bind the
connection pool", e );
+ }
+ throw new ConfigurationException( "There was an error trying to bind
the connection pool", e );
+ }
+ }
+
+ /**
+ * Helper method to enable tracing support in the Informix driver.
+ *
+ * @param config a <code>Configuration</code> value
+ * @exception ConfigurationException if an error occurs
+ */
+ private void configureTracing( final Configuration config )
+ throws ConfigurationException
+ {
+ if ( config != null )
+ {
+ Configuration child = config.getChild( "jdbc", false );
+
+ if (child != null)
+ {
+ // enables tracing on the jdbc driver itself
+ setProperty(m_dataSource, "IfxTRACE", new Integer(
child.getAttributeAsInteger( "level" ) ) );
+ setProperty(m_dataSource, "IfxTRACEFILE", child.getAttribute(
"file" ) );
+ }
+
+ child = config.getChild( "sqli", false );
+
+ if (child != null)
+ {
+ // enables sqli message tracing
+ setProperty(m_dataSource, "IfxPROTOCOLTRACE", new Integer(
child.getAttributeAsInteger( "level" ) ) );
+ setProperty(m_dataSource, "IfxPROTOCOLTRACEFILE",
child.getAttribute( "file" ) );
+ }
+ }
+ }
+
+ private Object getInstance(String className)
+ throws ConfigurationException
+ {
+ Object instance = null;
+
+ try
+ {
+ instance =
Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException("Could not load class", e);
+ }
+
+ return instance;
+ }
+
+ private void setProperty(Object obj, String propertyName, Object value)
+ throws ConfigurationException
+ {
+ Class valueClass = value.getClass();
+
+ if ( value instanceof Integer )
+ {
+ valueClass = Integer.TYPE;
+ }
+
+ try
+ {
+ Method meth = obj.getClass().getMethod("set" + propertyName, new
Class[]{valueClass});
+ meth.invoke(obj, new Object[]{value});
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException("Could not set property", e);
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]