diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /branches/das-java-beta1/samples/dbconfig/src/main/java |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/das-java-beta1/samples/dbconfig/src/main/java')
7 files changed, 902 insertions, 0 deletions
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConfigUtil.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConfigUtil.java new file mode 100644 index 0000000000..12e37422eb --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConfigUtil.java @@ -0,0 +1,56 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.das.rdb.dbconfig;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import commonj.sdo.helper.XMLHelper;
+
+/**
+ * Config util provides config-related utilities such as loading a Config
+ * instance from an InputStream
+ *
+ */
+public final class DBConfigUtil {
+
+ private DBConfigUtil() {
+ }
+
+ public static DBConfig loadDBConfig(InputStream dbconfigStream) {
+
+ if (dbconfigStream == null) {
+ throw new RuntimeException("Cannot load configuration from a null InputStream. "
+ + "Possibly caused by an incorrect config xml file name");
+ }
+
+ SDOUtil.registerStaticTypes(DbconfigFactory.class);
+ XMLHelper helper = XMLHelper.INSTANCE;
+
+ try {
+ return (DBConfig) helper.load(dbconfigStream).getRootObject();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConnectionHelper.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConnectionHelper.java new file mode 100644 index 0000000000..55f54f8f69 --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBConnectionHelper.java @@ -0,0 +1,165 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.das.rdb.dbconfig;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+public class DBConnectionHelper {
+ private static final Logger logger = Logger.getLogger(DBConnectionHelper.class);
+
+ protected DBConnectionHelper(){
+ if(logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper()");
+ }
+ }
+
+ /**
+ * Basic validation of Config for connection information and call to
+ * connection helper to establish database connection. Connection using
+ * DriverManager or DataSource are supported.
+ *
+ */
+ public static Connection createConnection(ConnectionInfo connectionInfo) {
+ if (logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper.createConnection(ConnectionInfo)");
+ }
+
+ if (connectionInfo == null ||
+ (connectionInfo.getDataSource() == null && connectionInfo.getConnectionProperties() == null)) {
+ throw new RuntimeException("No connection has been provided and no data source has been specified");
+ }
+
+ if(connectionInfo.getDataSource() != null && connectionInfo.getConnectionProperties() != null){
+ throw new RuntimeException("Use either dataSource or ConnectionProperties. Can't use both !");
+ }
+
+ Connection connection = null;
+
+ if(connectionInfo.getConnectionProperties() != null){
+ connection = initializeDriverManagerConnection(connectionInfo);
+ }else{
+ connection = initializeDatasourceConnection(connectionInfo);
+ }
+
+ return connection;
+
+ }
+ /**
+ * Initializes a DB connection on a managed environmet (e.g inside Tomcat)
+ */
+ private static Connection initializeDatasourceConnection(ConnectionInfo connectionInfo){
+ if (logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper.initializeDatasourceConnection(ConnectionInfo)");
+ }
+
+ InitialContext ctx;
+ Connection connection;
+
+ try {
+ ctx = new InitialContext();
+ } catch (NamingException e) {
+ throw new RuntimeException(e);
+ }
+ try {
+ DataSource ds = (DataSource) ctx.lookup(connectionInfo.getDataSource());
+ try {
+ connection = ds.getConnection();
+ if (connection == null) {
+ throw new RuntimeException("Could not obtain a Connection from DataSource");
+ }
+ connection.setAutoCommit(true);
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ } catch (NamingException e) {
+ throw new RuntimeException(e);
+ }
+
+ if( logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper.initializeDatasourceConnection() exit");
+ }
+ return connection;
+ }
+
+ /**
+ * Initialize a DB connection on a J2SE environment
+ * For more info, see http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/drivermanager.html
+ */
+ private static Connection initializeDriverManagerConnection(ConnectionInfo connectionInfo) {
+ if (logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper.initializeDriverManagerConnection(ConnectionInfo)");
+ }
+
+ if (connectionInfo.getConnectionProperties() == null) {
+ throw new DataSourceInitializationException("No existing context and no connection properties");
+ }
+
+ if (connectionInfo.getConnectionProperties().getDriverClass() == null) {
+ throw new DataSourceInitializationException("No jdbc driver class specified!");
+ }
+
+ Connection connection;
+
+ try {
+ //initialize driver and register it with DriverManager
+ Class.forName(connectionInfo.getConnectionProperties().getDriverClass());
+
+ //prepare to initialize connection
+ String databaseUrl = connectionInfo.getConnectionProperties().getDatabaseURL();
+ String userName = connectionInfo.getConnectionProperties().getUserName();
+ String userPassword = connectionInfo.getConnectionProperties().getPassword();
+ int loginTimeout = connectionInfo.getConnectionProperties().getLoginTimeout();
+
+ DriverManager.setLoginTimeout(loginTimeout);
+ if( (userName == null || userName.length() ==0) && (userPassword == null || userPassword.length()==0) ){
+ //no username or password suplied
+ connection = DriverManager.getConnection(databaseUrl);
+ }else{
+ connection = DriverManager.getConnection(databaseUrl, userName, userPassword);
+ }
+
+ if(connection == null){
+ throw new DataSourceInitializationException("Error initializing connection : null");
+ }
+
+ connection.setAutoCommit(true);
+ }catch(ClassNotFoundException cnf){
+ throw new DataSourceInitializationException("JDBC Driver '" + connectionInfo.getConnectionProperties().getDriverClass() + "' not found", cnf);
+ }catch(SQLException sqle){
+ throw new DataSourceInitializationException(sqle.getMessage(), sqle);
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.log(Level.DEBUG, "DBConnectionHelper.initializeDriverManagerConnection() exit");
+ }
+
+ return connection;
+ }
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBDataHelper.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBDataHelper.java new file mode 100644 index 0000000000..2ef25bdf81 --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBDataHelper.java @@ -0,0 +1,251 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.das.rdb.dbconfig;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+public class DBDataHelper {
+ private static final String CLASS_NAME = "DBDataHelper";
+
+ private final Logger logger = Logger.getLogger(DBDataHelper.class);
+
+ private final DBConfig dbConfig;
+
+ protected DBDataHelper(DBConfig dbConfig) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "DBDataHelper()");
+ }
+
+ this.dbConfig = dbConfig;
+ }
+
+ public boolean isDatabasePopulated() {
+ boolean isPopulated = true;
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".isDatabasePopulated()");
+ }
+
+ Iterator tableIterator = dbConfig.getTable().iterator();
+ while (tableIterator.hasNext()) {
+ Table table = (Table) tableIterator.next();
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".isDatabasePopulated() calling isTablePopulated() for '" + table.getName() + "'");
+ }
+
+ isPopulated = this.isTablePopulated(table.getName());
+ if (isPopulated == false) {
+ break;
+ }
+ }
+ return isPopulated;
+ }
+
+ /**
+ *
+ * @param tableName
+ * @return Count of rows present in the table specified by tableName
+ */
+ protected boolean isTablePopulated(String tableName) {
+ boolean isPopulated = false;
+ Connection dbConnection = null;
+ Statement dbStatement = null;
+
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbStatement = dbConnection.createStatement();
+ String sqlString = "select count(*) from " + tableName;
+
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> sqlString => '" + sqlString + "'");
+
+ ResultSet rs = dbStatement.executeQuery(sqlString);
+ rs.next();
+
+ if (rs != null) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> pointer set");
+ }
+ }
+
+ int count = rs.getInt(1);
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> '" + tableName + "' => " + count);
+ }
+
+ if (count > 0) {
+ isPopulated = true;
+ }
+
+ } catch (SQLException e) {
+ // ignore and return false
+ } finally {
+ try {
+ dbStatement.close();
+ dbConnection.close();
+ } catch (SQLException e1) {
+ // ignore and return false
+ }
+ }
+
+ return isPopulated;
+ }
+
+ protected String generateInsertSQL(Connection dbConnection, String tableName, String rowValues) throws SQLException {
+ StringBuffer sqlBuffer = new StringBuffer(50);
+
+ Statement dbStatement = dbConnection.createStatement();
+ ResultSet dummyRS = dbStatement.executeQuery("SELECT * FROM "+tableName);
+ ResultSetMetaData rsMetaData = dummyRS.getMetaData();
+
+
+ sqlBuffer.append("INSERT INTO ").append(tableName).append(" (");
+
+ int numberOfColumns = rsMetaData.getColumnCount();
+ String columnName = null;
+ int i;
+ for (i = 1; i <= numberOfColumns -1; i++) {
+ //get the column's name.
+ columnName = rsMetaData.getColumnName(i);
+ if(!rsMetaData.isAutoIncrement(i)){
+ sqlBuffer.append(columnName).append(",");
+ }
+ else{
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> auto increment column => " + i + " "+columnName);
+ }
+ }
+ }
+ sqlBuffer.append(rsMetaData.getColumnName(i)).append(") ");
+ sqlBuffer.append("VALUES (").append(rowValues).append(")");
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> SQL Clause => " + sqlBuffer);
+ }
+
+ return sqlBuffer.toString();
+ }
+
+ public void initializeDatabaseData() {
+ Connection dbConnection = null;
+ Statement dbStatement = null;
+
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbStatement = dbConnection.createStatement();
+
+ Iterator tableIterator = dbConfig.getTable().iterator();
+ while (tableIterator.hasNext()) {
+ Table table = (Table) tableIterator.next();
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> INSERT FOR TABLE => '" + table.getName() + "'");
+ }
+
+ //String columnClause = generateInsertSQL(dbConnection, table.getName());
+
+ String tableName = table.getName();
+ Iterator dataIterator = table.getRow().iterator();
+ while (dataIterator.hasNext()) {
+ String tableRow = (String) dataIterator.next();
+ String sqlString = generateInsertSQL(dbConnection, tableName, tableRow);
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> sqlString => '" + sqlString + "'");
+ }
+
+ try {
+ dbStatement.executeUpdate(sqlString);
+ }catch(SQLException e){
+ //e.printStackTrace();
+ //ignore and jump to new table
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Error inserting table data : " + e.getMessage(), e);
+ }
+ }
+ }
+ }
+ } catch (SQLException e) {
+ // ignore and return false
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Internal error : " + e.getMessage(), e);
+ }
+ } finally {
+ try {
+ dbStatement.close();
+ dbConnection.close();
+ } catch (SQLException e1) {
+ // ignore and return false
+ }
+ }
+ }
+
+ public void deleteDatabaseData() {
+ Connection dbConnection = null;
+ Statement dbStatement = null;
+
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbStatement = dbConnection.createStatement();
+ //inverse order - to take care of parent-child
+ List tables = dbConfig.getTable();
+ for(int i=tables.size()-1; i>-1; i-- ){
+ Table table = (Table) tables.get(i);
+ String sqlString = "DELETE FROM " + table.getName() ;
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".deleteDatabaseData()=> sqlString => '" + sqlString + "'");
+ }
+
+ try {
+ dbStatement.executeQuery(sqlString);
+ }catch(SQLException e){
+ e.printStackTrace();
+ //ignore and jump to new table
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".deleteDatabaseData() - Error inserting table data : " + e.getMessage(), e);
+ }
+ }
+ }
+ } catch (SQLException e) {
+ // ignore and return false
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Internal error : " + e.getMessage(), e);
+ }
+ } finally {
+ try {
+ dbStatement.close();
+ dbConnection.close();
+ } catch (SQLException e1) {
+ // ignore and return false
+ }
+ }
+ }
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBHelper.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBHelper.java new file mode 100644 index 0000000000..d048f73245 --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBHelper.java @@ -0,0 +1,196 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.das.rdb.dbconfig;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+public class DBHelper {
+ private static final String CLASS_NAME = "DBHelper";
+
+ private final Logger logger = Logger.getLogger(DBHelper.class);
+
+ private final DBConfig dbConfig;
+
+ /**
+ * Constructor
+ *
+ * @param DBConfig
+ */
+ protected DBHelper(DBConfig dbConfig) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "()");
+ }
+ this.dbConfig = dbConfig;
+ }
+
+ /**
+ * Check if tables specified in Config exist
+ *
+ * @return true if all specified tables exist, false otherwise
+ */
+ protected boolean isDatabaseReady() {
+ boolean bResult = true;
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "isDatabaseReady()");
+ }
+
+ Connection dbConnection = null;
+ DatabaseMetaData dbMetaData = null;
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbMetaData = dbConnection.getMetaData();
+
+ if (dbConfig.getTable() != null && dbConfig.getTable().size() > 0) {
+ Iterator tableIterator = dbConfig.getTable().iterator();
+ while (tableIterator.hasNext()) {
+ Table table = (Table) tableIterator.next();
+
+ if (!dbMetaData.getTables(null, null, table.getName(), null).next()) {
+ bResult = false;
+ }
+
+ }
+ }
+ } catch (SQLException e) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Error retrieving database metadata", e);
+ }
+ } finally {
+ try {
+ dbConnection.close();
+ } catch (SQLException e) {
+ // ignore here
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "isDatabaseReady() exit");
+ }
+
+ return bResult;
+ }
+
+ /**
+ * Create the database tables based on dbConfig definition
+ */
+ protected void initializeDatabase() {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "initializeDatabase()");
+ }
+
+ Connection dbConnection = null;
+ Statement dbStatement = null;
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbStatement = dbConnection.createStatement();
+
+ if (dbConfig.getTable() != null && dbConfig.getTable().size() > 0) {
+ Iterator tableIterator = dbConfig.getTable().iterator();
+ while (tableIterator.hasNext()) {
+ Table table = (Table) tableIterator.next();
+
+ if (table.getSQLCreate() != null && table.getSQLCreate().length() > 0) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Creating table '" + table.getName() + "' => " + table.getSQLCreate());
+ }
+
+ dbStatement.execute(table.getSQLCreate());
+ }
+ }
+ }
+ } catch (SQLException e) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Error retrieving database metadata", e);
+ }
+ } finally {
+ try {
+ dbStatement.close();
+ dbConnection.close();
+ } catch (SQLException e) {
+ // ignore here
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "initializeDatabase() exit");
+ }
+ }
+
+ /**
+ * Drop the database tables based on dbConfig definition
+ */
+ protected void dropDatabaseTables() {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "dropDatabaseTables()");
+ }
+
+ Connection dbConnection = null;
+ Statement dbStatement = null;
+ try {
+ dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
+ dbStatement = dbConnection.createStatement();
+
+ if (dbConfig.getTable() != null && dbConfig.getTable().size() > 0) {
+ Iterator tableIterator = dbConfig.getTable().iterator();
+ while (tableIterator.hasNext()) {
+ Table table = (Table) tableIterator.next();
+
+ if (table.getSQLCreate() != null && table.getSQLCreate().length() > 0) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Dropping table '" + table.getName() );
+ }
+
+ try {
+ dbStatement.execute("DROP TABLE " + table.getName());
+ } catch (SQLException e) {
+ //ignore
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Error droping table '" + table.getName() + "'", e);
+ }
+ }
+ }
+ }
+ }
+ } catch (SQLException e) {
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, "Error droping table", e);
+ }
+ } finally {
+ try {
+ dbStatement.close();
+ dbConnection.close();
+ } catch (SQLException e) {
+ // ignore here
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ this.logger.log(Level.DEBUG, CLASS_NAME + "dropDatabaseTables() exit");
+ }
+ }
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBInitializer.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBInitializer.java new file mode 100644 index 0000000000..cc6a670dfd --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBInitializer.java @@ -0,0 +1,154 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.das.rdb.dbconfig;
+
+import java.io.InputStream;
+import java.sql.Connection;
+
+/**
+ * This is the master class having public utility APIs exposed for DAS sample creater - for table and data maintainance.
+ *
+ */
+public class DBInitializer {
+ private static final String DEFAULT_CANNED_DB_CONFIGURATION = "CannedSampleDBConfig.xml";
+
+ /**
+ * Database configuration model
+ */
+ protected DBConfig dbConfig;
+
+ /**
+ * Database connection
+ */
+ protected Connection connection;
+
+ /**
+ * To manage database connection
+ */
+ protected DBConnectionHelper dbConnectionHelper;
+
+ /**
+ * To manage database phisical structure
+ */
+ protected DBHelper dbHelper;
+
+ /**
+ * To manage database population
+ */
+ protected DBDataHelper dbDataHelper;
+
+ /**
+ * Create instance of DBInitHelper based on default canned database configuration
+ *
+ * @param dbconfigFileLocation
+ * @throws Exception
+ */
+ public DBInitializer() {
+ InputStream dbConfigStream = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_CANNED_DB_CONFIGURATION);
+ init(dbConfigStream);
+ }
+
+ /**
+ * Create instance of DBInitHelper based on Config file location
+ *
+ * @param dbconfigFileLocation
+ * @throws Exception
+ */
+ public DBInitializer(String dbconfigFileLocation) throws Exception {
+ InputStream dbConfigStream = this.getClass().getClassLoader().getResourceAsStream(dbconfigFileLocation);
+ init(dbConfigStream);
+ }
+
+ /**
+ * Create instance of DBInitHelper based on Config stream
+ *
+ * @param dbconfigStream
+ * @throws Exception
+ */
+ public DBInitializer(InputStream dbConfigStream) throws Exception {
+ init(dbConfigStream);
+ }
+
+ /**
+ * Initialize helper members based on Config
+ *
+ * @param dbconfigStream
+ */
+ protected void init(InputStream dbconfigStream) {
+ dbConfig = DBConfigUtil.loadDBConfig(dbconfigStream);
+
+ dbConnectionHelper = new DBConnectionHelper();
+ dbHelper = new DBHelper(dbConfig);
+ dbDataHelper = new DBDataHelper(dbConfig);
+
+ }
+
+ /**
+ * Check if the Database and all tables have been created on the database
+ *
+ * @return return true if tables exist, else return false.
+ */
+ public boolean isDatabaseReady() {
+ return dbHelper.isDatabaseReady();
+ }
+
+ /**
+ *
+ * @return - return true if all tables have at least a row, false otherwise
+ */
+ public boolean isDatabasePopulated() {
+ return dbDataHelper.isDatabasePopulated();
+ }
+
+ /**
+ * Create tables and populate data.
+ *
+ * @param clean - If true, tables will be force dropped and recreated, else it will skip table creation for pre-existing tables.
+ * @throws Exception
+ */
+ public void initializeDatabase(boolean clean) throws DatabaseInitializerException {
+ if (clean) {
+ dbHelper.dropDatabaseTables();
+ }
+ dbHelper.initializeDatabase();
+ dbDataHelper.initializeDatabaseData();
+ }
+
+ /**
+ * Populate database data
+ *
+ * @param clean If true, table data will be droped before data is created
+ */
+ public void initializeDatabaseData(boolean clean) throws DatabaseInitializerException{
+ if (clean) {
+ dbDataHelper.deleteDatabaseData();
+ }
+ dbDataHelper.initializeDatabaseData();
+ }
+
+ /**
+ * Refresh data in tables.
+ *
+ * @throws Exception
+ */
+ public void refreshDatabaseData() throws DatabaseInitializerException {
+ initializeDatabase(true);
+ }
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DataSourceInitializationException.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DataSourceInitializationException.java new file mode 100644 index 0000000000..ef5bef7761 --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DataSourceInitializationException.java @@ -0,0 +1,40 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.das.rdb.dbconfig;
+
+/**
+ *
+ * Class responsible to throw exception encountered when initializing DataSource.
+ *
+ */
+public class DataSourceInitializationException extends RuntimeException {
+ private static final long serialVersionUID = 302160989411041041L;
+
+ public DataSourceInitializationException(String string) {
+ super(string);
+ }
+
+ public DataSourceInitializationException(Throwable e){
+ super(e);
+ }
+
+ public DataSourceInitializationException(String string, Throwable e) {
+ super(string, e);
+ }
+}
diff --git a/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DatabaseInitializerException.java b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DatabaseInitializerException.java new file mode 100644 index 0000000000..8c4fd0d71b --- /dev/null +++ b/branches/das-java-beta1/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DatabaseInitializerException.java @@ -0,0 +1,40 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.das.rdb.dbconfig;
+
+/**
+ *
+ * Class responsible to throw exception encountered when initializing DataSource.
+ *
+ */
+public class DatabaseInitializerException extends RuntimeException {
+ private static final long serialVersionUID = 302160989411041041L;
+
+ public DatabaseInitializerException(String string) {
+ super(string);
+ }
+
+ public DatabaseInitializerException(Throwable e){
+ super(e);
+ }
+
+ public DatabaseInitializerException(String string, Throwable e) {
+ super(string, e);
+ }
+}
|