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. |