From 7582eb6eb3b95c2e6bdf49a9c2da43c30cca8b99 Mon Sep 17 00:00:00 2001 From: Laramie Crocker Date: Wed, 5 Jan 2011 23:16:16 +0000 Subject: [PATCH] CSPACE-2496 Added support for richer params in IInitHandler and in initHandler block in tenant-bindings. --- .../main/config/services/tenant-bindings.xml | 38 ++++++++++ .../services/common/ServiceMain.java | 17 +++-- .../services/common/init/AddIndices.java | 22 +++++- .../services/common/init/IInitHandler.java | 4 +- .../services/common/init/InitHandler.java | 76 ++++++++++--------- .../services/common/storage/JDBCTools.java | 61 ++++++++++++++- .../common/src/main/resources/service.xsd | 32 +++++++- 7 files changed, 205 insertions(+), 45 deletions(-) diff --git a/services/common/src/main/config/services/tenant-bindings.xml b/services/common/src/main/config/services/tenant-bindings.xml index 92d2cdc76..6736d569c 100644 --- a/services/common/src/main/config/services/tenant-bindings.xml +++ b/services/common/src/main/config/services/tenant-bindings.xml @@ -269,6 +269,7 @@ org.collectionspace.services.loanin.nuxeo.LoaninValidatorHandler + + + org.collectionspace.services.common.init.InitHandler + + + mytable + mycol + mytpe + myparam, myparamB + + + mytable2 + mycol2 + mytpe2 + myparam, myparam2B + + + + my keymy value + + + my key 2my value 2 + + + + + + + + datePatternMMM dd, yyyy + datePatterndd.MM.yyyy + + localeLanguageen + + + + 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 fb0615c5a..97167c197 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 @@ -56,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 REPOSITORY_NAME = "CspaceDS"; + public static String DEFAULT_REPOSITORY_NAME = "CspaceDS"; private ServiceMain() { } @@ -528,12 +528,19 @@ public class ServiceMain { if (list!=null && list.size()>0){ org.collectionspace.services.common.service.InitHandler handlerType = list.get(0); String initHandlerClassname = handlerType.getClassname(); - org.collectionspace.services.common.service.InitHandler.Fields ft = handlerType.getFields(); - List fields = ft.getField(); + + List + fields = handlerType.getParams().getField(); + + List + props = handlerType.getParams().getProperty(); + + //org.collectionspace.services.common.service.InitHandler.Fields ft = handlerType.getFields(); + //List fields = ft.getField(); Object o = instantiate(initHandlerClassname, IInitHandler.class); if (o != null && o instanceof IInitHandler){ IInitHandler handler = (IInitHandler)o; - handler.onRepositoryInitialized(sbt, fields); + handler.onRepositoryInitialized(sbt, fields, props); //The InitHandler may be the default one, // or specialized classes which still implement this interface and are registered in tenant-bindings.xml. } @@ -593,7 +600,7 @@ public class ServiceMain { } private Connection getConnection() throws LoginException, SQLException { - return JDBCTools.getConnection(REPOSITORY_NAME); + return JDBCTools.getConnection(DEFAULT_REPOSITORY_NAME); } void retrieveAllWorkspaceIds() throws Exception { 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 index 2d4ef97d1..b763be671 100755 --- 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 @@ -1,6 +1,26 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ package org.collectionspace.services.common.init; import org.collectionspace.services.common.service.ServiceBindingType; +import org.collectionspace.services.common.service.InitHandler.Params.Field; +import org.collectionspace.services.common.service.InitHandler.Params.Property; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,7 +36,7 @@ public class AddIndices extends InitHandler implements IInitHandler { final Logger logger = LoggerFactory.getLogger(AddIndices.class); - public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception { + public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List property) 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); diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java index 321006e35..b4ee4ef39 100755 --- a/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java @@ -2,6 +2,8 @@ package org.collectionspace.services.common.init; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.service.ServiceBindingType; +import org.collectionspace.services.common.service.InitHandler.Params.Field; +import org.collectionspace.services.common.service.InitHandler.Params.Property; import java.util.List; @@ -11,5 +13,5 @@ import java.util.List; * $LastChangedDate: $ */ public interface IInitHandler { - public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception; + public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List property) throws Exception; } 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 index e6076390b..8786c5a76 100755 --- 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 @@ -1,58 +1,64 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ + package org.collectionspace.services.common.init; +import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.common.storage.JDBCTools; import org.collectionspace.services.common.service.ServiceBindingType; +import org.collectionspace.services.common.service.InitHandler.Params.Field; +import org.collectionspace.services.common.service.InitHandler.Params.Property; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.sql.*; import java.util.List; -/** - * User: laramie - * $LastChangedRevision: $ - * $LastChangedDate: $ +/** Concrete class which does nothing, but subclasses may override to do + * some action on the event onRepositoryInitialized(), such as sending JDBC + * calls to the repository to add indices, etc. + * @author Laramie */ public class InitHandler implements IInitHandler { final Logger logger = LoggerFactory.getLogger(InitHandler.class); - public void onRepositoryInitialized(ServiceBindingType sbt, List fields) throws Exception { + public void onRepositoryInitialized(ServiceBindingType sbt, List fields, List properties) 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); - } + for (Field field : fields){ + System.out.println( "InitHandler.fields:" + +"\r\n col: "+field.getCol() + +" table: "+field.getTable() + +" type: "+field.getType() + +" param: "+field.getParam()); + } + for (Property prop : properties){ + System.out.println( "InitHandler.properties:" + +"\r\n key: "+prop.getKey() + +" value: "+prop.getValue()); - 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 ResultSet openResultSet(String sql) throws Exception { + return JDBCTools.openResultSet(sql); } public void closeResultSet(ResultSet rs) throws SQLException { 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 index 30fe5153f..b6921a5f0 100755 --- 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 @@ -1,13 +1,36 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ + package org.collectionspace.services.common.storage; +import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.common.Tools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; 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.ResultSet; import java.sql.SQLException; +import java.sql.Statement; /** * User: laramie @@ -16,11 +39,12 @@ import java.sql.SQLException; */ public class JDBCTools { - public static final String DEFAULT_REPOSITORY_NAME = "CspaceDS"; + //todo: make sure this will get instantiated in the right order + final static Logger logger = LoggerFactory.getLogger(JDBCTools.class); public static Connection getConnection(String repositoryName) throws LoginException, SQLException { if (Tools.isEmpty(repositoryName)){ - repositoryName = DEFAULT_REPOSITORY_NAME; + repositoryName = ServiceMain.DEFAULT_REPOSITORY_NAME; } InitialContext ctx = null; Connection conn = null; @@ -46,6 +70,39 @@ public class JDBCTools { } } + public static ResultSet openResultSet(String sql) throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = JDBCTools.getConnection(ServiceMain.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; + } + } + + } + diff --git a/services/common/src/main/resources/service.xsd b/services/common/src/main/resources/service.xsd index 9ef2ccf2f..af351db82 100644 --- a/services/common/src/main/resources/service.xsd +++ b/services/common/src/main/resources/service.xsd @@ -187,7 +187,7 @@ - + @@ -201,6 +201,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.47.3