creating credential store database as a seperate when server start up

Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/f22acb82
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/f22acb82
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/f22acb82

Branch: refs/heads/master
Commit: f22acb82622d81f30118b0ba3adb922f88149f03
Parents: e303bb5
Author: Chathuri Wimalasena <[email protected]>
Authored: Wed Feb 24 11:36:49 2016 -0500
Committer: Chathuri Wimalasena <[email protected]>
Committed: Wed Feb 24 11:36:49 2016 -0500

----------------------------------------------------------------------
 .../airavata/api/server/AiravataAPIServer.java  |  10 +-
 .../server/util/CredentialStoreInitUtil.java    | 156 +++++++++++++++++++
 .../scripts/credential-store-h2.sql             |  42 -----
 .../scripts/credential-store-mysql.sql          |  38 -----
 .../src/main/resources/credstore-derby.sql      |  39 +++++
 .../src/main/resources/credstore-mysql.sql      |  39 +++++
 .../src/main/resources/expcatalog-derby.sql     |  21 +--
 .../src/main/resources/expcatalog-mysql.sql     |  21 +--
 8 files changed, 242 insertions(+), 124 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/AiravataAPIServer.java
----------------------------------------------------------------------
diff --git 
a/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/AiravataAPIServer.java
 
b/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/AiravataAPIServer.java
index d2e50ef..b530bff 100644
--- 
a/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/AiravataAPIServer.java
+++ 
b/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/AiravataAPIServer.java
@@ -30,10 +30,7 @@ import 
org.apache.airavata.api.server.handler.AiravataServerHandler;
 import org.apache.airavata.api.server.security.AiravataSecurityManager;
 import org.apache.airavata.api.server.security.SecurityManagerFactory;
 import org.apache.airavata.api.server.security.interceptor.SecurityModule;
-import org.apache.airavata.api.server.util.AppCatalogInitUtil;
-import org.apache.airavata.api.server.util.Constants;
-import org.apache.airavata.api.server.util.ExperimentCatalogInitUtil;
-import org.apache.airavata.api.server.util.WorkflowCatalogInitUtil;
+import org.apache.airavata.api.server.util.*;
 import org.apache.airavata.common.exception.ApplicationSettingsException;
 import org.apache.airavata.common.utils.IServer;
 import org.apache.airavata.common.utils.ServerSettings;
@@ -67,9 +64,14 @@ public class AiravataAPIServer implements IServer{
        
     public void startAiravataServer(Airavata.Processor<Airavata.Iface> 
airavataAPIServer) throws AiravataSystemException {
         try {
+            // creating experiment catalog db
             ExperimentCatalogInitUtil.initializeDB();
+            // creating app catalog db
             AppCatalogInitUtil.initializeDB();
+            // creating workflow catalog db
             WorkflowCatalogInitUtil.initializeDB();
+            // creating credential store db
+            CredentialStoreInitUtil.initializeDB();
             final String serverHost = 
ServerSettings.getSetting(Constants.API_SERVER_HOST, null);
             if (!ServerSettings.isTLSEnabled()) {
                 final int serverPort = 
Integer.parseInt(ServerSettings.getSetting(Constants.API_SERVER_PORT, "8930"));

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/util/CredentialStoreInitUtil.java
----------------------------------------------------------------------
diff --git 
a/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/util/CredentialStoreInitUtil.java
 
b/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/util/CredentialStoreInitUtil.java
new file mode 100644
index 0000000..20dd1aa
--- /dev/null
+++ 
b/airavata-api/airavata-api-server/src/main/java/org/apache/airavata/api/server/util/CredentialStoreInitUtil.java
@@ -0,0 +1,156 @@
+/*
+*
+* 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.api.server.util;
+
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.common.utils.ServerSettings;
+import org.apache.derby.drda.NetworkServerControl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.URI;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public class CredentialStoreInitUtil {
+    private static final Logger logger = 
LoggerFactory.getLogger(CredentialStoreInitUtil.class);
+    public static final String CREDENTIALS = "CREDENTIALS";
+    public static final String START_DERBY_ENABLE = "start.derby.server.mode";
+    public static final String DERBY_SERVER_MODE_SYS_PROPERTY = 
"derby.drda.startNetworkServer";
+    private static NetworkServerControl server;
+    private static JdbcStorage db;
+    private static String jdbcURl;
+    private static String jdbcDriver;
+    private static String jdbcUser;
+    private static String jdbcPassword;
+
+
+    public static void initializeDB() {
+//        System.setProperty("appcatalog.initialize.state", "0");
+        try{
+            jdbcDriver = ServerSettings.getCredentialStoreDBDriver();
+            jdbcURl = ServerSettings.getCredentialStoreDBURL();
+            jdbcUser = ServerSettings.getCredentialStoreDBUser();
+            jdbcPassword = ServerSettings.getCredentialStoreDBPassword();
+            jdbcURl = jdbcURl + "?" + "user=" + jdbcUser + "&" + "password=" + 
jdbcPassword;
+        } catch (ApplicationSettingsException e) {
+            logger.error("Unable to read airavata server properties", 
e.getMessage());
+        }
+
+        if (getDBType(jdbcURl).equals("derby") && isDerbyStartEnabled()) {
+            startDerbyInServerMode();
+        }
+        db = new JdbcStorage(10, 50, jdbcURl, jdbcDriver, true);
+
+        Connection conn = null;
+        try {
+            conn = db.connect();
+            if (!DatabaseCreator.isDatabaseStructureCreated(CREDENTIALS, 
conn)) {
+                
DatabaseCreator.createRegistryDatabase("database_scripts/credstore", conn);
+                logger.info("New Database created for Credential Store !!! ");
+            } else {
+                logger.info("Database already created for Credential Store 
!!!");
+            }
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            throw new RuntimeException("Database failure", e);
+        } finally {
+            db.closeConnection(conn);
+            try {
+                if(conn != null){
+                    if (!conn.getAutoCommit()) {
+                        conn.commit();
+                    }
+                    conn.close();
+                }
+            } catch (SQLException e) {
+                logger.error("Error while closing database connection...", 
e.getMessage(), e);
+            }
+        }
+//        System.setProperty("appcatalog.initialize.state", "1");
+    }
+
+    public 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 isDerbyStartEnabled(){
+        try {
+            String s = ServerSettings.getSetting(START_DERBY_ENABLE);
+            if("true".equals(s)){
+                return true;
+            }
+        }  catch (ApplicationSettingsException e) {
+            logger.error("Unable to read airavata server properties", 
e.getMessage(), e);
+            return false;
+        }
+        return false;
+    }
+
+    public static void startDerbyInServerMode() {
+        try {
+            System.setProperty(DERBY_SERVER_MODE_SYS_PROPERTY, "true");
+            server = new NetworkServerControl(InetAddress.getByName("0.0.0.0"),
+                    getPort(jdbcURl),
+                    jdbcUser, jdbcPassword);
+            java.io.PrintWriter consoleWriter = new 
java.io.PrintWriter(System.out, true);
+            server.start(consoleWriter);
+        } catch (IOException e) {
+            logger.error("Unable to start Apache derby in the server mode! 
Check whether " +
+                    "specified port is available");
+        } catch (Exception e) {
+            logger.error("Unable to start Apache derby in the server mode! 
Check whether " +
+                    "specified port is available");
+        }
+    }
+    
+    public static void stopDerbyInServerMode() {
+            System.setProperty(DERBY_SERVER_MODE_SYS_PROPERTY, "false");
+            if (server!=null){
+               try {
+                                       server.shutdown();
+                               } catch (Exception e) {
+                           logger.error("Error when stopping the derby server 
: "+e.getLocalizedMessage());
+                               }
+            }
+    }
+
+    public static int getPort(String jdbcURL){
+        try{
+            String cleanURI = jdbcURL.substring(5);
+            URI uri = URI.create(cleanURI);
+            return uri.getPort();
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            return -1;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/credential-store/credential-store-service/scripts/credential-store-h2.sql
----------------------------------------------------------------------
diff --git 
a/modules/credential-store/credential-store-service/scripts/credential-store-h2.sql
 
b/modules/credential-store/credential-store-service/scripts/credential-store-h2.sql
deleted file mode 100644
index 91915b6..0000000
--- 
a/modules/credential-store/credential-store-service/scripts/credential-store-h2.sql
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- *
- * 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.
- *
- */
-CREATE TABLE COMMUNITY_USER
-(
-       GATEWAY_NAME VARCHAR(256) NOT NULL,
-       COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
-       COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
-        PRIMARY KEY (GATEWAY_NAME, COMMUNITY_USER_NAME)
-);
-
-
-CREATE TABLE CREDENTIALS
-(
-       GATEWAY_NAME VARCHAR(256) NOT NULL,
-       COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
-       CREDENTIAL CLOB NOT NULL,
-       PRIVATE_KEY CLOB NOT NULL,
-       NOT_BEFORE VARCHAR(256) NOT NULL,
-       NOT_AFTER VARCHAR(256) NOT NULL,
-       LIFETIME MEDIUMINT NOT NULL,
-       REQUESTING_PORTAL_USER_NAME VARCHAR(256) NOT NULL,
-       REQUESTED_TIME TIMESTAMP DEFAULT '0000-00-00 00:00:00',
-        PRIMARY KEY (GATEWAY_NAME, COMMUNITY_USER_NAME)
-);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/credential-store/credential-store-service/scripts/credential-store-mysql.sql
----------------------------------------------------------------------
diff --git 
a/modules/credential-store/credential-store-service/scripts/credential-store-mysql.sql
 
b/modules/credential-store/credential-store-service/scripts/credential-store-mysql.sql
deleted file mode 100644
index b0e4fb9..0000000
--- 
a/modules/credential-store/credential-store-service/scripts/credential-store-mysql.sql
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- *
- * 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.
- *
- */
-CREATE TABLE COMMUNITY_USER
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
-        PRIMARY KEY (GATEWAY_ID, COMMUNITY_USER_NAME, TOKEN_ID)
-);
-
-CREATE TABLE CREDENTIALS
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        CREDENTIAL BLOB NOT NULL,
-        PORTAL_USER_ID VARCHAR(256) NOT NULL,
-        TIME_PERSISTED TIMESTAMP DEFAULT NOW() ON UPDATE NOW(),
-        PRIMARY KEY (GATEWAY_ID, TOKEN_ID)
-);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/registry/registry-core/src/main/resources/credstore-derby.sql
----------------------------------------------------------------------
diff --git 
a/modules/registry/registry-core/src/main/resources/credstore-derby.sql 
b/modules/registry/registry-core/src/main/resources/credstore-derby.sql
new file mode 100644
index 0000000..9403cfd
--- /dev/null
+++ b/modules/registry/registry-core/src/main/resources/credstore-derby.sql
@@ -0,0 +1,39 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+ CREATE TABLE COMMUNITY_USER
+(
+        GATEWAY_ID VARCHAR(256) NOT NULL,
+        COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
+        TOKEN_ID VARCHAR(256) NOT NULL,
+        COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
+        PRIMARY KEY (GATEWAY_ID, COMMUNITY_USER_NAME, TOKEN_ID)
+);
+
+CREATE TABLE CREDENTIALS
+(
+        GATEWAY_ID VARCHAR(256) NOT NULL,
+        TOKEN_ID VARCHAR(256) NOT NULL,
+        CREDENTIAL BLOB NOT NULL,
+        PORTAL_USER_ID VARCHAR(256) NOT NULL,
+        TIME_PERSISTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+        PRIMARY KEY (GATEWAY_ID, TOKEN_ID)
+);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/registry/registry-core/src/main/resources/credstore-mysql.sql
----------------------------------------------------------------------
diff --git 
a/modules/registry/registry-core/src/main/resources/credstore-mysql.sql 
b/modules/registry/registry-core/src/main/resources/credstore-mysql.sql
new file mode 100644
index 0000000..df9e2bc
--- /dev/null
+++ b/modules/registry/registry-core/src/main/resources/credstore-mysql.sql
@@ -0,0 +1,39 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+ CREATE TABLE COMMUNITY_USER
+(
+        GATEWAY_ID VARCHAR(256) NOT NULL,
+        COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
+        TOKEN_ID VARCHAR(256) NOT NULL,
+        COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
+        PRIMARY KEY (GATEWAY_ID, COMMUNITY_USER_NAME, TOKEN_ID)
+);
+
+CREATE TABLE CREDENTIALS
+(
+        GATEWAY_ID VARCHAR(256) NOT NULL,
+        TOKEN_ID VARCHAR(256) NOT NULL,
+        CREDENTIAL BLOB NOT NULL,
+        PORTAL_USER_ID VARCHAR(256) NOT NULL,
+        TIME_PERSISTED TIMESTAMP DEFAULT NOW() ON UPDATE NOW(),
+        PRIMARY KEY (GATEWAY_ID, TOKEN_ID)
+);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/registry/registry-core/src/main/resources/expcatalog-derby.sql
----------------------------------------------------------------------
diff --git 
a/modules/registry/registry-core/src/main/resources/expcatalog-derby.sql 
b/modules/registry/registry-core/src/main/resources/expcatalog-derby.sql
index c395b94..3d47071 100644
--- a/modules/registry/registry-core/src/main/resources/expcatalog-derby.sql
+++ b/modules/registry/registry-core/src/main/resources/expcatalog-derby.sql
@@ -341,23 +341,4 @@ CREATE TABLE CONFIGURATION
         PRIMARY KEY(CONFIG_KEY, CONFIG_VAL, CATEGORY_ID)
 );
 
-INSERT INTO CONFIGURATION (CONFIG_KEY, CONFIG_VAL, EXPIRE_DATE, CATEGORY_ID) 
VALUES('registry.version', '0.16', CURRENT_TIMESTAMP ,'SYSTEM');
-
-CREATE TABLE COMMUNITY_USER
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
-        PRIMARY KEY (GATEWAY_ID, COMMUNITY_USER_NAME, TOKEN_ID)
-);
-
-CREATE TABLE CREDENTIALS
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        CREDENTIAL BLOB NOT NULL,
-        PORTAL_USER_ID VARCHAR(256) NOT NULL,
-        TIME_PERSISTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-        PRIMARY KEY (GATEWAY_ID, TOKEN_ID)
-);
\ No newline at end of file
+INSERT INTO CONFIGURATION (CONFIG_KEY, CONFIG_VAL, EXPIRE_DATE, CATEGORY_ID) 
VALUES('registry.version', '0.16', CURRENT_TIMESTAMP ,'SYSTEM');
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/f22acb82/modules/registry/registry-core/src/main/resources/expcatalog-mysql.sql
----------------------------------------------------------------------
diff --git 
a/modules/registry/registry-core/src/main/resources/expcatalog-mysql.sql 
b/modules/registry/registry-core/src/main/resources/expcatalog-mysql.sql
index b3dcf43..ce409ae 100644
--- a/modules/registry/registry-core/src/main/resources/expcatalog-mysql.sql
+++ b/modules/registry/registry-core/src/main/resources/expcatalog-mysql.sql
@@ -345,23 +345,4 @@ CREATE TABLE CONFIGURATION
         PRIMARY KEY(CONFIG_KEY, CONFIG_VAL, CATEGORY_ID)
 );
 
-INSERT INTO CONFIGURATION (CONFIG_KEY, CONFIG_VAL, EXPIRE_DATE, CATEGORY_ID) 
VALUES('registry.version', '0.16', NOW() ,'SYSTEM');
-
-CREATE TABLE COMMUNITY_USER
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,
-        PRIMARY KEY (GATEWAY_ID, COMMUNITY_USER_NAME, TOKEN_ID)
-);
-
-CREATE TABLE CREDENTIALS
-(
-        GATEWAY_ID VARCHAR(256) NOT NULL,
-        TOKEN_ID VARCHAR(256) NOT NULL,
-        CREDENTIAL BLOB NOT NULL,
-        PORTAL_USER_ID VARCHAR(256) NOT NULL,
-        TIME_PERSISTED TIMESTAMP DEFAULT NOW() ON UPDATE NOW(),
-        PRIMARY KEY (GATEWAY_ID, TOKEN_ID)
-);
\ No newline at end of file
+INSERT INTO CONFIGURATION (CONFIG_KEY, CONFIG_VAL, EXPIRE_DATE, CATEGORY_ID) 
VALUES('registry.version', '0.16', NOW() ,'SYSTEM');
\ No newline at end of file

Reply via email to