|
From: <tr...@us...> - 2003-08-15 00:50:27
|
Update of /cvsroot/babeldoc/babeldoc/modules/sql/src/com/babeldoc/sql/resource
In directory sc8-pr-cvs1:/tmp/cvs-serv6089/sql/src/com/babeldoc/sql/resource
Modified Files:
PooledJdbc.java SimpleJdbc.java
Log Message:
Resources now are Configurable. Added dbDriver configuration option to sql resources. Lots of work on the resources - made things more logical from a SW POV. Added Null scanner.
Index: PooledJdbc.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/sql/src/com/babeldoc/sql/resource/PooledJdbc.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** PooledJdbc.java 14 Jul 2003 17:02:36 -0000 1.4
--- PooledJdbc.java 15 Aug 2003 00:25:30 -0000 1.5
***************
*** 66,71 ****
package com.babeldoc.sql.resource;
- import com.babeldoc.core.resource.Resource;
import com.babeldoc.core.resource.ResourceException;
import org.apache.commons.dbcp.ConnectionFactory;
--- 66,71 ----
package com.babeldoc.sql.resource;
import com.babeldoc.core.resource.ResourceException;
+ import com.babeldoc.core.option.IConfigInfo;
import org.apache.commons.dbcp.ConnectionFactory;
***************
*** 76,82 ****
import org.apache.commons.pool.impl.GenericObjectPool;
- import java.sql.Connection;
- import java.sql.DriverManager;
-
/**
--- 76,79 ----
***************
*** 88,174 ****
* @version 1.0
*/
! public class PooledJdbc extends Resource {
/** the connection pool stuff. */
private static PoolingDriver driver = new PoolingDriver();
- /** the user and database url stuff. */
- private static String DB_USER = "dbUser";
- private static String DB_PASSWD = "dbPassword";
- private static String DB_URL = "dbUrl";
-
- /** Jdbc variables */
- private String dbName;
- private String dbPasswd;
- private String dbUrl;
-
/**
! * Checkin this connection
*
! * @param connection
*
! * @throws ResourceException
*/
! public void checkIn(Object connection) throws ResourceException {
! try {
! ((Connection) connection).close();
! } catch (Exception e) {
! throw new ResourceException("[ResourceException.checkIn]", e);
! }
}
/**
! * Get a new connection.
! *
! * @return the connection
*
! * @throws ResourceException
*/
! public Object checkOut() throws ResourceException {
try {
! synchronized(this) {
! // if we have no dbUrl, setup jdbc NOW
! if (dbUrl == null) {
! setupJdbc();
! }
! }
!
! return DriverManager.getConnection(dbUrl, dbName, dbPasswd);
} catch (Exception e) {
! throw new ResourceException("[ResourceException.checkOut]", e);
}
}
/**
! * TODO: DOCUMENT ME!
! *
! * @param connectURI DOCUMENT ME!
*
! * @throws Exception DOCUMENT ME!
*/
! public void setupDriver(String connectURI) throws Exception {
! ObjectPool connectionPool = new GenericObjectPool(null);
! ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,
! dbName, dbPasswd);
! PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
! connectionPool, null, null, false, true);
! driver.registerPool(getName(), connectionPool);
!
! // convert to the url that talks to the connection pool
! dbUrl = "jdbc:apache:commons:dbcp:" + getName();
! }
! /**
! * Setup the jdbc stuff
! *
! * @throws Exception DOCUMENT ME!
! */
! private void setupJdbc() throws Exception {
! try {
! dbName = getConfig().getString(DB_USER);
! dbPasswd = getConfig().getString(DB_PASSWD);
! setupDriver(getConfig().getString(DB_URL));
! } catch (Exception x) {
! com.babeldoc.core.LogService.getInstance().logError(x);
}
}
}
--- 85,154 ----
* @version 1.0
*/
! public class PooledJdbc extends SimpleJdbc {
/** the connection pool stuff. */
private static PoolingDriver driver = new PoolingDriver();
/**
! * Now setup the connection pool - this means rewriting the URL to point instead
! * to the apache connection pool
*
! * @param connectURI the connection uri to the underlying Jdbc driver
*
! * @throws Exception Oh, anything really!!!
*/
! protected void setupConnectionPool(String connectURI) throws Exception {
! ObjectPool connectionPool = new GenericObjectPool(null);
! ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,
! getDbName(), getDbPasswd());
! PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
! connectionPool, null, null, false, true);
! driver.registerPool(getName(), connectionPool);
!
! // toXml to the url that talks to the connection pool
! setDbUrl("jdbc:apache:commons:dbcp:" + getName());
}
/**
! * Setup the jdbc stuff
*
! * @throws ResourceException DOCUMENT ME!
*/
! protected void setup() throws ResourceException {
! super.setup();
try {
! setupConnectionPool(getDbUrl());
} catch (Exception e) {
! throw new ResourceException("", e);
}
}
/**
! * Get the configuration iformation for this class
*
! * @return IConfigInfo object
*/
! public IConfigInfo getInfo() {
! if(info==null) {
! info = new SimpleJdbcConfigInfo() {
! /**
! * Return description of this worker
! *
! * @return description
! */
! public String getDescription() {
! return "Pooled jdbc connection resource. CheckIn and Checkouts operate on a pool of connections.";
! }
! /**
! * return the name
! *
! * @return
! */
! public String getName() {
! return "pooled";
! }
! };
}
+ return info;
}
}
Index: SimpleJdbc.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/sql/src/com/babeldoc/sql/resource/SimpleJdbc.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** SimpleJdbc.java 27 Jun 2003 02:05:59 -0000 1.3
--- SimpleJdbc.java 15 Aug 2003 00:25:30 -0000 1.4
***************
*** 68,78 ****
import com.babeldoc.core.resource.Resource;
import com.babeldoc.core.resource.ResourceException;
import java.sql.Connection;
import java.sql.DriverManager;
/**
! * How to get connections using the simplest jdbc code, etc. This merely
* creates new connections every time the resource is checked out. Not too
* good especially for drives / databases that have a limited number of
--- 68,84 ----
import com.babeldoc.core.resource.Resource;
import com.babeldoc.core.resource.ResourceException;
+ import com.babeldoc.core.resource.ResourceConfigInfo;
+ import com.babeldoc.core.option.IConfigInfo;
+ import com.babeldoc.core.option.ConfigOption;
+ import com.babeldoc.core.option.IConfigOptionType;
import java.sql.Connection;
import java.sql.DriverManager;
+ import java.util.Collection;
+ import java.util.ArrayList;
/**
! * How to getChild connections using the simplest jdbc code, etc. This merely
* creates new connections every time the resource is checked out. Not too
* good especially for drives / databases that have a limited number of
***************
*** 82,96 ****
* @version 1.0
*/
! public class SimpleJdbc extends Resource {
public static final String DB_USER = "dbUser";
public static final String DB_PASSWD = "dbPassword";
public static final String DB_DRIVER = "dbDriver";
public static final String DB_URL = "dbUrl";
- private String dbDriver;
/** Jdbc variables */
! private String dbName;
! private String dbPasswd;
! private String dbUrl;
/**
--- 88,105 ----
* @version 1.0
*/
! public class SimpleJdbc
! extends Resource {
public static final String DB_USER = "dbUser";
public static final String DB_PASSWD = "dbPassword";
public static final String DB_DRIVER = "dbDriver";
public static final String DB_URL = "dbUrl";
/** Jdbc variables */
! private String dbName = null;
! private String dbPasswd = null;
! private String dbUrl = null;
! private String dbDriver = null;
!
! protected IConfigInfo info = null;
/**
***************
*** 110,114 ****
/**
! * Get a new connection.
*
* @return the connection
--- 119,124 ----
/**
! * Get a new connection. This method calls the super checkOut which
! * does resouce initialization checking.
*
* @return the connection
***************
*** 117,127 ****
*/
public synchronized Object checkOut() throws ResourceException {
! try {
! // if we have no dbUrl, setup jdbc NOW
! if (dbUrl == null) {
! setupJdbc();
! }
! return DriverManager.getConnection(dbUrl, dbName, dbPasswd);
} catch (Exception e) {
throw new ResourceException("[ResourceException.checkOut]", e);
--- 127,134 ----
*/
public synchronized Object checkOut() throws ResourceException {
! super.checkOut();
! try {
! return DriverManager.getConnection(getDbUrl(), getDbName(), getDbPasswd());
} catch (Exception e) {
throw new ResourceException("[ResourceException.checkOut]", e);
***************
*** 130,150 ****
/**
! * Setup the jdbc stuff
*/
! private void setupJdbc() {
! try {
! dbName = getConfig().getString(DB_USER);
! dbPasswd = getConfig().getString(DB_PASSWD);
! dbDriver = getConfig().getString(DB_DRIVER);
! dbUrl = getConfig().getString(DB_URL);
!
! if ((dbName != null) && (dbPasswd != null) && (dbDriver != null) &&
! (dbUrl != null)) {
! Class.forName(dbDriver).newInstance();
}
! } catch (Exception x) {
! com.babeldoc.core.LogService.getInstance().logError(x);
! dbUrl = null;
}
}
}
--- 137,298 ----
/**
! * Setup the jdbc stuff. This largely ensures that all the four components of the
! * sql resource are provided and that the driver class
! *
! * @throws ResourceException
*/
! protected void setup() throws ResourceException {
! if ((getDbName() != null) &&
! (getDbPasswd() != null) &&
! (getDbDriver() != null) &&
! (getDbUrl() != null)) {
! try {
! Class.forName(getDbDriver()).newInstance();
! } catch (Exception x) {
! setDbUrl(null);
! throw new ResourceException("Jdbc driver exception: ", x);
}
! } else {
! throw new ResourceException("The resource is not COMPLETELY specified");
! }
! }
!
! /**
! * Get the db user name - get from the config if not provided
! *
! * @return
! */
! protected String getDbName() {
! if(dbName==null) {
! dbName = getInfo().getStrValue(DB_USER);
! }
! return dbName;
! }
!
! /**
! * Set the db username
! *
! * @param dbName
! */
! protected void setDbName(String dbName) {
! this.dbName = dbName;
! }
!
! /**
! * get the db password
! *
! * @return
! */
! protected String getDbPasswd() {
! if(dbPasswd==null) {
! dbPasswd = getInfo().getStrValue(DB_PASSWD);
! }
! return dbPasswd;
! }
!
! /**
! * Set the db password
! *
! * @param dbPasswd
! */
! protected void setDbPasswd(String dbPasswd) {
! this.dbPasswd = dbPasswd;
! }
!
! /**
! * get the db url
! *
! * @return
! */
! protected String getDbUrl() {
! if(dbUrl==null) {
! dbUrl=getInfo().getStrValue(DB_URL);
}
+ return dbUrl;
+ }
+
+ /**
+ * Set the db url
+ *
+ * @param dbUrl
+ */
+ protected void setDbUrl(String dbUrl) {
+ this.dbUrl = dbUrl;
+ }
+
+ /**
+ * Get the db driver
+ *
+ * @return
+ */
+ protected String getDbDriver() {
+ if(dbDriver==null) {
+ dbDriver=getInfo().getStrValue(DB_DRIVER);
+ }
+ return dbDriver;
+ }
+
+ /**
+ * set the db driver
+ *
+ * @param dbDriver
+ */
+ protected void setDbDriver(String dbDriver) {
+ this.dbDriver = dbDriver;
+ }
+
+ /**
+ * Get the configuration iformation for this class
+ *
+ * @return IConfigInfo object
+ */
+ public IConfigInfo getInfo() {
+ if(info==null) {
+ info = new SimpleJdbcConfigInfo();
+ }
+ return info;
+ }
+ }
+
+
+ /**
+ * Manage the configuration options for resources.
+ */
+ class SimpleJdbcConfigInfo
+ extends ResourceConfigInfo{
+ /**
+ * This method returns type specific options
+ *
+ * @return comments
+ */
+ public Collection getTypeSpecificOptions() {
+ Collection options = new ArrayList();
+ options.add(new ConfigOption(SimpleJdbc.DB_USER, IConfigOptionType.STRING,
+ null, true, "Database user to logon with"));
+ options.add(new ConfigOption(SimpleJdbc.DB_PASSWD, IConfigOptionType.STRING,
+ null, true, "Database user password to logon with"));
+ options.add(new ConfigOption(SimpleJdbc.DB_DRIVER, IConfigOptionType.STRING,
+ null, true, "JDBC Driver class to manage the connection to your database. See your vendors documentation. This has to be in the classpath"));
+ options.add(new ConfigOption(SimpleJdbc.DB_URL, IConfigOptionType.STRING,
+ null, true, "The JDBC connection string. This generally contains the database name, etc. See your vendors documentation."));
+ return options;
+ }
+
+ /**
+ * Return description of this worker
+ *
+ * @return description
+ */
+ public String getDescription() {
+ return "Simple, non-pooled jdbc connection resource. CheckIn and Checkouts create and destroy connections";
+ }
+
+ /**
+ * return the name
+ *
+ * @return
+ */
+ public String getName() {
+ return "jdbc";
}
}
|