You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ch...@apache.org on 2013/01/23 17:50:24 UTC

svn commit: r1437560 - in /airavata/trunk/tools: registry-migrate/ registry-tool/ registry-tool/src/ registry-tool/src/main/ registry-tool/src/main/java/ registry-tool/src/main/java/org/ registry-tool/src/main/java/org/apache/ registry-tool/src/main/ja...

Author: chathuri
Date: Wed Jan 23 16:50:23 2013
New Revision: 1437560

URL: http://svn.apache.org/viewvc?rev=1437560&view=rev
Log:
rename registry-migrate to registry-tool

Added:
    airavata/trunk/tools/registry-tool/
    airavata/trunk/tools/registry-tool/pom.xml
    airavata/trunk/tools/registry-tool/src/
    airavata/trunk/tools/registry-tool/src/main/
    airavata/trunk/tools/registry-tool/src/main/java/
    airavata/trunk/tools/registry-tool/src/main/java/org/
    airavata/trunk/tools/registry-tool/src/main/java/org/apache/
    airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/
    airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/
    airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/
    airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/DBMigrator.java
    airavata/trunk/tools/registry-tool/src/main/resources/
    airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh   (with props)
    airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/
    airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/
    airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_derby.sql
    airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_mysql.sql
Removed:
    airavata/trunk/tools/registry-migrate/

Added: airavata/trunk/tools/registry-tool/pom.xml
URL: http://svn.apache.org/viewvc/airavata/trunk/tools/registry-tool/pom.xml?rev=1437560&view=auto
==============================================================================
--- airavata/trunk/tools/registry-tool/pom.xml (added)
+++ airavata/trunk/tools/registry-tool/pom.xml Wed Jan 23 16:50:23 2013
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--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. -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <parent>
+        <groupId>org.apache.airavata</groupId>
+        <artifactId>airavata-tools-parent</artifactId>
+        <version>0.6-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.apache.airavata</groupId>
+    <artifactId>registry-tool</artifactId>
+    <packaging>jar</packaging>
+    <name>registry-tool</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.airavata</groupId>
+            <artifactId>airavata-registry-api</artifactId>
+            <version>0.6-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>5.1.6</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.derby</groupId>
+            <artifactId>derby</artifactId>
+            <version>10.9.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.derby</groupId>
+            <artifactId>derbyclient</artifactId>
+            <version>10.9.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.derby</groupId>
+            <artifactId>derbynet</artifactId>
+            <version>10.9.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.derby</groupId>
+            <artifactId>derbytools</artifactId>
+            <version>10.9.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>jcl-over-slf4j</artifactId>
+            <version>1.6.1</version>
+        </dependency>
+    </dependencies>
+</project>

Added: airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/DBMigrator.java
URL: http://svn.apache.org/viewvc/airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/DBMigrator.java?rev=1437560&view=auto
==============================================================================
--- airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/DBMigrator.java (added)
+++ airavata/trunk/tools/registry-tool/src/main/java/org/apache/airavata/registry/tool/DBMigrator.java Wed Jan 23 16:50:23 2013
@@ -0,0 +1,276 @@
+/*
+ *
+ * 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.airavata.registry.tool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.net.URI;
+import java.sql.*;
+import java.util.*;
+import java.util.Date;
+
+public class DBMigrator {
+    private static final Logger logger = LoggerFactory.getLogger(DBMigrator.class);
+    private static final String delimiter = ";";
+    private static final String MIGRATE_SQL_DERBY = "migrate_derby.sql";
+    private static final String MIGRATE_SQL_MYSQL = "migrate_mysql.sql";
+    private static final String REGISTRY_VERSION = "registry.version";
+    private static String currentAiravataVersion = "0.5";
+    private static final String RELATIVE_PATH = "db-scripts/0.6/";
+    private static final String SELECT_QUERY = "SELECT config_val FROM CONFIGURATION WHERE config_key=' " + REGISTRY_VERSION + "'";
+    private static final String INSERT_QUERY = "INSERT INTO CONFIGURATION (config_key, config_val, expire_date, category_id) VALUES('" +
+            REGISTRY_VERSION + "', '" + getIncrementedVersion(currentAiravataVersion) + "', '" + getCurrentDate() +
+            "','SYSTEM')";
+
+
+    public static void main(String[] args) {
+         updateDB("jdbc:mysql://localhost:3306/persistent_data",
+                 "airavata",
+                 null);
+    }
+
+    //we assume given database is up and running
+    public static void updateDB (String jdbcUrl, String jdbcUser, String jdbcPwd){
+        InputStream sqlStream = null;
+        Scanner in = new Scanner(System.in);
+        if (jdbcPwd == null || jdbcPwd.equals("")){
+            System.out.println("Enter JDBC password : ");
+            jdbcPwd = in.next();
+        }
+        String dbType = getDBType(jdbcUrl);
+        String jdbcDriver = null;
+
+        Connection connection;
+        try {
+            if (dbType.contains("derby")){
+                jdbcDriver = "org.apache.derby.jdbc.ClientDriver";
+                sqlStream = DBMigrator.class.getClassLoader().getResourceAsStream(RELATIVE_PATH + MIGRATE_SQL_DERBY);
+            } else if (dbType.contains("mysql")){
+                jdbcDriver = "com.mysql.jdbc.Driver";
+                sqlStream = DBMigrator.class.getClassLoader().getResourceAsStream(RELATIVE_PATH + MIGRATE_SQL_MYSQL);
+            }
+            Class.forName(jdbcDriver).newInstance();
+            connection = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPwd);
+            if (canUpdated(connection)){
+                executeSQLScript(connection, sqlStream);
+                //update configuration table with airavata version
+                updateConfigTable(connection);
+            }
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (InstantiationException e) {
+            e.printStackTrace();
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static boolean canUpdated (Connection conn){
+        String config = executeSelectQuery(conn);
+        if (config != null) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    private static void updateConfigTable (Connection connection){
+        executeInsertQuery(connection);
+    }
+
+    private static Timestamp getCurrentDate (){
+        Calendar cal = Calendar.getInstance();
+        Date date = cal.getTime();
+        Timestamp d = new Timestamp(date.getTime());
+        return d;
+    }
+
+    private static String getIncrementedVersion (String currentVersion){
+        Double currentVer = Double.valueOf(currentVersion);
+        return String.valueOf(currentVer + 0.1);
+    }
+
+    private static String executeSelectQuery (Connection conn){
+        try {
+            Statement statement = conn.createStatement();
+            ResultSet rs = statement.executeQuery(SELECT_QUERY);
+            if (rs != null){
+                while (rs.next()) {
+                    currentAiravataVersion = rs.getString(1);
+                    return currentAiravataVersion;
+                }
+            }
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    private static void executeInsertQuery (Connection conn){
+        try {
+            Statement statement = conn.createStatement();
+            statement.execute(INSERT_QUERY) ;
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void executeSQLScript(Connection conn, InputStream inputStream) throws Exception {
+        StringBuffer sql = new StringBuffer();
+        BufferedReader reader = null;
+        try{
+            reader = new BufferedReader(new InputStreamReader(inputStream));
+            String line;
+            while ((line = reader.readLine()) != null) {
+                line = line.trim();
+                if (line.startsWith("//")) {
+                    continue;
+                }
+                if (line.startsWith("--")) {
+                    continue;
+                }
+                StringTokenizer st = new StringTokenizer(line);
+                if (st.hasMoreTokens()) {
+                    String token = st.nextToken();
+                    if ("REM".equalsIgnoreCase(token)) {
+                        continue;
+                    }
+                }
+                sql.append(" ").append(line);
+
+                // SQL defines "--" as a comment to EOL
+                // and in Oracle it may contain a hint
+                // so we cannot just remove it, instead we must end it
+                if (line.indexOf("--") >= 0) {
+                    sql.append("\n");
+                }
+                if ((checkStringBufferEndsWith(sql, delimiter))) {
+                    String sqlString = sql.substring(0, sql.length() - delimiter.length());
+                    executeSQL(sqlString, conn);
+                    sql.replace(0, sql.length(), "");
+                }
+            }
+            // Catch any statements not followed by ;
+            if (sql.length() > 0) {
+                executeSQL(sql.toString(), conn);
+            }
+        }catch (IOException e){
+            logger.error("Error occurred while executing SQL script for creating Airavata database", e);
+            throw new Exception("Error occurred while executing SQL script for creating Airavata database", e);
+        }finally {
+            if (reader != null) {
+                reader.close();
+            }
+
+        }
+    }
+
+    private static String getDBType(String jdbcURL){
+        try{
+            String cleanURI = jdbcURL.substring(5);
+            URI uri = URI.create(cleanURI);
+            return uri.getScheme();
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            return null;
+        }
+    }
+
+    public static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) {
+        if (suffix.length() > buffer.length()) {
+            return false;
+        }
+        // this loop is done on purpose to avoid memory allocation performance
+        // problems on various JDKs
+        // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
+        // implementation is ok though does allocation/copying
+        // StringBuffer.toString().endsWith() does massive memory
+        // allocation/copying on JDK 1.5
+        // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
+        int endIndex = suffix.length() - 1;
+        int bufferIndex = buffer.length() - 1;
+        while (endIndex >= 0) {
+            if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
+                return false;
+            }
+            bufferIndex--;
+            endIndex--;
+        }
+        return true;
+    }
+
+    private static void executeSQL(String sql, Connection conn) throws Exception {
+        if ("".equals(sql.trim())) {
+            return;
+        }
+        Statement statement = null;
+        try {
+            logger.debug("SQL : " + sql);
+
+            boolean ret;
+            int updateCount = 0, updateCountTotal = 0;
+            statement = conn.createStatement();
+            ret = statement.execute(sql);
+            updateCount = statement.getUpdateCount();
+            do {
+                if (!ret) {
+                    if (updateCount != -1) {
+                        updateCountTotal += updateCount;
+                    }
+                }
+                ret = statement.getMoreResults();
+                if (ret) {
+                    updateCount = statement.getUpdateCount();
+                }
+            } while (ret);
+
+            logger.debug(sql + " : " + updateCountTotal + " rows affected");
+
+            SQLWarning warning = conn.getWarnings();
+            while (warning != null) {
+                logger.warn(warning + " sql warning");
+                warning = warning.getNextWarning();
+            }
+            conn.clearWarnings();
+        } catch (SQLException e) {
+            if (e.getSQLState().equals("X0Y32")) {
+                logger.info("Table Already Exists", e);
+            } else {
+                throw new Exception("Error occurred while executing : " + sql, e);
+            }
+        } finally {
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    logger.error("Error occurred while closing result set.", e);
+                }
+            }
+        }
+    }
+}

Added: airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh
URL: http://svn.apache.org/viewvc/airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh?rev=1437560&view=auto
==============================================================================
--- airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh (added)
+++ airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh Wed Jan 23 16:50:23 2013
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+# 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.
+
+. `dirname $0`/setenv.sh
+cd $AIRAVATA_HOME/bin
+
+# update classpath
+REG_MIGRATE_CLASSPATH="$AIRAVATA_HOME/lib"
+for f in $AIRAVATA_HOME/lib/*.jar
+do
+  REG_MIGRATE_CLASSPATH=$REG_MIGRATE_CLASSPATH:$f
+done
+
+$JAVA_HOME/bin/java -server -Xms128M -Xmx128M \
+   $XDEBUG \
+   $TEMP_PROPS \
+   -Djava.endorsed.dirs=$AIRAVATA_HOME/lib/endorsed \
+   -classpath $REG_MIGRATE_CLASSPATH \
+   -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5000,suspend=n \
+   org.apache.airavata.registry.tool.DBMigrator $*

Propchange: airavata/trunk/tools/registry-tool/src/main/resources/db-migrate.sh
------------------------------------------------------------------------------
    svn:executable = *

Added: airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_derby.sql
URL: http://svn.apache.org/viewvc/airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_derby.sql?rev=1437560&view=auto
==============================================================================
--- airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_derby.sql (added)
+++ airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_derby.sql Wed Jan 23 16:50:23 2013
@@ -0,0 +1,12 @@
+ALTER TABLE Configuration ADD category_id varchar(255) NOT NULL DEFAULT 'SYSTEM';
+
+ALTER TABLE Configuration DROP PRIMARY KEY;
+
+ALTER TABLE Configuration ADD PRIMARY KEY(config_key, config_val, category_id);
+
+ALTER TABLE Node_Data
+ADD execution_index int NOT NULL DEFAULT 0;
+
+ALTER TABLE Node_Data DROP PRIMARY KEY;
+
+ALTER TABLE Node_Data ADD PRIMARY KEY(workflow_instanceID, node_id, execution_index);
\ No newline at end of file

Added: airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_mysql.sql
URL: http://svn.apache.org/viewvc/airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_mysql.sql?rev=1437560&view=auto
==============================================================================
--- airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_mysql.sql (added)
+++ airavata/trunk/tools/registry-tool/src/main/resources/db-scripts/0.6/migrate_mysql.sql Wed Jan 23 16:50:23 2013
@@ -0,0 +1,11 @@
+ALTER TABLE Configuration
+ADD category_id varchar(255);
+
+UPDATE Configuration SET category_id="SYSTEM" ;
+
+ALTER TABLE Configuration DROP PRIMARY KEY, ADD PRIMARY KEY(config_key, config_val, category_id);
+
+ALTER TABLE Node_Data
+ADD execution_index int NOT NULL;
+
+ALTER TABLE Node_Data DROP PRIMARY KEY, ADD PRIMARY KEY(workflow_instanceID, node_id, execution_index);
\ No newline at end of file