]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-2496 added org.collectionspace.services.common.init package and IInitHandler...
authorLaramie Crocker <laramie@berkeley.edu>
Tue, 4 Jan 2011 21:37:33 +0000 (21:37 +0000)
committerLaramie Crocker <laramie@berkeley.edu>
Tue, 4 Jan 2011 21:37:33 +0000 (21:37 +0000)
services/common/src/main/config/services/tenant-bindings.xml
services/common/src/main/java/org/collectionspace/services/common/InitHandler.java [deleted file]
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java [new file with mode: 0755]
services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java [moved from services/common/src/main/java/org/collectionspace/services/common/document/IInitHandler.java with 84% similarity, mode: 0755]
services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java [new file with mode: 0755]
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java [new file with mode: 0755]

index c6c662251179d3de6e39841f0352633555a547a8..92d2cdc76e6dd45e3ff464a0fb66d0e3ae94c509 100644 (file)
                 org.collectionspace.services.loanin.nuxeo.LoaninValidatorHandler
             </service:validatorHandler>
             <service:initHandler xmlns:service='http://collectionspace.org/services/common/service'>
-                <service:classname>org.collectionspace.services.common.InitHandler</service:classname>
+                <service:classname>org.collectionspace.services.common.init.InitHandler</service:classname>
                 <service:fields>
                     <service:field>ID</service:field>
                     <service:field>Foo</service:field>
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 (file)
index f8a215a..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-package org.collectionspace.services.common;\r
-\r
-import com.sun.media.jai.util.DataBufferUtils;\r
-import org.collectionspace.services.common.context.ServiceContext;\r
-import org.collectionspace.services.common.document.DocumentHandler;\r
-import org.collectionspace.services.common.document.IInitHandler;\r
-import org.collectionspace.services.common.document.InvalidDocumentException;\r
-import org.collectionspace.services.common.service.ServiceBindingType;\r
-\r
-import java.sql.*;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-/**\r
- * User: laramie\r
- * $LastChangedRevision:  $\r
- * $LastChangedDate:  $\r
- */\r
-public class InitHandler implements IInitHandler {\r
-\r
-    public void onRepositoryInitialized(ServiceBindingType sbt, List<String> fields) throws Exception {\r
-          //todo: all post-init tasks for services, or delegate to services that override.\r
-          System.out.println("\r\n\r\n~~~~~~~~~~~~~ in onRepositoryInitialized with ServiceBindingType: "+sbt);\r
-\r
-        //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]);\r
-        //for every field that has an authRef, do ...\r
-        //    --> Connection conn = getConnection();\r
-        //see parameter that you need for adding indices to SQL.\r
-\r
-    }\r
-     /*\r
-    private void doJDBC(){\r
-        Connection conn = null;\r
-        PreparedStatement pstmt = null;\r
-       Statement stmt = null;\r
-        // First find or create the tenants\r
-        try {\r
-            String queryTenantSQL = "UPDATE TABLE ADD KEY `tablename`.`id`...";\r
-               conn = getConnection();\r
-               // First find or create the tenants\r
-               stmt = conn.createStatement();\r
-                       ResultSet rs = stmt.executeQuery(queryTenantSQL);\r
-               ArrayList<String> existingTenants = new ArrayList<String>();\r
-                       while (rs.next()) {\r
-                               String tId = rs.getString("id");\r
-                               String tName = rs.getString("name");\r
-                               if(tenantInfo.containsKey(tId)) {\r
-                                       existingTenants.add(tId);\r
-                                       if(!tenantInfo.get(tId).equalsIgnoreCase(tName)) {\r
-                                               logger.warn("Configured name for tenant: "\r
-                                                               +tId+" in repository: "+tName\r
-                                                               +" does not match config'd name: "+ tenantInfo.get(tId));\r
-                                       }\r
-                               }\r
-                       }\r
-                       rs.close();\r
-               pstmt.close();\r
-                       stmt.close();\r
-        } catch (RuntimeException rte) {\r
-               if (logger.isDebugEnabled()) {\r
-                       logger.debug("Exception in createDefaultAccounts: "+\r
-                                               rte.getLocalizedMessage());\r
-                       logger.debug(rte.getStackTrace().toString());\r
-               }\r
-            throw rte;\r
-        } catch (SQLException sqle) {\r
-            // SQLExceptions can be chained. We have at least one exception, so\r
-            // set up a loop to make sure we let the user know about all of them\r
-            // if there happens to be more than one.\r
-               if (logger.isDebugEnabled()) {\r
-                       SQLException tempException = sqle;\r
-                       while (null != tempException) {\r
-                               logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
-                               tempException = tempException.getNextException();\r
-                       }\r
-                       logger.debug(sqle.getStackTrace().toString());\r
-               }\r
-            throw new RuntimeException("SQL problem in createDefaultAccounts: ", sqle);\r
-        } catch (Exception e) {\r
-               if (logger.isDebugEnabled()) {\r
-                       logger.debug("Exception in createDefaultAccounts: "+\r
-                                               e.getLocalizedMessage());\r
-               }\r
-        } finally {\r
-               try {\r
-               if(conn!=null)\r
-                    conn.close();\r
-               if(pstmt!=null)\r
-                    pstmt.close();\r
-               if(stmt!=null)\r
-                    stmt.close();\r
-            } catch (SQLException sqle) {\r
-               if (logger.isDebugEnabled()) {\r
-                               logger.debug("SQL Exception closing statement/connection: "\r
-                                               + sqle.getLocalizedMessage());\r
-               }\r
-               }\r
-        }\r
-    }\r
-    */\r
-\r
-}\r
index 8a28079eeafbc8e23725da453153a9ec4f5b4a2e..fb0615c5a4b61cd662b324a14283ca763278d025 100644 (file)
@@ -13,16 +13,14 @@ import java.util.Hashtable;
 import java.util.List;\r
 import java.util.UUID;\r
 \r
-import javax.naming.InitialContext;\r
-import javax.naming.NamingException;\r
 import javax.security.auth.login.LoginException;\r
-import javax.sql.DataSource;\r
 \r
 import org.collectionspace.services.common.config.ServicesConfigReaderImpl;\r
 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;\r
-import org.collectionspace.services.common.document.IInitHandler;\r
+import org.collectionspace.services.common.init.IInitHandler;\r
 import org.collectionspace.services.common.security.SecurityUtils;\r
 import org.collectionspace.services.common.service.*;\r
+import org.collectionspace.services.common.storage.JDBCTools;\r
 import org.collectionspace.services.common.tenant.TenantBindingType;\r
 import org.collectionspace.services.common.types.PropertyItemType;\r
 import org.collectionspace.services.common.types.PropertyType;\r
@@ -58,7 +56,7 @@ public class ServiceMain {
     private static final String DEFAULT_ADMIN_PASSWORD = "Administrator";\r
     private static final String DEFAULT_READER_PASSWORD = "reader";\r
     \r
-    private static String repositoryName = "CspaceDS";\r
+    private static String REPOSITORY_NAME = "CspaceDS";\r
 \r
     private ServiceMain() {\r
     }\r
@@ -594,34 +592,10 @@ public class ServiceMain {
         }\r
     }\r
 \r
-\r
     private Connection getConnection() throws LoginException, SQLException {\r
-        InitialContext ctx = null;\r
-        Connection conn = null;\r
-        try {\r
-            ctx = new InitialContext();\r
-            DataSource ds = (DataSource) ctx.lookup(repositoryName);\r
-            if (ds == null) {\r
-                throw new IllegalArgumentException("datasource not found: " + repositoryName);\r
-            }\r
-            conn = ds.getConnection();\r
-            return conn;\r
-        } catch (NamingException ex) {\r
-            LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName);\r
-            le.initCause(ex);\r
-            throw le;\r
-        } finally {\r
-            if (ctx != null) {\r
-                try {\r
-                    ctx.close();\r
-                } catch (Exception e) {\r
-                }\r
-            }\r
-        }\r
+        return JDBCTools.getConnection(REPOSITORY_NAME);\r
     }\r
 \r
-\r
-\r
     void retrieveAllWorkspaceIds() throws Exception {\r
         //all configs are read, connector is initialized, retrieve workspaceids\r
         Hashtable<String, TenantBindingType> tenantBindings =\r
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 (executable)
index 0000000..2d4ef97
--- /dev/null
@@ -0,0 +1,42 @@
+package org.collectionspace.services.common.init;\r
+\r
+import org.collectionspace.services.common.service.ServiceBindingType;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import java.sql.ResultSet;\r
+import java.util.List;\r
+\r
+/**\r
+ * User: laramie\r
+ * $LastChangedRevision:  $\r
+ * $LastChangedDate:  $\r
+ */\r
+public class AddIndices extends InitHandler implements IInitHandler {\r
+\r
+    final Logger logger = LoggerFactory.getLogger(AddIndices.class);\r
+\r
+    public void onRepositoryInitialized(ServiceBindingType sbt, List<String> fields) throws Exception {\r
+          //todo: all post-init tasks for services, or delegate to services that override.\r
+          System.out.println("\r\n\r\n~~~~~~~~~~~~~ in AddIndices.onRepositoryInitialized with ServiceBindingType: "+sbt);\r
+\r
+        // call something like this:\r
+        ResultSet rs = null;\r
+        try {\r
+            String addIndex_SQL = "UPDATE TABLE ADD KEY `tablename`.`id`...";\r
+            rs = openResultSet(addIndex_SQL);\r
+            if (rs != null){\r
+                // .....\r
+            }\r
+        } catch (Exception e){\r
+            throw e;\r
+        } finally {\r
+            closeResultSet(rs);\r
+        }\r
+        //call something like this: services.common.storage.DBUtils.addIndex(String tablename, String fields[]);\r
+        //for every field that has an authRef, do ...\r
+        //    --> Connection conn = getConnection();\r
+        //see parameter that you need for adding indices to SQL.\r
+\r
+    }\r
+}\r
@@ -1,4 +1,4 @@
-package org.collectionspace.services.common.document;\r
+package org.collectionspace.services.common.init;\r
 \r
 import org.collectionspace.services.common.context.ServiceContext;\r
 import org.collectionspace.services.common.service.ServiceBindingType;\r
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 (executable)
index 0000000..e607639
--- /dev/null
@@ -0,0 +1,63 @@
+package org.collectionspace.services.common.init;\r
+\r
+import org.collectionspace.services.common.storage.JDBCTools;\r
+import org.collectionspace.services.common.service.ServiceBindingType;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import java.sql.*;\r
+import java.util.List;\r
+\r
+/**\r
+ * User: laramie\r
+ * $LastChangedRevision:  $\r
+ * $LastChangedDate:  $\r
+ */\r
+public class InitHandler implements IInitHandler {\r
+\r
+    final Logger logger = LoggerFactory.getLogger(InitHandler.class);\r
+\r
+    public void onRepositoryInitialized(ServiceBindingType sbt, List<String> fields) throws Exception {\r
+        // see org.collectionspace.services.common.init.AddIndices for a real implementation example.\r
+        System.out.println("\r\n\r\n~~~~~~~~~~~~~ in InitHandler.onRepositoryInitialized with ServiceBindingType: "+sbt);\r
+    }\r
+\r
+    public ResultSet openResultSet(String sql) throws Exception {\r
+        Connection conn = null;\r
+       Statement stmt = null;\r
+        try {\r
+               conn = JDBCTools.getConnection(JDBCTools.DEFAULT_REPOSITORY_NAME);\r
+               stmt = conn.createStatement();\r
+                       ResultSet rs = stmt.executeQuery(sql);\r
+                       stmt.close();\r
+            return rs;  //don't call rs.close() here ... Let caller close and catch any exceptions.\r
+        } catch (RuntimeException rte) {\r
+            logger.debug("Exception in createDefaultAccounts: "+rte.getLocalizedMessage());\r
+            logger.debug(rte.getStackTrace().toString());\r
+            throw rte;\r
+        } catch (SQLException sqle) {\r
+            SQLException tempException = sqle;\r
+            while (null != tempException) {       // SQLExceptions can be chained. Loop to log all.\r
+                logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
+                tempException = tempException.getNextException();\r
+            }\r
+            logger.debug(sqle.getStackTrace().toString());\r
+            throw new RuntimeException("SQL problem in openResultSet: ", sqle);\r
+        } finally {\r
+               try {\r
+               if(conn!=null) conn.close();\r
+               if(stmt!=null) stmt.close();\r
+            } catch (SQLException sqle) {\r
+                logger.debug("SQL Exception closing statement/connection in openResultSet: "+ sqle.getLocalizedMessage());\r
+                return null;\r
+               }\r
+        }\r
+\r
+    }\r
+\r
+    public void closeResultSet(ResultSet rs) throws SQLException {\r
+        rs.close();\r
+    }\r
+\r
+\r
+}\r
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 (executable)
index 0000000..30fe515
--- /dev/null
@@ -0,0 +1,52 @@
+package org.collectionspace.services.common.storage;\r
+\r
+import org.collectionspace.services.common.Tools;\r
+\r
+import javax.naming.InitialContext;\r
+import javax.naming.NamingException;\r
+import javax.security.auth.login.LoginException;\r
+import javax.sql.DataSource;\r
+import java.sql.Connection;\r
+import java.sql.SQLException;\r
+\r
+/**\r
+ * User: laramie\r
+ * $LastChangedRevision:  $\r
+ * $LastChangedDate:  $\r
+ */\r
+public class JDBCTools {\r
+\r
+    public static final String DEFAULT_REPOSITORY_NAME = "CspaceDS";\r
+\r
+    public static Connection getConnection(String repositoryName) throws LoginException, SQLException {\r
+        if (Tools.isEmpty(repositoryName)){\r
+            repositoryName = DEFAULT_REPOSITORY_NAME;\r
+        }\r
+        InitialContext ctx = null;\r
+        Connection conn = null;\r
+        try {\r
+            ctx = new InitialContext();\r
+            DataSource ds = (DataSource) ctx.lookup(repositoryName);\r
+            if (ds == null) {\r
+                throw new IllegalArgumentException("datasource not found: " + repositoryName);\r
+            }\r
+            conn = ds.getConnection();\r
+            return conn;\r
+        } catch (NamingException ex) {\r
+            LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName);\r
+            le.initCause(ex);\r
+            throw le;\r
+        } finally {\r
+            if (ctx != null) {\r
+                try {\r
+                    ctx.close();\r
+                } catch (Exception e) {\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+\r
+\r
+}\r