You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by dd...@apache.org on 2005/11/17 17:20:59 UTC

svn commit: r345276 - /portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java

Author: ddewolf
Date: Thu Nov 17 08:20:55 2005
New Revision: 345276

URL: http://svn.apache.org/viewcvs?rev=345276&view=rev
Log:
Implementing Database Creation on Startup

Modified:
    portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java

Modified: portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java
URL: http://svn.apache.org/viewcvs/portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java?rev=345276&r1=345275&r2=345276&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java (original)
+++ portals/pluto/trunk/pluto-portal-driver/src/main/java/org/apache/pluto/driver/services/container/db/EmbeddedDataSourceManager.java Thu Nov 17 08:20:55 2005
@@ -25,6 +25,13 @@
 import java.sql.SQLException;
 import java.sql.ResultSet;
 import java.sql.Statement;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Iterator;
 
 
 /**
@@ -91,6 +98,63 @@
                 LOG.info("Pluto Database does not exist.  Creating database now.");
             }
 
+            boolean autoCommit = false;
+            Connection conn = null;
+            Statement  stmt = null;
+            try {
+                List script = readScript();
+                conn = embeddedDataSource.getConnection();
+                autoCommit = conn.getAutoCommit();
+                conn.setAutoCommit(false);
+
+                stmt = conn.createStatement();
+                Iterator it = script.iterator();
+                int results = 0;
+                while(it.hasNext()) {
+                    String sql = it.next().toString();
+                    if(sql.trim().length() > 0) {
+                       results += stmt.executeUpdate(sql);
+                    }
+                }
+                conn.commit();
+                if(LOG.isDebugEnabled()) {
+                    LOG.debug("Created "+results+ " database objects");
+                }
+           }
+            catch(IOException io) {
+                if(LOG.isErrorEnabled()) {
+                    LOG.error("Error reading script file", io);
+                }
+
+                throw new PortletContainerException(io);
+            }
+            catch(SQLException sql) {
+                try {
+                    if(conn != null)
+                        conn.rollback();
+                } catch(SQLException sqle) {
+                    if(LOG.isErrorEnabled()) {
+                        LOG.error("SQLException rolling back connection", sqle);
+                    }
+                }
+
+
+                if(LOG.isErrorEnabled()) {
+                    LOG.error("Error creating database schema", sql);
+                }
+                throw new PortletContainerException(sql);
+            }
+            finally {
+                try {
+                    conn.setAutoCommit(autoCommit);
+                }
+                catch(SQLException sql) {
+                    if(LOG.isErrorEnabled()) {
+                        LOG.error("Error resetting autocommit.",sql);
+                    }
+                }
+                cleanup(conn, stmt, null);
+            }
         }
             // 1) Read in the PORTABLE CREATE SQL Script
             // 2) Execute the CREATE Script via JDBC
@@ -109,19 +173,62 @@
         try {
             conn = embeddedDataSource.getConnection();
             stmt = conn.createStatement();
-            rs   = stmt.executeQuery("SELECT count(*) FROM SYS.SYSTABLES WHERE TABLENAME = 'PPD_VERSION'");
+            rs   = stmt.executeQuery("SELECT count(*) FROM SYS.SYSTABLES WHERE TABLENAME = 'VERSION'");
             if(rs.next()) {
                 found = rs.getInt(1) == 1;
             }
             else throw new SQLException("No results returned.  Should never happen for count(*)");
         }
         catch(SQLException sql) {
+            if(LOG.isErrorEnabled()) {
+                LOG.error("Error testing database validity", sql);
+            }
             throw new PortletContainerException(sql);
         }
         finally {
             cleanup(conn, stmt, rs);
         }
        return found;
+    }
+
+    private ArrayList readScript() throws IOException {
+        InputStream in = getClass().getResourceAsStream("/create_schema.sql");
+        if(in == null) {
+            throw new IOException("Unable to find schemal data definition");
+        }
+
+        BufferedReader input = new BufferedReader(new InputStreamReader(in));
+
+        ArrayList commands = new ArrayList();
+        StringBuffer sb = new StringBuffer();
+        String read;
+
+        if(LOG.isDebugEnabled()) {
+            LOG.debug("Adding SQL Commands for schema creation");
+        }
+
+        while( (read = input.readLine()) != null) {
+            read = read.trim();
+            if(read.startsWith("--"))
+                continue;
+
+           if(read.indexOf(';') > -0) {
+                read = read.replace(';', ' ');
+                if(LOG.isDebugEnabled()) {
+                    LOG.debug(sb.toString());
+                }
+                commands.add(sb.append(read).toString());
+                sb = new StringBuffer();
+               continue;
+            }
+
+            if(read.length() > 0) {
+                sb.append(read).append(" ");
+           }
+        }
+
+        input.close();
+        return commands;
     }
 
     private void cleanup(Connection conn, Statement stmt, ResultSet rs) {