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. |
From: <pat...@us...> - 2009-01-21 22:22:12
|
Revision: 848 http://cishell.svn.sourceforge.net/cishell/?rev=848&view=rev Author: pataphil Date: 2009-01-21 21:57:08 +0000 (Wed, 21 Jan 2009) Log Message: ----------- Changed starting mechanism from immedate activatation to bundle starting. 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/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 21:09:43 UTC (rev 847) +++ trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF 2009-01-21 21:57:08 UTC (rev 848) @@ -4,10 +4,12 @@ Bundle-SymbolicName: org.cishell.reference.service.database Bundle-Version: 1.0.0 Bundle-RequiredExecutionEnvironment: J2SE-1.4 +Bundle-Activator: org.cishell.reference.service.database.DatabaseServiceImpl X-AutoStart: true Import-Package: org.apache.commons.dbcp, org.apache.commons.pool, org.apache.commons.pool.impl, + org.apache.derby, org.cishell.framework.algorithm;version="1.0.0", org.cishell.service.database, org.osgi.framework;version="1.4.0", 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 21:09:43 UTC (rev 847) +++ trunk/core/org.cishell.reference.service.database/OSGI-INF/component.xml 2009-01-21 21:57:08 UTC (rev 848) @@ -1,8 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> -<component name="org.cishell.reference.service.database.DatabaseService.component" immediate="true"> +<component name="org.cishell.reference.service.database.DatabaseService.component" immediate="false"> <implementation class="org.cishell.reference.service.database.DatabaseServiceImpl"/> <properties entry="OSGI-INF/service.properties"/> - <reference name="LOG" interface="org.osgi.service.log.LogService"/> <service> <provide interface= 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 21:09:43 UTC (rev 847) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java 2009-01-21 21:57:08 UTC (rev 848) @@ -1,7 +1,7 @@ package org.cishell.reference.service.database; -import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Hashtable; import org.apache.commons.dbcp.ConnectionFactory; import org.apache.commons.dbcp.DriverManagerConnectionFactory; @@ -10,15 +10,15 @@ 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.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.log.LogService; -public class DatabaseServiceImpl implements DatabaseService { +public class DatabaseServiceImpl implements DatabaseService, BundleActivator { /* TODO: These variables should be abstracted out in a Preferences page at some point (I guess). */ @@ -35,26 +35,46 @@ */ private static int dbNameCounter = 0; - private static PoolingDataSource poolingDataSource = null; + private ServiceRegistration databaseServiceRegistration; + private PoolingDataSource poolingDataSource = null; + // TODO: Needed? I just want to make sure nothing goes wrong for now. + private DataSourceWithID myDataSource = null; + private String driver; - private LogService logger; - protected void activate(ComponentContext ctxt) { + public void start(BundleContext context) throws Exception { this.driver = DEFAULT_DRIVER_NAME; - this.logger = (LogService)ctxt.locateService("LOG"); + + System.err.println("starting!"); + + // Register me as a service! (This doesn't work?) + databaseServiceRegistration = context.registerService + (DatabaseService.class.getName(), this, new Hashtable()); + + // Get MY data source! It's mine, and you can't have it! + try { + myDataSource = createDatabase(); + } + catch (DatabaseCreationException e) { + System.err.println(":'( " + e.getMessage()); + throw e; + } + + System.err.println("meep?"); } - protected void deactivate(ComponentContext ctxt) { + public void stop(BundleContext context) throws Exception { } // If one hasn't been created yet, create a connection pool and return it. - private PoolingDataSource getConnectionPool() throws AlgorithmExecutionException + private PoolingDataSource getConnectionPool() throws DatabaseCreationException { if (poolingDataSource != null) return poolingDataSource; try { + System.err.println("Loading driver"); // This loads the database driver. Class.forName(DEFAULT_DRIVER_NAME); @@ -67,6 +87,8 @@ newDatabaseName + DEFAULT_CREATE_CONNECTION_STRING; + System.err.println("connection url: " + newDatabaseConnectionURL); + // This connection factory actually uses the loaded database driver to // generate connections. ConnectionFactory connectionFactory = new DriverManagerConnectionFactory @@ -96,16 +118,12 @@ poolingDataSource.getConnection().close(); } catch (SQLException e) { - this.logger.log(LogService.LOG_WARNING, - "Problem opening test connection.", - e); - - throw new AlgorithmExecutionException + throw new DatabaseCreationException ("Could not properly initiate database.", e); } } catch (ClassNotFoundException e) { - throw new AlgorithmExecutionException + throw new DatabaseCreationException ("Database driver (" + driver + ") could not be found", e); } @@ -113,20 +131,6 @@ } public DataSourceWithID createDatabase() throws DatabaseCreationException { - DataSourceWithID dataSource = null; - - return dataSource; + return new DataSourceWithID(getConnectionPool()); } - - public DataSourceWithID copyDatabase(DataSourceWithID database) - throws DatabaseCopyException { - // TODO Auto-generated method stub. - return null; - } - - 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. |
From: <mwl...@us...> - 2009-01-26 18:31:16
|
Revision: 856 http://cishell.svn.sourceforge.net/cishell/?rev=856&view=rev Author: mwlinnem Date: 2009-01-26 18:31:07 +0000 (Mon, 26 Jan 2009) Log Message: ----------- Major code cleanup. Improved error handling. Modified Paths: -------------- trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF Added Paths: ----------- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java Removed Paths: ------------- 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-24 19:34:25 UTC (rev 855) +++ trunk/core/org.cishell.reference.service.database/META-INF/MANIFEST.MF 2009-01-26 18:31:07 UTC (rev 856) @@ -4,7 +4,7 @@ Bundle-SymbolicName: org.cishell.reference.service.database Bundle-Version: 1.0.0 Bundle-RequiredExecutionEnvironment: J2SE-1.4 -Bundle-Activator: org.cishell.reference.service.database.DatabaseServiceImpl +Bundle-Activator: org.cishell.reference.service.database.DerbyDatabaseService X-AutoStart: true Import-Package: org.apache.commons.dbcp, org.apache.commons.pool, Deleted: 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-24 19:34:25 UTC (rev 855) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DatabaseServiceImpl.java 2009-01-26 18:31:07 UTC (rev 856) @@ -1,183 +0,0 @@ -package org.cishell.reference.service.database; - -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Hashtable; - -import javax.sql.DataSource; - -import org.apache.commons.dbcp.ConnectionFactory; -import org.apache.commons.dbcp.DriverManagerConnectionFactory; -import org.apache.commons.dbcp.PoolableConnectionFactory; -import org.apache.commons.dbcp.PoolingDataSource; -import org.apache.commons.pool.KeyedObjectPoolFactory; -import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.cishell.service.database.DataSourceWithID; -import org.cishell.service.database.DatabaseCreationException; -import org.cishell.service.database.DatabaseService; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; - - - -public class DatabaseServiceImpl implements DatabaseService, BundleActivator { - /* 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"; - - //db name must be unique per running instance of the same NWB installation, otherwise they will overlap - private static int dbNameCounter = 0; - - private ServiceRegistration databaseServiceRegistration; - - private PoolingDataSource poolingDataSource = null; - // TODO: Needed? I just want to make sure nothing goes wrong for now. - private DataSourceWithID myDataSource = null; - - private String driver; - - public void start(BundleContext context) throws Exception { - this.driver = DEFAULT_DRIVER_NAME; - - System.err.println("starting!"); - - // Register me as a service! (This doesn't work?) - databaseServiceRegistration = context.registerService - (DatabaseService.class.getName(), this, new Hashtable()); - - ServiceReference ref = context.getServiceReference(DatabaseService.class.getName()); - if (ref == null) { - System.out.println("REEEEEEEEEEEEEEEEEEEEEEEFFFFFFF IS NUUUUUUUULLLLLLL!!@!(@!(!)!"); - } - DatabaseService dbService = (DatabaseService) context.getService(ref); - if (dbService == null) { - System.out.println("DEEEBEESEERVICE IS NUUUUL!L!@L!@!@!@@!@!@L..1"); - } else { - - } - - // Get MY data source! It's mine, and you can't have it! - try { - myDataSource = createDatabase(); - } - catch (DatabaseCreationException e) { - System.err.println(":'( " + e.getMessage()); - throw e; - } - - System.err.println("meep?"); - cleanOutDatabaseTables(); - } - - private void cleanOutDatabaseTables() throws Exception { - //TODO: We need to be cleaning up and starting the DB correctly - //TODO: Clean this up - DataSource ds = getDataSource(); - Connection c = ds.getConnection(); - DatabaseMetaData dmd = c.getMetaData(); - ResultSet tableNames = dmd.getTables(null, null, null, null); - while (tableNames.next()) { - for (int ii = 1; ii <= tableNames.getMetaData().getColumnCount(); ii++) { - String tableContents = tableNames.getString(ii); - System.out.print(tableContents + ", "); - } - if (tableNames.getString(2).indexOf("APP") != -1) { - System.out.println("MAGOOT!"); - Statement s = c.createStatement(); - System.out.println(tableNames.getString(3)); - System.out.println(s.executeUpdate("DROP TABLE APP." + tableNames.getString(3))); - - } - System.out.println(""); - } - } - - public void stop(BundleContext context) throws Exception { - cleanOutDatabaseTables(); - try { - DriverManager.getConnection("jdbc:derby:;shutdown=true"); - } catch (Exception e) { - - } - } - - // If one hasn't been created yet, create a connection pool and return it. - private PoolingDataSource getDataSource() throws DatabaseCreationException - { - if (poolingDataSource != null) - return poolingDataSource; - - try { - System.err.println("Loading driver"); - // This loads the database driver. - Class.forName(DEFAULT_DRIVER_NAME); - - // We can use this later to check acceptsUrl for better error reporting. - // Driver jdbcDriver = (Driver) Class.forName(driver).newInstance(); - - //TODO: It may exist from before, actually. Fix this. - String newDatabaseName = - DEFAULT_DB_NAME; - String newDatabaseConnectionURL = DEFAULT_PROTOCOL + - newDatabaseName + - DEFAULT_CREATE_CONNECTION_STRING; - - System.err.println("connection url: " + newDatabaseConnectionURL); - - // 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(); - - // Not sure what this does? - KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null); - - // 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); - - // TODO: Remove this? - // Make sure we can get a connection from the connection pool. - try { - poolingDataSource.getConnection().close(); - } - catch (SQLException e) { - throw new DatabaseCreationException - ("Could not properly initiate database.", e); - } - } - catch (ClassNotFoundException e) { - throw new DatabaseCreationException - ("Database driver (" + driver + ") could not be found", e); - } - - return poolingDataSource; - } - - public DataSourceWithID createDatabase() throws DatabaseCreationException { - return new DataSourceWithIDImpl(getDataSource()); - } -} Copied: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java (from rev 853, 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/DerbyDatabaseService.java (rev 0) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java 2009-01-26 18:31:07 UTC (rev 856) @@ -0,0 +1,163 @@ +package org.cishell.reference.service.database; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Hashtable; + +import javax.sql.DataSource; + +import org.apache.commons.dbcp.ConnectionFactory; +import org.apache.commons.dbcp.DriverManagerConnectionFactory; +import org.apache.commons.dbcp.PoolableConnectionFactory; +import org.apache.commons.dbcp.PoolingDataSource; +import org.apache.commons.pool.KeyedObjectPoolFactory; +import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; +import org.apache.commons.pool.impl.GenericObjectPool; +import org.cishell.service.database.DataSourceWithID; +import org.cishell.service.database.DatabaseCreationException; +import org.cishell.service.database.DatabaseService; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; + + + +public class DerbyDatabaseService implements DatabaseService, BundleActivator { + + /* + * Variables used to connect to the internal derby database + * + * TODO: Eventually support connecting databases other than derby, + * and connecting to databases other than our internal one + * (maybe not by modifying this class directly though). + */ + private static final String DERBY_DRIVER_NAME = + "org.apache.derby.jdbc.EmbeddedDriver"; + private static final String DERBY_PROTOCOL = "jdbc:derby:"; + private static final String DEFAULT_CREATE_CONNECTION_STRING = ";create=true"; + private static final String DEFAULT_DB_NAME = "cishell_database"; + + //where the database exists on the filesystem (relative to the application root directory) + private static final String DATABASE_DIRECTORY = "database/"; + + //(new connections to the database will come from a pool of connections, improving performance) + private PoolingDataSource poolingDataSource = null; + + //hold on to our service registration so we can unregister when this plugin stops. + private ServiceRegistration databaseServiceRegistration; + + public void start(BundleContext context) throws Exception { + + /* + * Tell Derby to look for an existing database or create a new database + * in the default directory (within our application's directory) + */ + System.setProperty("derby.system.home", DATABASE_DIRECTORY); + + /* + * Make sure we don't have any leftover cruft in the db from previous sessions. + * This is our second chance, if the database wasn't properly cleaned out + * in the stop() method on application exit. + * TODO: If we ever implement some kind of persistence we may not want to do this so bluntly + */ + removeAllNonSystemDatabaseTables(); + + //allow the database service to be found by other services/plugins + databaseServiceRegistration = context.registerService + (DatabaseService.class.getName(), this, new Hashtable()); + + } + + public void stop(BundleContext context) { + //disallow the database service to be found by other services/plugins + this.databaseServiceRegistration.unregister(); + + //try to clean out the database and shut it down. + try { + removeAllNonSystemDatabaseTables(); + DriverManager.getConnection("jdbc:derby:;shutdown=true"); + } catch (Exception e) { + String message = + "An unexpected exception occurred while shutting down the internal database." + + "Aborting database shutdown process." + + "Database may not be left in a valid state (but it will probably be okay)."; + throw new RuntimeException(message, e); + } + } + + //Return a link to a "database" that cannot interfere with other "databases" in the system. + //TODO: May need to refactor to improve the terminology used surrounding this functionality. + public DataSourceWithID createDatabase() throws DatabaseCreationException { + return new DataSourceWithIDImpl(getDataSource()); + } + + + private static final int SCHEMA_NAME_INDEX = 2; + private static final int TABLE_NAME_INDEX = 3; + private static final String NONSYSTEM_SCHEMA_NAME = "APP"; + + private void removeAllNonSystemDatabaseTables() throws Exception { + + DataSource db = getDataSource(); + Connection dbConnection = db.getConnection(); + DatabaseMetaData dbMetadata = dbConnection.getMetaData(); + ResultSet allTableNames = dbMetadata.getTables(null, null, null, null); + + Statement removeTables = dbConnection.createStatement(); + + while (allTableNames.next()) { + if (allTableNames.getString(SCHEMA_NAME_INDEX).indexOf(NONSYSTEM_SCHEMA_NAME) != -1) { + String removeTableSQL = + "DROP TABLE " + + NONSYSTEM_SCHEMA_NAME + "." + allTableNames.getString(TABLE_NAME_INDEX); + removeTables.addBatch(removeTableSQL); + } + } + + removeTables.executeBatch(); + } + + //TODO: It could be that we should give everyone different datasources instead of the same one + private PoolingDataSource getDataSource() throws DatabaseCreationException + { + if (this.poolingDataSource == null) { initializePoolingDataSource(); }; + return poolingDataSource; + } + + //lazy-load the pooling data source + private void initializePoolingDataSource() throws DatabaseCreationException { + if (this.poolingDataSource != null) { + return; + } + + try { + //Load the database driver + Class.forName(DERBY_DRIVER_NAME); + + /* + * TODO:We can use this later to check acceptsUrl for better error reporting. + * Driver jdbcDriver = (Driver) Class.forName(driver).newInstance(); + */ + String newDatabaseConnectionURL = DERBY_PROTOCOL + + DEFAULT_DB_NAME + + DEFAULT_CREATE_CONNECTION_STRING; + + ConnectionFactory connectionFactory = new DriverManagerConnectionFactory + (newDatabaseConnectionURL, null, null); + GenericObjectPool connectionPool = new GenericObjectPool(); + KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null); + //(side-effects the connection-pool so it gets its connections from the connection factory (I think)) + new PoolableConnectionFactory(connectionFactory, connectionPool, stmtPool, null, false, true); + this.poolingDataSource = new PoolingDataSource(connectionPool); + } + catch (ClassNotFoundException e) { + throw new DatabaseCreationException + ("Database driver '" + DERBY_DRIVER_NAME + "' could not be found", e); + } + } +} Property changes on: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java ___________________________________________________________________ Added: svn:mergeinfo + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mwl...@us...> - 2009-12-23 16:07:56
|
Revision: 997 http://cishell.svn.sourceforge.net/cishell/?rev=997&view=rev Author: mwlinnem Date: 2009-12-23 16:07:47 +0000 (Wed, 23 Dec 2009) Log Message: ----------- Cleanup. Modified Paths: -------------- trunk/core/org.cishell.reference.service.database/.project trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/InternalDerbyDatabase.java Added Paths: ----------- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java Removed Paths: ------------- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java Modified: trunk/core/org.cishell.reference.service.database/.project =================================================================== --- trunk/core/org.cishell.reference.service.database/.project 2009-12-21 21:30:24 UTC (rev 996) +++ trunk/core/org.cishell.reference.service.database/.project 2009-12-23 16:07:47 UTC (rev 997) @@ -20,9 +20,15 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>net.sf.eclipsecs.core.CheckstyleBuilder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> <nature>org.eclipse.pde.PluginNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> + <nature>net.sf.eclipsecs.core.CheckstyleNature</nature> </natures> </projectDescription> Modified: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java =================================================================== --- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java 2009-12-21 21:30:24 UTC (rev 996) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/DerbyDatabaseService.java 2009-12-23 16:07:47 UTC (rev 997) @@ -31,6 +31,7 @@ public static final String DERBY_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver"; public static final String DERBY_PROTOCOL = "jdbc:derby:"; + public static final String DEFAULT_CREATE_CONNECTION_STRING = ";create=true"; public static final String DEFAULT_SHUTDOWN_CONNECTION_STRING =";shutdown=true"; @@ -40,53 +41,56 @@ //hold on to our service registration so we can unregister when this plugin stops. private ServiceRegistration databaseServiceRegistration; + /* + * internal databases are created when requested by outside services, + * and are cleaned and shut down when this service is stopped. + */ private List internalDatabases = new ArrayList(); - public void start(BundleContext context) throws Exception { + public final void start(BundleContext context) throws Exception { /* * Tell Derby to look for an existing database or create a new database - * in the default directory (within our application's directory) + * in the default directory (within our application's directory) */ System.setProperty("derby.system.home", DATABASE_DIRECTORY); //allow the database service to be found by other services/plugins - databaseServiceRegistration = context.registerService - (DatabaseService.class.getName(), this, new Hashtable()); + databaseServiceRegistration = context.registerService( + DatabaseService.class.getName(), this, new Hashtable()); } - public void stop(BundleContext context) { + public final void stop(BundleContext context) { //disallow the database service to be found by other services/plugins this.databaseServiceRegistration.unregister(); - //try to clean out the databases and shut them down. + //try to clean out the internal databases and shut them down. try { - - for (Iterator it = internalDatabases.iterator(); it.hasNext();) { - InternalDerbyDatabase activeDatabase = (InternalDerbyDatabase) it.next(); - Connection activeDatabaseConnection = activeDatabase.getConnection(); - removeAllNonSystemDatabaseTables(activeDatabaseConnection); - activeDatabase.shutdown(); + InternalDerbyDatabase internalDatabase = (InternalDerbyDatabase) it.next(); + Connection internalDatabaseConnection = internalDatabase.getConnection(); + removeAllNonSystemDatabaseTables(internalDatabaseConnection); + internalDatabase.shutdown(); } } catch (Exception e) { String message = - "An unexpected exception occurred while shutting down the internal database." + - "Aborting database shutdown process." + - "Database may not be left in a valid state (but it will probably be okay)."; + "An unexpected exception occurred while shutting down the internal database." + + "Aborting database shutdown process." + + "Database may not be left in a valid state, " + + "but we will try to make its state valid on next startup."; throw new RuntimeException(message, e); } } - private static final String DB_NAME_PREFIX = "cishell_database"; + private static final String INTERNAL_DB_NAME_PREFIX = "cishell_database"; private static int id = 0; public synchronized Database createNewDatabase() throws DatabaseCreationException { try { //connect to and create a 'new' database - String databaseName = DB_NAME_PREFIX + id; - Database db = new InternalDerbyDatabase(databaseName, createNewInternalDataSource(databaseName)); + String databaseName = INTERNAL_DB_NAME_PREFIX + id; + Database db = new InternalDerbyDatabase(createNewInternalDataSource(databaseName)); //if this database existed on disk from a previous session, clean it to be like new removeAllNonSystemDatabaseTables(db.getConnection()); @@ -95,6 +99,7 @@ internalDatabases.add(db); id++; + return db; } catch (Exception e) { throw new DatabaseCreationException(e); @@ -111,9 +116,7 @@ throws DatabaseCreationException { DataSource dataSource = createNewDataSource(driver, url, username, password); - //TODO: change Database to support non-derby urls and whatnot Database db = new ExternalDatabase(dataSource); - //(assuming we don't need to keep track of these databases, as we will not close them on shutdown) return db; } @@ -128,27 +131,28 @@ //Load the database driver Class.forName(driver); - ConnectionFactory connectionFactory = new DriverManagerConnectionFactory - (url, username, password); + //create a new data source based on the database connection info provided. + ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( + url, username, password); GenericObjectPool connectionPool = new GenericObjectPool(); KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null); - //(side-effects) - new PoolableConnectionFactory(connectionFactory, connectionPool, stmtPool, null, false, true); - //(creating the data source this way is probably overkill for our current purposes, - //(but I don't imagine it does much harm either). + new PoolableConnectionFactory( + connectionFactory, connectionPool, stmtPool, null, false, true); DataSource dataSource = new PoolingDataSource(connectionPool); + + //return that data source. return dataSource; } catch (ClassNotFoundException e) { - throw new DatabaseCreationException - ("Database driver '" + driver + "' could not be found", e); + throw new DatabaseCreationException( + "Database driver '" + driver + "' could not be found", e); } } private DataSource createNewInternalDataSource(String dbName) throws DatabaseCreationException { - String newDatabaseConnectionURL = DERBY_PROTOCOL + - dbName + - DEFAULT_CREATE_CONNECTION_STRING; + String newDatabaseConnectionURL = DERBY_PROTOCOL + + dbName + + DEFAULT_CREATE_CONNECTION_STRING; return createNewDataSource(DERBY_DRIVER_NAME, newDatabaseConnectionURL, null, null); } @@ -163,10 +167,10 @@ Statement removeTables = dbConnection.createStatement(); while (allTableNames.next()) { - if (allTableNames.getString(SCHEMA_NAME_INDEX).indexOf(NONSYSTEM_SCHEMA_NAME) != -1) { + if (!hasSystemSchema(allTableNames.getString(SCHEMA_NAME_INDEX))) { String removeTableSQL = - "DROP TABLE " + - NONSYSTEM_SCHEMA_NAME + "." + allTableNames.getString(TABLE_NAME_INDEX); + "DROP TABLE " + + NONSYSTEM_SCHEMA_NAME + "." + allTableNames.getString(TABLE_NAME_INDEX); removeTables.addBatch(removeTableSQL); } } @@ -174,5 +178,7 @@ removeTables.executeBatch(); } - + private boolean hasSystemSchema(String tableSchemaName) { + return tableSchemaName.indexOf(NONSYSTEM_SCHEMA_NAME) == -1; + } } Deleted: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java =================================================================== --- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java 2009-12-21 21:30:24 UTC (rev 996) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java 2009-12-23 16:07:47 UTC (rev 997) @@ -1,30 +0,0 @@ -package org.cishell.reference.service.database; - -import java.sql.Connection; -import java.sql.SQLException; - -import javax.sql.DataSource; - -import org.cishell.service.database.Database; - -public class ExternalDatabase implements Database { - - private DataSource dataSource; - - public ExternalDatabase(DataSource dataSource) { - this.dataSource = dataSource; - } - - public Connection getConnection() throws SQLException { - return dataSource.getConnection(); - } - - /* - * TODO: We could store the password from the beginning. - * Will want to discuss security implications here. - */ - public Connection getConnection(String username, String password) - throws SQLException { - return dataSource.getConnection(username, password); - } -} Copied: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java (from rev 993, trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java) =================================================================== --- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java (rev 0) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java 2009-12-23 16:07:47 UTC (rev 997) @@ -0,0 +1,30 @@ +package org.cishell.reference.service.database; + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import org.cishell.service.database.Database; + +public class ExternalDatabase implements Database { + + private DataSource dataSource; + + public ExternalDatabase(DataSource dataSource) { + this.dataSource = dataSource; + } + + public Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + + /* + * TODO: We could store the password from the beginning. + * Will want to discuss security implications here. + */ + public Connection getConnection(String username, String password) + throws SQLException { + return dataSource.getConnection(username, password); + } +} Property changes on: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/ExternalDatabase.java ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/InternalDerbyDatabase.java =================================================================== --- trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/InternalDerbyDatabase.java 2009-12-21 21:30:24 UTC (rev 996) +++ trunk/core/org.cishell.reference.service.database/src/org/cishell/reference/service/database/InternalDerbyDatabase.java 2009-12-23 16:07:47 UTC (rev 997) @@ -10,11 +10,9 @@ public class InternalDerbyDatabase implements Database { - private String name; private DataSource dataSource; - public InternalDerbyDatabase(String name, DataSource dataSource) { - this.name = name; + public InternalDerbyDatabase(DataSource dataSource) { this.dataSource = dataSource; } @@ -25,8 +23,8 @@ public void shutdown() throws SQLException { String shutdownDatabaseCommand = - DerbyDatabaseService.DERBY_PROTOCOL + - DerbyDatabaseService.DEFAULT_SHUTDOWN_CONNECTION_STRING; + DerbyDatabaseService.DERBY_PROTOCOL + + DerbyDatabaseService.DEFAULT_SHUTDOWN_CONNECTION_STRING; DriverManager.getConnection(shutdownDatabaseCommand); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |