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>
+++ /dev/null
-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
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
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
}\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
--- /dev/null
+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
-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
--- /dev/null
+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
--- /dev/null
+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