Author: rwatler
Date: Mon May 10 06:11:49 2010
New Revision: 942663

URL: http://svn.apache.org/viewvc?rev=942663&view=rev
Log:
JS2-1127: bulk 2.1.[34] and 2.2.0 to 2.2.[12] migration capability

Added:
    
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/JetspeedCapabilitiesMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigrationApplication.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/SQLScriptReader.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-page-manager/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-page-manager/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-page-manager/src/main/java/org/apache/jetspeed/tools/migration/JetspeedDBPageManagerMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-profiler/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-profiler/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-profiler/src/main/java/org/apache/jetspeed/tools/migration/JetspeedProfilerMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-registry/src/main/java/org/apache/jetspeed/tools/migration/JetspeedRegistryMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/tools/migration/JetspeedSecurityMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-sso/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-sso/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-sso/src/main/java/org/apache/jetspeed/tools/migration/JetspeedSSOSecurityMigration.java
    
portals/jetspeed-2/portal/trunk/components/jetspeed-statistics/src/main/java/org/apache/jetspeed/tools/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-statistics/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/components/jetspeed-statistics/src/main/java/org/apache/jetspeed/tools/migration/JetspeedStatisticsMigration.java
    
portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/tools/migration/
    
portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigration.java
    portals/jetspeed-2/portal/trunk/jetspeed-installer/etc/database/lib/
    
portals/jetspeed-2/portal/trunk/jetspeed-installer/etc/database/lib/log4j.xml
Modified:
    portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/pom.xml
    
portals/jetspeed-2/portal/trunk/jetspeed-ant-tasks/src/main/java/org/apache/jetspeed/anttasks/AntInstallerCheckConnection.java
    portals/jetspeed-2/portal/trunk/jetspeed-installer/   (props changed)
    
portals/jetspeed-2/portal/trunk/jetspeed-installer/etc/ant-installer/antinstall-config.xml
    
portals/jetspeed-2/portal/trunk/jetspeed-installer/etc/ant-installer/build.xml
    portals/jetspeed-2/portal/trunk/jetspeed-installer/etc/database/build.xml

Added: 
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/JetspeedCapabilitiesMigration.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/JetspeedCapabilitiesMigration.java?rev=942663&view=auto
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/JetspeedCapabilitiesMigration.java
 (added)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-capability/src/main/java/org/apache/jetspeed/tools/migration/JetspeedCapabilitiesMigration.java
 Mon May 10 06:11:49 2010
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.tools.migration;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Jetspeed Migration for Capabilities component.
+ * 
+ * @author <a href="mailto:rwat...@apache.org";>Randy Watler</a>
+ * @version $Id$
+ */
+public class JetspeedCapabilitiesMigration implements JetspeedMigration
+{
+    /* (non-Javadoc)
+     * @see org.apache.jetspeed.tools.migration.JetspeedMigration#getName()
+     */
+    public String getName()
+    {
+        return "Capabilities";
+    }
+    
+    /* (non-Javadoc)
+     * @see 
org.apache.jetspeed.tools.migration.JetspeedMigration#detectSourceVersion(java.sql.Connection,
 int)
+     */
+    public int detectSourceVersion(Connection sourceConnection, int 
sourceVersion) throws SQLException
+    {
+        // no migration required in capabilities schema
+        return ((sourceVersion > JETSPEED_SCHEMA_VERSION_UNKNOWN) ? 
sourceVersion : JETSPEED_SCHEMA_VERSION_2_1_3);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.jetspeed.tools.migration.JetspeedMigration#migrate(java.sql.Connection,
 int, java.sql.Connection)
+     */
+    public int migrate(Connection sourceConnection, int sourceVersion, 
Connection targetConnection) throws SQLException
+    {
+        int rowsMigrated = 0;
+        
+        // MEDIA_TYPE
+        PreparedStatement mediaTypeInsertStatement = 
targetConnection.prepareStatement("INSERT INTO MEDIA_TYPE (MEDIATYPE_ID, NAME, 
CHARACTER_SET, TITLE, DESCRIPTION) VALUES (?, ?, ?, ?, ?);");
+        Statement mediaTypeQueryStatement = sourceConnection.createStatement();
+        ResultSet mediaTypeResultSet = 
mediaTypeQueryStatement.executeQuery("SELECT MEDIATYPE_ID, NAME, CHARACTER_SET, 
TITLE, DESCRIPTION FROM MEDIA_TYPE;");
+        while (mediaTypeResultSet.next())
+        {
+            mediaTypeInsertStatement.setInt(1, mediaTypeResultSet.getInt(1));
+            mediaTypeInsertStatement.setString(2, 
mediaTypeResultSet.getString(2));
+            mediaTypeInsertStatement.setString(3, 
mediaTypeResultSet.getString(3));
+            mediaTypeInsertStatement.setString(4, 
mediaTypeResultSet.getString(4));
+            mediaTypeInsertStatement.setString(5, 
mediaTypeResultSet.getString(5));
+            mediaTypeInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        mediaTypeResultSet.close();
+        mediaTypeQueryStatement.close();
+        mediaTypeInsertStatement.close();
+        
+        // CLIENT
+        PreparedStatement clientInsertStatement = 
targetConnection.prepareStatement("INSERT INTO CLIENT (CLIENT_ID, EVAL_ORDER, 
NAME, USER_AGENT_PATTERN, MANUFACTURER, MODEL, VERSION, PREFERRED_MIMETYPE_ID) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
+        Statement clientQueryStatement = sourceConnection.createStatement();
+        ResultSet clientResultSet = clientQueryStatement.executeQuery("SELECT 
CLIENT_ID, EVAL_ORDER, NAME, USER_AGENT_PATTERN, MANUFACTURER, MODEL, VERSION, 
PREFERRED_MIMETYPE_ID FROM CLIENT;");
+        while (clientResultSet.next())
+        {
+            clientInsertStatement.setInt(1, clientResultSet.getInt(1));
+            clientInsertStatement.setInt(2, clientResultSet.getInt(2));
+            clientInsertStatement.setString(3, clientResultSet.getString(3));
+            clientInsertStatement.setString(4, clientResultSet.getString(4));
+            clientInsertStatement.setString(5, clientResultSet.getString(5));
+            clientInsertStatement.setString(6, clientResultSet.getString(6));
+            clientInsertStatement.setString(7, clientResultSet.getString(7));
+            clientInsertStatement.setInt(8, clientResultSet.getInt(8));
+            clientInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        clientResultSet.close();
+        clientQueryStatement.close();
+        clientInsertStatement.close();
+        
+        // MIMETYPE
+        PreparedStatement mimeTypeInsertStatement = 
targetConnection.prepareStatement("INSERT INTO MIMETYPE (MIMETYPE_ID, NAME) 
VALUES (?, ?);");
+        Statement mimeTypeQueryStatement = sourceConnection.createStatement();
+        ResultSet mimeTypeResultSet = 
mimeTypeQueryStatement.executeQuery("SELECT MIMETYPE_ID, NAME FROM MIMETYPE;");
+        while (mimeTypeResultSet.next())
+        {
+            mimeTypeInsertStatement.setInt(1, mimeTypeResultSet.getInt(1));
+            mimeTypeInsertStatement.setString(2, 
mimeTypeResultSet.getString(2));
+            mimeTypeInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        mimeTypeResultSet.close();
+        mimeTypeQueryStatement.close();
+        mimeTypeInsertStatement.close();
+        
+        // CAPABILITY
+        PreparedStatement capabilityInsertStatement = 
targetConnection.prepareStatement("INSERT INTO CAPABILITY (CAPABILITY_ID, 
CAPABILITY) VALUES (?, ?);");
+        Statement capabilityQueryStatement = 
sourceConnection.createStatement();
+        ResultSet capabilityResultSet = 
capabilityQueryStatement.executeQuery("SELECT CAPABILITY_ID, CAPABILITY FROM 
CAPABILITY;");
+        while (capabilityResultSet.next())
+        {
+            capabilityInsertStatement.setInt(1, capabilityResultSet.getInt(1));
+            capabilityInsertStatement.setString(2, 
capabilityResultSet.getString(2));
+            capabilityInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        capabilityResultSet.close();
+        capabilityQueryStatement.close();
+        capabilityInsertStatement.close();
+
+        // CLIENT_TO_CAPABILITY
+        PreparedStatement clientToCapabilityInsertStatement = 
targetConnection.prepareStatement("INSERT INTO CLIENT_TO_CAPABILITY (CLIENT_ID, 
CAPABILITY_ID) VALUES (?, ?);");
+        Statement clientToCapabilityQueryStatement = 
sourceConnection.createStatement();
+        ResultSet clientToCapabilityResultSet = 
clientToCapabilityQueryStatement.executeQuery("SELECT CLIENT_ID, CAPABILITY_ID 
FROM CLIENT_TO_CAPABILITY;");
+        while (clientToCapabilityResultSet.next())
+        {
+            clientToCapabilityInsertStatement.setInt(1, 
clientToCapabilityResultSet.getInt(1));
+            clientToCapabilityInsertStatement.setInt(2, 
clientToCapabilityResultSet.getInt(2));
+            clientToCapabilityInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        clientToCapabilityResultSet.close();
+        clientToCapabilityQueryStatement.close();
+        clientToCapabilityInsertStatement.close();
+
+        // CLIENT_TO_MIMETYPE
+        PreparedStatement clientToMimeTypeInsertStatement = 
targetConnection.prepareStatement("INSERT INTO CLIENT_TO_MIMETYPE (CLIENT_ID, 
MIMETYPE_ID) VALUES (?, ?);");
+        Statement clientToMimeTypeQueryStatement = 
sourceConnection.createStatement();
+        ResultSet clientToMimeTypeResultSet = 
clientToMimeTypeQueryStatement.executeQuery("SELECT CLIENT_ID, MIMETYPE_ID FROM 
CLIENT_TO_MIMETYPE;");
+        while (clientToMimeTypeResultSet.next())
+        {
+            clientToMimeTypeInsertStatement.setInt(1, 
clientToMimeTypeResultSet.getInt(1));
+            clientToMimeTypeInsertStatement.setInt(2, 
clientToMimeTypeResultSet.getInt(2));
+            clientToMimeTypeInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        clientToMimeTypeResultSet.close();
+        clientToMimeTypeQueryStatement.close();
+        clientToMimeTypeInsertStatement.close();
+
+        // MEDIATYPE_TO_CAPABILITY
+        PreparedStatement mediaTypeToCapabilityInsertStatement = 
targetConnection.prepareStatement("INSERT INTO MEDIATYPE_TO_CAPABILITY 
(MEDIATYPE_ID, CAPABILITY_ID) VALUES (?, ?);");
+        Statement mediaTypeToCapabilityQueryStatement = 
sourceConnection.createStatement();
+        ResultSet mediaTypeToCapabilityResultSet = 
mediaTypeToCapabilityQueryStatement.executeQuery("SELECT MEDIATYPE_ID, 
CAPABILITY_ID FROM MEDIATYPE_TO_CAPABILITY;");
+        while (mediaTypeToCapabilityResultSet.next())
+        {
+            mediaTypeToCapabilityInsertStatement.setInt(1, 
mediaTypeToCapabilityResultSet.getInt(1));
+            mediaTypeToCapabilityInsertStatement.setInt(2, 
mediaTypeToCapabilityResultSet.getInt(2));
+            mediaTypeToCapabilityInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        mediaTypeToCapabilityResultSet.close();
+        mediaTypeToCapabilityQueryStatement.close();
+        mediaTypeToCapabilityInsertStatement.close();
+
+        // MEDIATYPE_TO_MIMETYPE
+        PreparedStatement mediaTypeToMimeTypeInsertStatement = 
targetConnection.prepareStatement("INSERT INTO MEDIATYPE_TO_MIMETYPE 
(MEDIATYPE_ID, MIMETYPE_ID) VALUES (?, ?);");
+        Statement mediaTypeToMimeTypeQueryStatement = 
sourceConnection.createStatement();
+        ResultSet mediaTypeToMimeTypeResultSet = 
mediaTypeToMimeTypeQueryStatement.executeQuery("SELECT MEDIATYPE_ID, 
MIMETYPE_ID FROM MEDIATYPE_TO_MIMETYPE;");
+        while (mediaTypeToMimeTypeResultSet.next())
+        {
+            mediaTypeToMimeTypeInsertStatement.setInt(1, 
mediaTypeToMimeTypeResultSet.getInt(1));
+            mediaTypeToMimeTypeInsertStatement.setInt(2, 
mediaTypeToMimeTypeResultSet.getInt(2));
+            mediaTypeToMimeTypeInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        mediaTypeToMimeTypeResultSet.close();
+        mediaTypeToMimeTypeQueryStatement.close();
+        mediaTypeToMimeTypeInsertStatement.close();
+        
+        // OJB_HL_SEQ
+        PreparedStatement ojbInsertStatement = 
targetConnection.prepareStatement("INSERT INTO OJB_HL_SEQ (TABLENAME, 
FIELDNAME, MAX_KEY, GRAB_SIZE, VERSION) VALUES (?, ?, ?, ?, ?);");
+        Statement ojbQueryStatement = sourceConnection.createStatement();
+        ResultSet ojbResultSet = ojbQueryStatement.executeQuery("SELECT 
TABLENAME, FIELDNAME, MAX_KEY, GRAB_SIZE, VERSION FROM OJB_HL_SEQ WHERE 
TABLENAME IN ('SEQ_CAPABILITY', 'SEQ_CLIENT', 'SEQ_MEDIA_TYPE', 
'SEQ_MIMETYPE');");
+        while (ojbResultSet.next())
+        {
+            ojbInsertStatement.setString(1, ojbResultSet.getString(1));
+            ojbInsertStatement.setString(2, ojbResultSet.getString(2));
+            ojbInsertStatement.setInt(3, ojbResultSet.getInt(3));
+            ojbInsertStatement.setInt(4, ojbResultSet.getInt(4));
+            ojbInsertStatement.setInt(5, ojbResultSet.getInt(5));
+            ojbInsertStatement.executeUpdate();
+            rowsMigrated++;
+        }
+        ojbResultSet.close();
+        ojbQueryStatement.close();
+        ojbInsertStatement.close();
+        
+        return rowsMigrated;
+    }
+}

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/pom.xml
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/pom.xml?rev=942663&r1=942662&r2=942663&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/pom.xml 
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/pom.xml Mon 
May 10 06:11:49 2010
@@ -124,6 +124,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.portals.jetspeed-2</groupId>
+      <artifactId>jetspeed-statistics</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.portals.jetspeed-2</groupId>
       <artifactId>jetspeed-serializer</artifactId>
       <exclusions>
         <exclusion>

Added: 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigrationApplication.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigrationApplication.java?rev=942663&view=auto
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigrationApplication.java
 (added)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/JetspeedMigrationApplication.java
 Mon May 10 06:11:49 2010
@@ -0,0 +1,313 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.tools.migration;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.regex.Pattern;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.jetspeed.components.datasource.DBCPDatasourceComponent;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Jetspeed Migration application.
+ * 
+ * @author <a href="mailto:rwat...@apache.org";>Randy Watler</a>
+ * @version $Id$
+ */
+public class JetspeedMigrationApplication
+{
+    private static final Logger log = 
LoggerFactory.getLogger(JetspeedMigrationApplication.class);
+    
+    private String sourceDBUsername;
+    private String sourceDBPassword;
+    private String sourceJDBCUrl;
+    private String sourceJDBCDriverClass;
+    private String dbUsername;
+    private String dbPassword;
+    private String jdbcUrl;
+    private String jdbcDriverClass;
+    private File dropSchemaSQLScriptFile;
+    private File createSchemaSQLScriptFile;
+    private DBCPDatasourceComponent sourceDataSourceFactory;
+    private DBCPDatasourceComponent targetDataSourceFactory;
+    private JetspeedMigration [] migrations;
+    
+    /**
+     * Construct application instance using arguments.
+     * 
+     * @param args application arguments
+     */
+    public JetspeedMigrationApplication(String[] args)
+    {
+        for (String arg : args)
+        {
+            if (arg.startsWith("-source-db-username="))
+            {
+                this.sourceDBUsername = arg.substring(20);
+            }
+            else if (arg.startsWith("-source-db-password="))
+            {
+                this.sourceDBPassword = arg.substring(20);
+            }
+            else if (arg.startsWith("-source-jdbc-url="))
+            {
+                this.sourceJDBCUrl = arg.substring(17);
+            }
+            else if (arg.startsWith("-source-jdbc-driver-class="))
+            {
+                this.sourceJDBCDriverClass = arg.substring(26);
+            }
+            else if (arg.startsWith("-db-username="))
+            {
+                this.dbUsername = arg.substring(13);
+            }
+            else if (arg.startsWith("-db-password="))
+            {
+                this.dbPassword = arg.substring(13);
+            }
+            else if (arg.startsWith("-jdbc-url="))
+            {
+                this.jdbcUrl = arg.substring(10);
+            }
+            else if (arg.startsWith("-jdbc-driver-class="))
+            {
+                this.jdbcDriverClass = arg.substring(19);
+            }
+            else if (arg.startsWith("-drop-schema-sql="))
+            {
+                this.dropSchemaSQLScriptFile = new File(arg.substring(17));
+                if (!this.dropSchemaSQLScriptFile.isFile())
+                {
+                    throw new RuntimeException("Cannot access -drop-schema-sql 
file: "+this.dropSchemaSQLScriptFile);
+                }
+            }
+            else if (arg.startsWith("-create-schema-sql="))
+            {
+                this.createSchemaSQLScriptFile = new File(arg.substring(19));
+                if (!this.createSchemaSQLScriptFile.isFile())
+                {
+                    throw new RuntimeException("Cannot access 
-create-schema-sql file: "+this.createSchemaSQLScriptFile);
+                }
+            }
+        }
+        
+        if (this.sourceDBUsername == null)
+        {
+            throw new RuntimeException("Missing -source-db-username argument");
+        }
+        if (this.sourceDBPassword == null)
+        {
+            throw new RuntimeException("Missing -source-db-password argument");
+        }
+        if (this.sourceJDBCUrl == null)
+        {
+            throw new RuntimeException("Missing -source-jdbc-url argument");
+        }
+        if (this.sourceJDBCDriverClass == null)
+        {
+            throw new RuntimeException("Missing -source-jdbc-driver-class 
argument");
+        }
+        if (this.dbUsername == null)
+        {
+            throw new RuntimeException("Missing -db-username argument");
+        }
+        if (this.dbPassword == null)
+        {
+            throw new RuntimeException("Missing -db-password argument");
+        }
+        if (this.jdbcUrl == null)
+        {
+            throw new RuntimeException("Missing -jdbc-url argument");
+        }
+        if (this.jdbcDriverClass == null)
+        {
+            throw new RuntimeException("Missing -jdbc-driver-class argument");
+        }
+        if (this.dropSchemaSQLScriptFile == null)
+        {
+            throw new RuntimeException("Missing -drop-schema-sql argument");
+        }
+        if (this.createSchemaSQLScriptFile == null)
+        {
+            throw new RuntimeException("Missing -create-schema-sql argument");
+        }
+        
+        if (this.jdbcUrl.equals(this.sourceJDBCUrl))
+        {
+            throw new RuntimeException("Source and target JDBC databases must 
be different: "+this.jdbcUrl);
+        }
+
+        sourceDataSourceFactory = new 
DBCPDatasourceComponent(this.sourceDBUsername, this.sourceDBPassword, 
this.sourceJDBCDriverClass, this.sourceJDBCUrl, 2, 0, 
GenericObjectPool.WHEN_EXHAUSTED_GROW, true);
+        targetDataSourceFactory = new DBCPDatasourceComponent(this.dbUsername, 
this.dbPassword, this.jdbcDriverClass, this.jdbcUrl, 2, 0, 
GenericObjectPool.WHEN_EXHAUSTED_GROW, true);
+        
+        migrations = new JetspeedMigration[]{new 
JetspeedCapabilitiesMigration(),
+                                             new JetspeedStatisticsMigration(),
+                                             new 
JetspeedDBPageManagerMigration(),
+                                             new JetspeedProfilerMigration(),
+                                             new JetspeedRegistryMigration(),
+                                             new JetspeedSecurityMigration(),
+                                             new 
JetspeedSSOSecurityMigration()};
+    }
+    
+    /**
+     * Perform application migration operation.
+     * 
+     * @throws IOException when migration error is encountered
+     * @throws SQLException when SQL migration error is encountered
+     */
+    public void run() throws IOException, SQLException
+    {
+        // start data source pools
+        sourceDataSourceFactory.start();
+        targetDataSourceFactory.start();
+        DataSource sourceDataSource = sourceDataSourceFactory.getDatasource();
+        DataSource targetDataSource = targetDataSourceFactory.getDatasource();
+        
+        // open connections
+        Connection sourceConnection = sourceDataSource.getConnection();
+        Connection targetConnection = targetDataSource.getConnection();
+        
+        // clean target database
+        log.info("Clean target database...");
+        executeSQLScript(targetConnection, dropSchemaSQLScriptFile, true);
+        // create tables and indices in target database
+        log.info("Initialize target database schema tables and indices...");
+        executeSQLScript(targetConnection, createSchemaSQLScriptFile, false, 
"^\\s*create\\s+(?:table|index|unique\\s+index)\\s", true);
+
+        // determine and validate schema version
+        int sourceVersion = JetspeedMigration.JETSPEED_SCHEMA_VERSION_UNKNOWN;
+        for (JetspeedMigration migration : migrations)
+        {
+            sourceVersion = migration.detectSourceVersion(sourceConnection, 
sourceVersion);
+        }
+        for (JetspeedMigration migration : migrations)
+        {
+            sourceVersion = migration.detectSourceVersion(sourceConnection, 
sourceVersion);
+        }
+        log.info("Detected source schema version: "+sourceVersion);
+
+        // migrate data from source to target database
+        for (JetspeedMigration migration : migrations)
+        {
+            log.info("Migrating "+migration.getName()+" data...");
+            int rowsMigrated = migration.migrate(sourceConnection, 
sourceVersion, targetConnection);
+            log.info("Migrated "+rowsMigrated+" "+migration.getName()+" data 
rows.");
+        }
+
+        // add constraints and remaining schema to target database
+        log.info("Setup target database schema constraints...");
+        executeSQLScript(targetConnection, createSchemaSQLScriptFile, false, 
"^\\s*create\\s+(?:table|index|unique\\s+index)\\s", false);
+
+        // close connections
+        sourceConnection.close();
+        targetConnection.close();
+
+        // stop data source pools
+        sourceDataSourceFactory.stop();
+        targetDataSourceFactory.stop();
+    }
+    
+    /**
+     * Execute SQL script statements.
+     * 
+     * @param connection database connection
+     * @param sqlScriptFile SQL script file
+     * @param ignoreSQLErrors ignore SQL errors
+     * @throws IOException when script read error is encountered
+     * @throws SQLException when SQL statement error is encountered
+     */
+    private void executeSQLScript(Connection connection, File sqlScriptFile, 
boolean ignoreSQLErrors) throws IOException, SQLException
+    {
+        executeSQLScript(connection, sqlScriptFile, ignoreSQLErrors, null, 
true);
+    }
+
+    /**
+     * Execute SQL script statements that match specified regexp.
+     * 
+     * @param connection database connection
+     * @param sqlScriptFile SQL script file
+     * @param ignoreSQLErrors ignore SQL errors
+     * @param statementsRegexp statements matching regexp or null
+     * @param include include matching statements
+     * @throws IOException when script read error is encountered
+     * @throws SQLException when SQL statement error is encountered
+     */
+    private void executeSQLScript(Connection connection, File sqlScriptFile, 
boolean ignoreSQLErrors, String statementsRegexp, boolean include) throws 
IOException, SQLException
+    {
+        Pattern statementsPattern = ((statementsRegexp != null) ? 
Pattern.compile(statementsRegexp, Pattern.CASE_INSENSITIVE) : null);
+        
+        SQLScriptReader reader = new SQLScriptReader(sqlScriptFile);
+        for (;;)
+        {
+            String scriptStatement = reader.readSQLStatement();
+            if (scriptStatement != null)
+            {
+                if ((statementsPattern == null) || 
(statementsPattern.matcher(scriptStatement).find() == include))
+                {
+                    Statement statement = connection.createStatement();
+                    if (ignoreSQLErrors)
+                    {
+                        try
+                        {
+                            statement.execute(scriptStatement);                
            
+                        }
+                        catch (SQLException sqle)
+                        {
+                        }
+                    }
+                    else
+                    {
+                        statement.execute(scriptStatement);
+                    }
+                }
+            }
+            else
+            {
+                break;
+            }
+        }
+    }
+    
+    /**
+     * Application main entry point.
+     * 
+     * @param args application arguments
+     */
+    public static void main(String[] args)
+    {
+        try
+        {
+            JetspeedMigrationApplication application = new 
JetspeedMigrationApplication(args);
+            application.run();
+            System.exit(0);
+        }
+        catch (Exception e)
+        {
+            log.error("Unexpected exception: "+e, e);
+            System.exit(-1);
+        }
+    }
+}

Added: 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/SQLScriptReader.java
URL: 
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/SQLScriptReader.java?rev=942663&view=auto
==============================================================================
--- 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/SQLScriptReader.java
 (added)
+++ 
portals/jetspeed-2/portal/trunk/components/jetspeed-db-tools/src/main/java/org/apache/jetspeed/tools/migration/SQLScriptReader.java
 Mon May 10 06:11:49 2010
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.tools.migration;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+/**
+ * SQL Script Reader
+ * 
+ * @author <a href="mailto:rwat...@apache.org";>Randy Watler</a>
+ * @version $Id$
+ */
+public class SQLScriptReader
+{
+    private BufferedReader reader;
+    
+    public SQLScriptReader(File scriptFile) throws FileNotFoundException
+    {
+        this.reader = new BufferedReader(new FileReader(scriptFile));
+    }
+    
+    public String readSQLStatement() throws IOException
+    {
+        StringBuilder sqlStatement = new StringBuilder();
+        boolean comment = false;
+        for (;;)
+        {
+            String line = reader.readLine();
+            if (line != null)
+            {
+                line = line.trim();
+                if (comment)
+                {
+                    comment = !line.endsWith("*/");
+                }
+                else
+                {
+                    comment = line.startsWith("/*");
+                    if (!comment && !line.startsWith("--") && 
!line.startsWith("//") && !line.startsWith("#") && (line.length() > 0))
+                    {
+                        if (sqlStatement.length() > 0)
+                        {
+                            sqlStatement.append(' ');
+                        }
+                        sqlStatement.append(line);
+                        if (line.endsWith(";"))
+                        {
+                            break;
+                        }
+                    }
+                }
+            }
+            else
+            {
+                sqlStatement.setLength(0);
+                break;
+            }
+        }
+        return ((sqlStatement.length() > 0) ? sqlStatement.toString() : null);
+    }
+
+    public void close() throws IOException
+    {
+        reader.close();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscr...@portals.apache.org
For additional commands, e-mail: jetspeed-dev-h...@portals.apache.org

Reply via email to