|
From: <pat...@us...> - 2009-01-21 20:57:42
|
Revision: 846
http://cishell.svn.sourceforge.net/cishell/?rev=846&view=rev
Author: pataphil
Date: 2009-01-21 20:57:39 +0000 (Wed, 21 Jan 2009)
Log Message:
-----------
Implemented part of the database service, but didn't finish.
Modified Paths:
--------------
trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF
trunk/core/org.cishell.reference.service.database/OSGI-INF/component.xml
trunk/core/org.cishell.reference.service.database/OSGI-INF/metatype/METADATA.XML
trunk/core/org.cishell.reference.service.database/build.properties
trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java
Modified: trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF
===================================================================
--- trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF 2009-01-21 20:56:31 UTC (rev 845)
+++ trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF 2009-01-21 20:57:39 UTC (rev 846)
@@ -3,13 +3,14 @@
Bundle-Name: Database Plug-in
Bundle-SymbolicName: org.cishell.reference.service.database
Bundle-Version: 1.0.0
-Bundle-Activator: org.cishell.reference.service.database.DatabaseServiceImpl
-Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: J2SE-1.4
+X-AutoStart: true
Import-Package: org.apache.commons.dbcp,
org.apache.commons.pool,
org.apache.commons.pool.impl,
+ org.cishell.framework.algorithm;version="1.0.0",
org.cishell.service.database,
org.osgi.framework;version="1.4.0",
org.osgi.service.component;version="1.0.0",
org.osgi.service.log;version="1.3.0"
+Service-Component: OSGI-INF/component.xml
Modified: trunk/core/org.cishell.reference.service.database/OSGI-INF/component.xml
===================================================================
--- trunk/core/org.cishell.reference.service.database/OSGI-INF/component.xml 2009-01-21 20:56:31 UTC (rev 845)
+++ trunk/core/org.cishell.reference.service.database/OSGI-INF/component.xml 2009-01-21 20:57:39 UTC (rev 846)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="org.cishell.reference.service.database.DatabaseService.component" immediate="true">
- <implementation class="org.cishell.reference.service.database.DatabaseServiceImpl.component"/>
+ <implementation class="org.cishell.reference.service.database.DatabaseServiceImpl"/>
<properties entry="OSGI-INF/service.properties"/>
<reference name="LOG" interface="org.osgi.service.log.LogService"/>
@@ -8,7 +8,4 @@
<provide interface=
"org.cishell.service.database.DatabaseService"/>
</service>
-</component>
-
-
-
\ No newline at end of file
+</component>
\ No newline at end of file
Modified: trunk/core/org.cishell.reference.service.database/OSGI-INF/metatype/METADATA.XML
===================================================================
--- trunk/core/org.cishell.reference.service.database/OSGI-INF/metatype/METADATA.XML 2009-01-21 20:56:31 UTC (rev 845)
+++ trunk/core/org.cishell.reference.service.database/OSGI-INF/metatype/METADATA.XML 2009-01-21 20:57:39 UTC (rev 846)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0">
- <Designate pid="org.cishell.reference.prefs.admin.PrefAdmin">
- <Object ocdref="org.cishell.reference.prefs.admin.PrefAdmin.OCD" />
+ <Designate pid="org.cishell.reference.service.database.DatabaseServiceImpl">
+ <Object ocdref="org.cishell.reference.service.database.DatabaseServiceImpl.OCD" />
</Designate>
</metatype:MetaData>
Modified: trunk/core/org.cishell.reference.service.database/build.properties
===================================================================
--- trunk/core/org.cishell.reference.service.database/build.properties 2009-01-21 20:56:31 UTC (rev 845)
+++ trunk/core/org.cishell.reference.service.database/build.properties 2009-01-21 20:57:39 UTC (rev 846)
@@ -1,4 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
- .
+ .,\
+ OSGI-INF/
Modified: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java
===================================================================
--- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java 2009-01-21 20:56:31 UTC (rev 845)
+++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java 2009-01-21 20:57:39 UTC (rev 846)
@@ -2,10 +2,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.Dictionary;
-import javax.sql.DataSource;
-
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
@@ -13,89 +10,123 @@
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
+import org.cishell.framework.algorithm.AlgorithmExecutionException;
+import org.cishell.service.database.DataSourceWithID;
import org.cishell.service.database.DatabaseCopyException;
import org.cishell.service.database.DatabaseCreationException;
import org.cishell.service.database.DatabaseService;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.log.LogService;
+
public class DatabaseServiceImpl implements DatabaseService {
-
- //TODO: These variables should be abstracted out in a Preferences page at some point (I guess)
- private static final String DEFAULT_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
- private static final String DEFAULT_CONNECTION_STRING_PREFIX = "jdbc:derby";
- private static final String DEFAULT_CONNECTION_STRING_SUFFIX = ";create=true";
+ /* TODO: These variables should be abstracted out in a Preferences page at some
+ point (I guess).
+ */
+ private static final String DEFAULT_DRIVER_NAME =
+ "org.apache.derby.jdbc.EmbeddedDriver";
+ private static final String DEFAULT_PROTOCOL = "jdbc:derby:";
+ private static final String DEFAULT_CREATE_CONNECTION_STRING = ";create=true";
+ private static final String DEFAULT_DB_NAME = "ultra_sasquatch";
- private static final String DEFAULT_DB_NAME = "ultra_sasquatch";
- //Give each db a unique (but meaningless) name by counting up from 0 for each new database
- //TODO: Using a rolling counter like this may have bad implications later on, but it's decent for now
+ // Give each db a unique (but meaningless) name by counting up from 0 for each
+ // new database.
+ /* TODO: Using a rolling counter like this may have bad implications later on,
+ but it's decent for now.
+ */
private static int dbNameCounter = 0;
- private LogService log;
+ private static PoolingDataSource poolingDataSource = null;
+ private String driver;
+ private LogService logger;
+
protected void activate(ComponentContext ctxt) {
-
- this.log = (LogService) ctxt.locateService("LOG");
+ this.driver = DEFAULT_DRIVER_NAME;
+ this.logger = (LogService)ctxt.locateService("LOG");
}
protected void deactivate(ComponentContext ctxt) {
}
- private void ensureConnectionPoolIsEstablished() {
- try {
- Class.forName(DEFAULT_DRIVER_NAME);
+ // If one hasn't been created yet, create a connection pool and return it.
+ private PoolingDataSource getConnectionPool() throws AlgorithmExecutionException
+ {
+ if (poolingDataSource != null)
+ return poolingDataSource;
- String newDatabaseName = DEFAULT_DB_NAME + Integer.toString(dbNameCounter);
- String newDatabaseConnectionURL = DEFAULT_CONNECTION_STRING_PREFIX +
- newDatabaseName +
- DEFAULT_CONNECTION_STRING_SUFFIX;
-
- ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(newDatabaseConnectionURL, null, null);
- GenericObjectPool connectionPool = new GenericObjectPool();
- } catch (ClassNotFoundException e) {
- throw new AlgorithmExecutionException("Database driver not found: " + driver, e);
- }
- }
-
- public DataSource createDatabase() throws DatabaseCreationException {
-
- javax.sql.DataSource dataSource;
-
try {
+ // This loads the database driver.
Class.forName(DEFAULT_DRIVER_NAME);
- //Driver jdbcDriver = (Driver) Class.forName(driver).newInstance(); //we can use this later to check acceptsUrl for better error reporting
- String newDatabaseName = DEFAULT_DB_NAME + Integer.toString(dbNameCounter);
- String newDatabaseConnectionURL = DEFAULT_CONNECTION_STRING_PREFIX +
+ // We can use this later to check acceptsUrl for better error reporting.
+ // Driver jdbcDriver = (Driver) Class.forName(driver).newInstance();
+
+ String newDatabaseName =
+ DEFAULT_DB_NAME + Integer.toString(dbNameCounter);
+ String newDatabaseConnectionURL = DEFAULT_PROTOCOL +
newDatabaseName +
- DEFAULT_CONNECTION_STRING_SUFFIX;
+ DEFAULT_CREATE_CONNECTION_STRING;
- ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(newDatabaseConnectionURL, null, null);
- GenericObjectPool connectionPool = new GenericObjectPool();
- dataSource = new PoolingDataSource(connectionPool);
+ // This connection factory actually uses the loaded database driver to
+ // generate connections.
+ ConnectionFactory connectionFactory = new DriverManagerConnectionFactory
+ (newDatabaseConnectionURL, null, null);
+
+ // This is a generic object pool. It must be linked to a poolable
+ // object factory (PoolableObjectFactory), which the new
+ // PoolableConnectionFactory below is.
+ GenericObjectPool connectionPool = new GenericObjectPool();
- try {
- dataSource.getConnection().close();
- } catch (SQLException e) {
- logger.log(LogService.LOG_WARNING, "Problem opening test connection.", e);
- }
+ // Not sure what this does?
+ KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null);
- //prefuseConnection = prefuse.data.io.sql.ConnectionFactory.getDatabaseConnection(connection);
+ // This is a poolable object factory (PoolableObjectFactory) used to
+ // create connections for an object pool. It is the glue between the
+ // connection factory and the object pool.
+ // It links itself up to the connect pool inside its constructor, which
+ // is why it's not assigned to a variable out here.
+ new PoolableConnectionFactory(connectionFactory, connectionPool, stmtPool, null, false, true);
+ // Finally, create the connection pool.
+ poolingDataSource = new PoolingDataSource(connectionPool);
-
- } catch (ClassNotFoundException e) {
- throw new AlgorithmExecutionException("Database driver not found: " + driver, e);
+ // TODO: Remove this?
+ // Make sure we can get a connection from the connection pool.
+ try {
+ poolingDataSource.getConnection().close();
+ }
+ catch (SQLException e) {
+ this.logger.log(LogService.LOG_WARNING,
+ "Problem opening test connection.",
+ e);
+
+ throw new AlgorithmExecutionException
+ ("Could not properly initiate database.", e);
+ }
}
+ catch (ClassNotFoundException e) {
+ throw new AlgorithmExecutionException
+ ("Database driver (" + driver + ") could not be found", e);
+ }
+
+ return poolingDataSource;
}
+
+ public DataSourceWithID createDatabase() throws DatabaseCreationException {
+ DataSourceWithID dataSource = null;
+
+ return dataSource;
+ }
- public DataSource copyDatabase(DataSource database) throws DatabaseCopyException {
- // TODO Auto-generated method stub
+ public DataSourceWithID copyDatabase(DataSourceWithID database)
+ throws DatabaseCopyException {
+ // TODO Auto-generated method stub.
return null;
}
- public DataSource createDatabase(ResultSet resultSet) throws DatabaseCreationException {
+ public DataSourceWithID createDatabase(ResultSet resultSet)
+ throws DatabaseCreationException {
// TODO Auto-generated method stub
return null;
}
-
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|