From d4fc48e59dbf07644f096c8cf760213a774cefef Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 3 Jan 2013 13:57:28 -0800 Subject: [PATCH] CSPACE-5813: RunSqlScript init handler now can run multiple scripts, each specified as a property in services tenant bindings configuration. --- .../tenants/tenant-bindings-proto.xml | 16 ++--- .../services/common/init/InitHandler.java | 2 + .../services/common/init/RunSqlScript.java | 59 ++++++++----------- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml index ce50d0750..35879a683 100644 --- a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml +++ b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml @@ -59,6 +59,10 @@ sqlScriptName create_id_generators_table.sql + + sqlScriptName + load_id_generators.sql + @@ -73,18 +77,6 @@ --> default-domain - - - - - org.collectionspace.services.common.init.RunSqlScript - - - sqlScriptName - load_id_generators.sql - - - 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 2ad25624f..ca2411243 100644 --- 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 @@ -42,6 +42,8 @@ public class InitHandler implements IInitHandler { * * See org.collectionspace.services.common.init.AddIndices for an implementation example. * + * @param dataSourceName + * @param repositoryName * @param sbt a service binding type. * @param fields A list of fields and their attributes. * @param properties A properties bag for additional properties. diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScript.java b/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScript.java index 085e45a85..0f259b967 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScript.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScript.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.util.ArrayList; import java.util.List; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.storage.JDBCTools; @@ -45,59 +46,51 @@ public class RunSqlScript extends InitHandler implements IInitHandler { */ @Override public void onRepositoryInitialized(String dataSourceName, - String repositoryName, - ServiceBindingType sbt, - List fields, - List properties) throws Exception { - - /* - if (logger.isInfoEnabled() && sbt != null) { - logger.info("Running SQL script in " + sbt.getName() - + " for repository domain " + sbt.getRepositoryDomain().trim() + "..."); - } - */ + String repositoryName, + ServiceBindingType sbt, + List fields, + List properties) throws Exception { if (properties == null || properties.isEmpty()) { logger.warn("No properties were provided to the RunSqlScript init handler."); logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); return; } - - String scriptName = getSqlScriptName(properties); - if (Tools.isBlank(scriptName)) { - logger.warn("Could not get SQL script name."); - logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); - return; - } - - String scriptPath = getSqlScriptPath(dataSourceName, repositoryName, scriptName); - if (Tools.isBlank(scriptPath)) { - logger.warn("Could not get path to SQL script."); + List scriptNames = getSqlScriptNames(properties); + if (scriptNames == null || scriptNames.isEmpty()) { + logger.warn("Could not obtain the name of any SQL script to run."); logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); return; } - - String scriptContents = getSqlScriptContents(scriptPath); - if (Tools.isBlank(scriptContents)) { - logger.warn("Could not get contents of SQL script."); - logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); - return; + for (String scriptName : scriptNames) { + String scriptPath = getSqlScriptPath(dataSourceName, repositoryName, scriptName); + if (Tools.isBlank(scriptPath)) { + logger.warn("Could not get path to SQL script."); + logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); + continue; + } + String scriptContents = getSqlScriptContents(scriptPath); + if (Tools.isBlank(scriptContents)) { + logger.warn("Could not get contents of SQL script."); + logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); + continue; + } + runScript(dataSourceName, repositoryName, scriptContents, scriptPath); } - - runScript(dataSourceName, repositoryName, scriptContents, scriptPath); } - private String getSqlScriptName(List properties) { + private List getSqlScriptNames(List properties) { String scriptName = ""; + List scriptNames = new ArrayList(); for (Property property : properties) { if (property.getKey().equals(SQL_SCRIPT_NAME_PROPERTY)) { scriptName = property.getValue(); if (Tools.notBlank(scriptName)) { - break; + scriptNames.add(scriptName); } } } - return scriptName; + return scriptNames; } private String getSqlScriptPath(String dataSourceName, String repositoryName, String scriptName) throws Exception { -- 2.47.3