]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5813: RunSqlScript init handler now can run multiple scripts, each specified...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 3 Jan 2013 21:57:28 +0000 (13:57 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 3 Jan 2013 21:57:28 +0000 (13:57 -0800)
services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto.xml
services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java
services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScript.java

index ce50d07508c5df9aa293db37bed86ac03e63811e..35879a683f2c151a1e5c246030be22fea54d3f77 100644 (file)
                         <service:key>sqlScriptName</service:key>
                         <service:value>create_id_generators_table.sql</service:value>
                     </service:property>
+                    <service:property>
+                        <service:key>sqlScriptName</service:key>
+                        <service:value>load_id_generators.sql</service:value>
+                    </service:property>
                 </service:params>
             </service:initHandler>
         </tenant:serviceBindings>
                 </service:uriPath>
             -->
             <service:repositoryDomain xmlns:service="http://collectionspace.org/services/config/service">default-domain</service:repositoryDomain>
-            <!-- The following initHandler config block is placed here, rather than in the -->
-            <!-- idgenerators service, above, as it appears that we currently can only run -->
-            <!-- one initHandler per service - ADR 2012-12-13 -->
-            <service:initHandler xmlns:service="http://collectionspace.org/services/config/service">
-                <service:classname>org.collectionspace.services.common.init.RunSqlScript</service:classname>
-                <service:params>
-                    <service:property>
-                        <service:key>sqlScriptName</service:key>
-                        <service:value>load_id_generators.sql</service:value>
-                    </service:property>
-                </service:params>
-            </service:initHandler>
         </tenant:serviceBindings>    
         <!-- end id service meta-data -->
         
index 2ad25624f4f94861dce88822a7bbc83cc8a79855..ca2411243e524190e92a855484a4cc5bc4fc6805 100644 (file)
@@ -42,6 +42,8 @@ public class InitHandler implements IInitHandler {
      *\r
      * See org.collectionspace.services.common.init.AddIndices for an implementation example.\r
      *\r
+     * @param dataSourceName \r
+     * @param repositoryName \r
      * @param sbt a service binding type.\r
      * @param fields A list of fields and their attributes.\r
      * @param properties A properties bag for additional properties.\r
index 085e45a8542839265cd46d994c606e66649e3fac..0f259b9676d5fcbeabd9dc94f097f02224733562 100644 (file)
@@ -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<Field> fields, 
-               List<Property> 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<Field> fields,
+            List<Property> 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<String> 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<Property> properties) {
+    private List<String> getSqlScriptNames(List<Property> properties) {
         String scriptName = "";
+        List<String> scriptNames = new ArrayList<String>();
         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 {