From cee3849e21fe81cb16f0e00639a8ec211ba9cbca Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Wed, 19 Nov 2008 12:56:10 +0000
Subject: [PATCH] [LMS-634] change: - 'DatabaseCreationUtil' moved to
 'sourceTest'. - 'DatabaseCreationUtil' only does migration and no longer
 indexing. add: - 'IndexCreationUtil' and 'Indexing of Database' launch
 configuration for indexing.

SVN: 9040
---
 .../eclipse/Indexing of Database.launch       |  14 +++
 .../Migration of Database Version 027.launch  |  28 ++---
 .../dataaccess/db/DatabaseCreationUtil.java   |  97 +++-------------
 .../dataaccess/db/IndexCreationUtil.java      | 108 ++++++++++++++++++
 4 files changed, 152 insertions(+), 95 deletions(-)
 create mode 100644 openbis/resource/eclipse/Indexing of Database.launch
 rename openbis/{source => sourceTest}/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java (60%)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/IndexCreationUtil.java

diff --git a/openbis/resource/eclipse/Indexing of Database.launch b/openbis/resource/eclipse/Indexing of Database.launch
new file mode 100644
index 00000000000..42c3b0eb914
--- /dev/null
+++ b/openbis/resource/eclipse/Indexing of Database.launch	
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
+<listEntry value="/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/IndexCreationUtil.java"/>
+</listAttribute>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
+<listEntry value="1"/>
+</listAttribute>
+<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ch.systemsx.cisd.openbis.generic.server.dataaccess.db.IndexCreationUtil"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="migration_dump"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="openbis"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea&#13;&#10;-Xmx512M"/>
+</launchConfiguration>
diff --git a/openbis/resource/eclipse/Migration of Database Version 027.launch b/openbis/resource/eclipse/Migration of Database Version 027.launch
index 356d34dd6e2..5cd941bb727 100644
--- a/openbis/resource/eclipse/Migration of Database Version 027.launch	
+++ b/openbis/resource/eclipse/Migration of Database Version 027.launch	
@@ -1,14 +1,14 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java"/>
-</listAttribute>
-<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
-<listEntry value="1"/>
-</listAttribute>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DatabaseCreationUtil"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="027"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="openbis"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea&#13;&#10;-Xmx512M"/>
-</launchConfiguration>
+<?xml version="1.0" encoding="UTF-8"?>
+<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
+<listEntry value="/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java"/>
+</listAttribute>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
+<listEntry value="1"/>
+</listAttribute>
+<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DatabaseCreationUtil"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="027"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="openbis"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea&#13;&#10;-Xmx512M"/>
+</launchConfiguration>
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java
similarity index 60%
rename from openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java
rename to openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java
index 07a993ee846..b9d3d57c6ad 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DatabaseCreationUtil.java
@@ -19,8 +19,6 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.db;
 import java.io.File;
 import java.io.IOException;
 
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.SessionFactory;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -31,9 +29,6 @@ import ch.systemsx.cisd.dbmigration.DBMigrationEngine;
 import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext;
 import ch.systemsx.cisd.dbmigration.ISqlScriptProvider;
 import ch.systemsx.cisd.dbmigration.postgresql.DumpPreparator;
-import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.FullTextIndexerRunnable;
-import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.HibernateSearchContext;
-import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.IndexMode;
 
 /**
  * Utility methods around database creation.
@@ -42,57 +37,16 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.IndexMode;
  */
 public final class DatabaseCreationUtil
 {
-    private static final String LUCENE_INDICES = "sourceTest/lucene/indices";
-
     private static BeanFactory beanFactory;
 
-    private static HibernateSearchContext hibernateSearchContext;
-
     static
     {
         LogInitializer.init();
-        // Deactivate the indexing in the application context loaded by Spring.
-        System.setProperty("hibernate.search.index-mode", "NO_INDEX");
-        System.setProperty("hibernate.search.index-base", LUCENE_INDICES);
     }
 
-    public static void main(final String[] args) throws Exception
+    private DatabaseCreationUtil()
     {
-        if (args.length == 0 || args.length > 2)
-        {
-            System.out.println("Usage: java " + DatabaseCreationUtil.class.getName()
-                    + "[--reindex[=true|false]] <database version>");
-            System.exit(1);
-        }
-        final String databaseVersion;
-        boolean reindex = false;
-        if (args.length == 1)
-        {
-            reindex = false;
-            databaseVersion = args[0];
-        } else
-        {
-            if (args[0].startsWith("--reindex"))
-            {
-                if (args[0].indexOf('=') > -1)
-                {
-                    reindex = Boolean.parseBoolean(StringUtils.split(args[0], '=')[1]);
-                } else
-                {
-                    reindex = true;
-                }
-            }
-            databaseVersion = args[1];
-        }
-        // If reindexing is asked, creating a HibernateSearchContext should be done before the
-        // Spring application context gets loaded because HibernateSearchContext removes the whole
-        // indices directory.
-        if (reindex)
-        {
-            hibernateSearchContext = createHibernateSearchContext();
-            hibernateSearchContext.afterPropertiesSet();
-        }
-        createFilesFromADumpOfAMigratedDatabase(databaseVersion, reindex);
+        // This class can not be instantiated.
     }
 
     private final static BeanFactory getBeanFactory()
@@ -110,11 +64,9 @@ public final class DatabaseCreationUtil
     /**
      * Creates all files in <code>sourceTest/sql/postgresql</code> necessary to set up a database
      * of the current version by dumping a database migrated from the specified version.
-     * 
-     * @param reindex
      */
-    private final static void createFilesFromADumpOfAMigratedDatabase(final String databaseVersion,
-            final boolean reindex) throws Exception
+    private final static void createFilesFromADumpOfAMigratedDatabase(final String databaseVersion)
+            throws Exception
     {
         final String databaseKind = "migration_dump";
         final DatabaseConfigurationContext context =
@@ -130,33 +82,6 @@ public final class DatabaseCreationUtil
         createDumpForJava(databaseKind, scriptProvider.getDumpFolder(DatabaseVersionHolder
                 .getDatabaseVersion()));
         scriptProvider.markAsDumpRestorable(DatabaseVersionHolder.getDatabaseVersion());
-        if (reindex)
-        {
-            performFullTextIndex(databaseKind);
-        }
-    }
-
-    /**
-     * Performs a full text index because the test database has been migrated.
-     */
-    private final static void performFullTextIndex(final String databaseKind) throws Exception
-    {
-        final BeanFactory factory = getBeanFactory();
-        final FullTextIndexerRunnable fullTextIndexer =
-                new FullTextIndexerRunnable((SessionFactory) factory
-                        .getBean("hibernate-session-factory"), hibernateSearchContext);
-        fullTextIndexer.run();
-    }
-
-    /**
-     * Creates a freshly new {@link HibernateSearchContext} overriding the one loaded by <i>Spring</i>.
-     */
-    private final static HibernateSearchContext createHibernateSearchContext()
-    {
-        final HibernateSearchContext context = new HibernateSearchContext();
-        context.setIndexBase(LUCENE_INDICES);
-        context.setIndexMode(IndexMode.INDEX_FROM_SCRATCH);
-        return context;
     }
 
     /**
@@ -190,8 +115,18 @@ public final class DatabaseCreationUtil
         return configurationContext;
     }
 
-    private DatabaseCreationUtil()
+    //
+    // Main method
+    //
+
+    public static void main(final String[] args) throws Exception
     {
-        // This class can not be instantiated.
+        if (args.length != 1)
+        {
+            System.out.println("Usage: java " + DatabaseCreationUtil.class.getName()
+                    + "<database version>");
+            System.exit(1);
+        }
+        createFilesFromADumpOfAMigratedDatabase(args[0]);
     }
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/IndexCreationUtil.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/IndexCreationUtil.java
new file mode 100644
index 00000000000..f3ccc45591f
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/IndexCreationUtil.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2008 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.server.dataaccess.db;
+
+import org.hibernate.SessionFactory;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import ch.systemsx.cisd.common.logging.LogInitializer;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.FullTextIndexerRunnable;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.HibernateSearchContext;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.IndexMode;
+
+/**
+ * Utility methods around database indexing with <i>Hibernate</i>.
+ * 
+ * @author Christian Ribeaud
+ */
+public final class IndexCreationUtil
+{
+    private static final String LUCENE_INDICES = "sourceTest/lucene/indices";
+
+    static
+    {
+        LogInitializer.init();
+        // Deactivate the indexing in the application context loaded by Spring.
+        System.setProperty("hibernate.search.index-mode", "NO_INDEX");
+        System.setProperty("hibernate.search.index-base", LUCENE_INDICES);
+    }
+
+    private static HibernateSearchContext hibernateSearchContext;
+
+    private static BeanFactory beanFactory;
+
+    private IndexCreationUtil()
+    {
+        // Can not be instantiated.
+    }
+
+    private final static BeanFactory getBeanFactory()
+    {
+        if (beanFactory == null)
+        {
+            final AbstractApplicationContext applicationContext =
+                    new ClassPathXmlApplicationContext(new String[]
+                        { "applicationContext.xml" }, true);
+            IndexCreationUtil.beanFactory = applicationContext;
+        }
+        return beanFactory;
+    }
+
+    /**
+     * Performs a full text index because the test database has been migrated.
+     */
+    private final static void performFullTextIndex() throws Exception
+    {
+        final BeanFactory factory = getBeanFactory();
+        final FullTextIndexerRunnable fullTextIndexer =
+                new FullTextIndexerRunnable((SessionFactory) factory
+                        .getBean("hibernate-session-factory"), hibernateSearchContext);
+        fullTextIndexer.run();
+    }
+
+    /**
+     * Creates a freshly new {@link HibernateSearchContext} overriding the one loaded by <i>Spring</i>.
+     */
+    private final static HibernateSearchContext createHibernateSearchContext()
+    {
+        final HibernateSearchContext context = new HibernateSearchContext();
+        context.setIndexBase(LUCENE_INDICES);
+        context.setIndexMode(IndexMode.INDEX_FROM_SCRATCH);
+        return context;
+    }
+
+    //
+    // Main method
+    //
+
+    public static void main(final String[] args) throws Exception
+    {
+        if (args.length != 1)
+        {
+            System.out.println("Usage: java " + DatabaseCreationUtil.class.getName()
+                    + "<database kind>");
+            System.exit(1);
+        }
+        System.setProperty("database.kind", args[0]);
+        hibernateSearchContext = createHibernateSearchContext();
+        hibernateSearchContext.afterPropertiesSet();
+        performFullTextIndex();
+    }
+
+}
-- 
GitLab