From: Laramie Crocker Date: Tue, 4 Jan 2011 21:37:33 +0000 (+0000) Subject: CSPACE-2496 added org.collectionspace.services.common.init package and IInitHandler... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=ce8e655ee6a40d369032df043763199e209dc6b1;p=tmp%2Fjakarta-migration.git CSPACE-2496 added org.collectionspace.services.common.init package and IInitHandler implementations using new JDBCTools class --- diff --git a/services/common/src/main/config/services/tenant-bindings.xml b/services/common/src/main/config/services/tenant-bindings.xml index c6c662251..92d2cdc76 100644 --- a/services/common/src/main/config/services/tenant-bindings.xml +++ b/services/common/src/main/config/services/tenant-bindings.xml @@ -270,7 +270,7 @@ org.collectionspace.services.loanin.nuxeo.LoaninValidatorHandler - org.collectionspace.services.common.InitHandler + org.collectionspace.services.common.init.InitHandler ID Foo diff --git a/services/common/src/main/java/org/collectionspace/services/common/InitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/InitHandler.java deleted file mode 100644 index f8a215afa..000000000 --- a/services/common/src/main/java/org/collectionspace/services/common/InitHandler.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.collectionspace.services.common; - -import com.sun.media.jai.util.DataBufferUtils; -import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.document.DocumentHandler; -import org.collectionspace.services.common.document.IInitHandler; -import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.service.ServiceBindingType; - -import java.sql.*; -import java.util.ArrayList; -import java.util.List; - -/** - * User: laramie - * $LastChangedRevision: $ - * $LastChangedDate: $ - */ -public class InitHandler implements IInitHandler { - - public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception { - //todo: all post-init tasks for services, or delegate to services that override. - System.out.println("\r\n\r\n~~~~~~~~~~~~~ in onRepositoryInitialized with ServiceBindingType: "+sbt); - - //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]); - //for every field that has an authRef, do ... - // --> Connection conn = getConnection(); - //see parameter that you need for adding indices to SQL. - - } - /* - private void doJDBC(){ - Connection conn = null; - PreparedStatement pstmt = null; - Statement stmt = null; - // First find or create the tenants - try { - String queryTenantSQL = "UPDATE TABLE ADD KEY `tablename`.`id`..."; - conn = getConnection(); - // First find or create the tenants - stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(queryTenantSQL); - ArrayList existingTenants = new ArrayList(); - while (rs.next()) { - String tId = rs.getString("id"); - String tName = rs.getString("name"); - if(tenantInfo.containsKey(tId)) { - existingTenants.add(tId); - if(!tenantInfo.get(tId).equalsIgnoreCase(tName)) { - logger.warn("Configured name for tenant: " - +tId+" in repository: "+tName - +" does not match config'd name: "+ tenantInfo.get(tId)); - } - } - } - rs.close(); - pstmt.close(); - stmt.close(); - } catch (RuntimeException rte) { - if (logger.isDebugEnabled()) { - logger.debug("Exception in createDefaultAccounts: "+ - rte.getLocalizedMessage()); - logger.debug(rte.getStackTrace().toString()); - } - throw rte; - } catch (SQLException sqle) { - // SQLExceptions can be chained. We have at least one exception, so - // set up a loop to make sure we let the user know about all of them - // if there happens to be more than one. - if (logger.isDebugEnabled()) { - SQLException tempException = sqle; - while (null != tempException) { - logger.debug("SQL Exception: " + sqle.getLocalizedMessage()); - tempException = tempException.getNextException(); - } - logger.debug(sqle.getStackTrace().toString()); - } - throw new RuntimeException("SQL problem in createDefaultAccounts: ", sqle); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Exception in createDefaultAccounts: "+ - e.getLocalizedMessage()); - } - } finally { - try { - if(conn!=null) - conn.close(); - if(pstmt!=null) - pstmt.close(); - if(stmt!=null) - stmt.close(); - } catch (SQLException sqle) { - if (logger.isDebugEnabled()) { - logger.debug("SQL Exception closing statement/connection: " - + sqle.getLocalizedMessage()); - } - } - } - } - */ - -} diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index 8a28079ee..fb0615c5a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -13,16 +13,14 @@ import java.util.Hashtable; import java.util.List; import java.util.UUID; -import javax.naming.InitialContext; -import javax.naming.NamingException; import javax.security.auth.login.LoginException; -import javax.sql.DataSource; import org.collectionspace.services.common.config.ServicesConfigReaderImpl; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; -import org.collectionspace.services.common.document.IInitHandler; +import org.collectionspace.services.common.init.IInitHandler; import org.collectionspace.services.common.security.SecurityUtils; import org.collectionspace.services.common.service.*; +import org.collectionspace.services.common.storage.JDBCTools; import org.collectionspace.services.common.tenant.TenantBindingType; import org.collectionspace.services.common.types.PropertyItemType; import org.collectionspace.services.common.types.PropertyType; @@ -58,7 +56,7 @@ public class ServiceMain { private static final String DEFAULT_ADMIN_PASSWORD = "Administrator"; private static final String DEFAULT_READER_PASSWORD = "reader"; - private static String repositoryName = "CspaceDS"; + private static String REPOSITORY_NAME = "CspaceDS"; private ServiceMain() { } @@ -594,34 +592,10 @@ public class ServiceMain { } } - private Connection getConnection() throws LoginException, SQLException { - InitialContext ctx = null; - Connection conn = null; - try { - ctx = new InitialContext(); - DataSource ds = (DataSource) ctx.lookup(repositoryName); - if (ds == null) { - throw new IllegalArgumentException("datasource not found: " + repositoryName); - } - conn = ds.getConnection(); - return conn; - } catch (NamingException ex) { - LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName); - le.initCause(ex); - throw le; - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - } - } - } + return JDBCTools.getConnection(REPOSITORY_NAME); } - - void retrieveAllWorkspaceIds() throws Exception { //all configs are read, connector is initialized, retrieve workspaceids Hashtable tenantBindings = diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java new file mode 100755 index 000000000..2d4ef97d1 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java @@ -0,0 +1,42 @@ +package org.collectionspace.services.common.init; + +import org.collectionspace.services.common.service.ServiceBindingType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.ResultSet; +import java.util.List; + +/** + * User: laramie + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class AddIndices extends InitHandler implements IInitHandler { + + final Logger logger = LoggerFactory.getLogger(AddIndices.class); + + public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception { + //todo: all post-init tasks for services, or delegate to services that override. + System.out.println("\r\n\r\n~~~~~~~~~~~~~ in AddIndices.onRepositoryInitialized with ServiceBindingType: "+sbt); + + // call something like this: + ResultSet rs = null; + try { + String addIndex_SQL = "UPDATE TABLE ADD KEY `tablename`.`id`..."; + rs = openResultSet(addIndex_SQL); + if (rs != null){ + // ..... + } + } catch (Exception e){ + throw e; + } finally { + closeResultSet(rs); + } + //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]); + //for every field that has an authRef, do ... + // --> Connection conn = getConnection(); + //see parameter that you need for adding indices to SQL. + + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java old mode 100644 new mode 100755 similarity index 84% rename from services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java rename to services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java index 9a72c2468..321006e35 --- a/services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java @@ -1,4 +1,4 @@ -package org.collectionspace.services.common.document; +package org.collectionspace.services.common.init; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.service.ServiceBindingType; diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java new file mode 100755 index 000000000..e6076390b --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java @@ -0,0 +1,63 @@ +package org.collectionspace.services.common.init; + +import org.collectionspace.services.common.storage.JDBCTools; +import org.collectionspace.services.common.service.ServiceBindingType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.*; +import java.util.List; + +/** + * User: laramie + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class InitHandler implements IInitHandler { + + final Logger logger = LoggerFactory.getLogger(InitHandler.class); + + public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception { + // see org.collectionspace.services.common.init.AddIndices for a real implementation example. + System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: "+sbt); + } + + public ResultSet openResultSet(String sql) throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = JDBCTools.getConnection(JDBCTools.DEFAULT_REPOSITORY_NAME); + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + stmt.close(); + return rs; //don't call rs.close() here ... Let caller close and catch any exceptions. + } catch (RuntimeException rte) { + logger.debug("Exception in createDefaultAccounts: "+rte.getLocalizedMessage()); + logger.debug(rte.getStackTrace().toString()); + throw rte; + } catch (SQLException sqle) { + SQLException tempException = sqle; + while (null != tempException) { // SQLExceptions can be chained. Loop to log all. + logger.debug("SQL Exception: " + sqle.getLocalizedMessage()); + tempException = tempException.getNextException(); + } + logger.debug(sqle.getStackTrace().toString()); + throw new RuntimeException("SQL problem in openResultSet: ", sqle); + } finally { + try { + if(conn!=null) conn.close(); + if(stmt!=null) stmt.close(); + } catch (SQLException sqle) { + logger.debug("SQL Exception closing statement/connection in openResultSet: "+ sqle.getLocalizedMessage()); + return null; + } + } + + } + + public void closeResultSet(ResultSet rs) throws SQLException { + rs.close(); + } + + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java new file mode 100755 index 000000000..30fe5153f --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -0,0 +1,52 @@ +package org.collectionspace.services.common.storage; + +import org.collectionspace.services.common.Tools; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.security.auth.login.LoginException; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * User: laramie + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class JDBCTools { + + public static final String DEFAULT_REPOSITORY_NAME = "CspaceDS"; + + public static Connection getConnection(String repositoryName) throws LoginException, SQLException { + if (Tools.isEmpty(repositoryName)){ + repositoryName = DEFAULT_REPOSITORY_NAME; + } + InitialContext ctx = null; + Connection conn = null; + try { + ctx = new InitialContext(); + DataSource ds = (DataSource) ctx.lookup(repositoryName); + if (ds == null) { + throw new IllegalArgumentException("datasource not found: " + repositoryName); + } + conn = ds.getConnection(); + return conn; + } catch (NamingException ex) { + LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName); + le.initCause(ex); + throw le; + } finally { + if (ctx != null) { + try { + ctx.close(); + } catch (Exception e) { + } + } + } + } + + + + +}