[exo-jcr-commits] exo-jcr SVN: r2066 - in core/trunk/exo.core.component.database/src: test/java/org/exoplatform/services/database/creator and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Mar 15 08:04:18 EDT 2010


Author: tolusha
Date: 2010-03-15 08:04:18 -0400 (Mon, 15 Mar 2010)
New Revision: 2066

Modified:
   core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java
   core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
   core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java
   core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/creator/TestDBCreator.java
   core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml
Log:
EXOJCR-573: DBCreator impl

Modified: core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java
===================================================================
--- core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java	2010-03-15 11:40:34 UTC (rev 2065)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java	2010-03-15 12:04:18 UTC (rev 2066)
@@ -40,5 +40,4 @@
    {
       super(message, e);
    }
-
 }

Modified: core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
===================================================================
--- core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java	2010-03-15 11:40:34 UTC (rev 2065)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java	2010-03-15 12:04:18 UTC (rev 2066)
@@ -41,19 +41,14 @@
    public static final String PASSWORD_TEMPLATE = "${password}";
 
    /**
-    * Create database using predefined SQL DDL script for specific user and password.
-    * Database name, user name and password defined in script via templates as
-    * ${database}, ${username} and ${password} respectively.
+    * Create database using predefined SQL DDL script for new user which are given via configuration.
+    * New database name, user name and password defined in script via templates as ${database}, ${username}
+    * and ${password} respectively.
     * 
     * @param dbName
-    *          database name
-    * @param userName
-    *          user name
-    * @param password
-    *          user's password
-    *          
+    *          new database name
     * @throws DBCreationException
     *          if any error occurs 
     */
-   void create(String dbName, String userName, String password) throws DBCreationException;
+   void create(String dbName) throws DBCreationException;
 }

Modified: core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java
===================================================================
--- core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java	2010-03-15 11:40:34 UTC (rev 2065)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java	2010-03-15 12:04:18 UTC (rev 2066)
@@ -18,6 +18,7 @@
  */
 package org.exoplatform.services.database.creator;
 
+import org.exoplatform.container.configuration.ConfigurationException;
 import org.exoplatform.container.xml.InitParams;
 import org.exoplatform.container.xml.PropertiesParam;
 
@@ -35,53 +36,123 @@
  */
 public class DBCreatorImpl implements DBCreator
 {
-   private static final String DB_CREATOR_PROPERTIES = "db-creator-properties";
+   public static final String SQL_DELIMITER = ";";
 
+   public static final String SQL_DELIMITER_COMMENT_PREFIX = "/*$DELIMITER:";
+
+   private static final String DB_CONNECTION_PROPERTIES = "db-connection";
+
+   private static final String DB_CREATION_PROPERTIES = "db-creation";
+
    private static final String DRIVER_PROPERTY = "driverClassName";
 
    private static final String URL_PROPERTY = "url";
 
+   private static final String SCRIPT_PATH_PROPERTY = "scriptPath";
+
    private static final String USERNAME_PROPERTY = "username";
 
    private static final String PASSWORD_PROPERTY = "password";
 
-   private static final String SCRIPT_PROPERTY = "script";
+   /**
+    * Driver class name.
+    */
+   protected final String driver;
 
-   // TODO javaDoc
-   private final String driver;
+   /**
+    * Database url.
+    */
+   protected final String url;
 
-   private final String url;
+   /**
+    * SA user name.
+    */
+   protected final String userName;
 
-   private final String userName;
+   /**
+    * SA user's password.
+    */
+   protected final String password;
 
-   private final String password;
+   /**
+    * DB script creation.
+    */
+   protected final String dbScript;
 
-   private final String script;
+   /**
+    * User name for new DB.
+    */
+   protected final String dbUserName;
 
    /**
+    * User's password.
+    */
+   protected final String dbPassword;
+
+   /**
     * DBCreatorImpl constructor.
     * 
     * @param contextInit
     *          Initial context initializer
     * @param params
-    *          Initializations params
+    *          Initializations parameters
     */
-   public DBCreatorImpl(InitParams params)
+   public DBCreatorImpl(InitParams params) throws ConfigurationException
    {
-      // TODO null checks
-      PropertiesParam prop = params.getPropertiesParam(DB_CREATOR_PROPERTIES);
+      if (params == null)
+      {
+         throw new ConfigurationException("Initializations parameters expected");
+      }
 
-      this.driver = prop.getProperty(DRIVER_PROPERTY);
-      this.url = prop.getProperty(URL_PROPERTY);
-      this.userName = prop.getProperty(USERNAME_PROPERTY);
-      this.password = prop.getProperty(PASSWORD_PROPERTY);
-      this.script = prop.getProperty(SCRIPT_PROPERTY);
+      PropertiesParam prop = params.getPropertiesParam(DB_CONNECTION_PROPERTIES);
+
+      if (prop != null)
+      {
+         this.driver = prop.getProperty(DRIVER_PROPERTY);
+         this.url = prop.getProperty(URL_PROPERTY);
+         this.userName = prop.getProperty(USERNAME_PROPERTY);
+         this.password = prop.getProperty(PASSWORD_PROPERTY);
+      }
+      else
+      {
+         throw new ConfigurationException(DB_CONNECTION_PROPERTIES + " expected in initializations parameters");
+      }
+
+      prop = params.getPropertiesParam(DB_CREATION_PROPERTIES);
+
+      if (prop != null)
+      {
+         String scriptPath = prop.getProperty(SCRIPT_PATH_PROPERTY);
+         if (scriptPath != null)
+         {
+            try
+            {
+               dbScript = readScriptResource(scriptPath);
+            }
+            catch (IOException e)
+            {
+               throw new ConfigurationException("Can't read script resource " + scriptPath, e);
+            }
+         }
+         else
+         {
+            throw new ConfigurationException(SCRIPT_PATH_PROPERTY + " expected in initializations parameters");
+         }
+
+         this.dbUserName = prop.getProperty(USERNAME_PROPERTY);
+         this.dbPassword = prop.getProperty(PASSWORD_PROPERTY);
+      }
+      else
+      {
+         throw new ConfigurationException(DB_CREATION_PROPERTIES + " expected in initializations parameters");
+      }
+
    }
 
    /**
     * {@inheritDoc}
     */
-   public void create(String dbName, String _userName, String _password) throws DBCreationException
+   public void create(String dbName) throws DBCreationException
    {
       Connection conn = null;
       try
@@ -98,24 +169,22 @@
          throw new DBCreationException("Can't load the JDBC driver " + driver, e);
       }
 
-      String sql;
       try
       {
-         sql = readScriptResource(script);
-      }
-      catch (IOException e)
-      {
-         throw new DBCreationException("Can't read SQL script resource " + script, e);
-      }
+         conn.setAutoCommit(false);
 
-      sql = sql.replace(DBCreator.DATABASE_TEMPLATE, dbName);
-      sql = sql.replace(DBCreator.USERNAME_TEMPLATE, _userName);
-      sql = sql.replace(DBCreator.PASSWORD_TEMPLATE, _password);
+         for (String scr : dbScript.split(SQL_DELIMITER))
+         {
+            scr = scr.replace(DBCreator.DATABASE_TEMPLATE, dbName);
+            scr = scr.replace(DBCreator.USERNAME_TEMPLATE, dbUserName);
+            scr = scr.replace(DBCreator.PASSWORD_TEMPLATE, dbPassword);
 
-      try
-      {
-         conn.setAutoCommit(false);
-         conn.createStatement().executeUpdate(sql);
+            String s = cleanWhitespaces(scr.trim());
+            if (s.length() > 0)
+            {
+               conn.createStatement().executeUpdate(s);
+            }
+         }
          conn.commit();
       }
       catch (SQLException e)
@@ -126,10 +195,21 @@
          }
          catch (SQLException e1)
          {
-            throw new DBCreationException("Can't perform rollback", e);
+            throw new DBCreationException("Can't perform rollback", e1);
          }
-         throw new DBCreationException("Can't execute SQL script " + sql, e);
+         throw new DBCreationException("Can't execute SQL script", e);
       }
+      finally
+      {
+         try
+         {
+            conn.close();
+         }
+         catch (SQLException e)
+         {
+            throw new DBCreationException("Can't close connection", e);
+         }
+      }
    }
 
    /**
@@ -137,8 +217,6 @@
     */
    protected String readScriptResource(String path) throws IOException
    {
-      //      InputStream is = this.getClass().getResourceAsStream(path);
-      // TODO
       InputStream is = new FileInputStream(path);
       InputStreamReader isr = new InputStreamReader(is);
       try
@@ -155,13 +233,27 @@
       }
       finally
       {
-         try
+         is.close();
+      }
+   }
+
+   /**
+    * Clean whitespace.
+    */
+   private String cleanWhitespaces(String string)
+   {
+      if (string != null)
+      {
+         char[] cc = string.toCharArray();
+         for (int ci = cc.length - 1; ci > 0; ci--)
          {
-            is.close();
+            if (Character.isWhitespace(cc[ci]))
+            {
+               cc[ci] = ' ';
+            }
          }
-         catch (IOException e)
-         {
-         }
+         return new String(cc);
       }
+      return string;
    }
 }

Modified: core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/creator/TestDBCreator.java
===================================================================
--- core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/creator/TestDBCreator.java	2010-03-15 11:40:34 UTC (rev 2065)
+++ core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/creator/TestDBCreator.java	2010-03-15 12:04:18 UTC (rev 2066)
@@ -54,7 +54,6 @@
 
    public void testCreate() throws Exception
    {
-      // TODO create DB
-      //      dbCreator.create("TestDB", "TestUser", "TestPwd");
+      dbCreator.create("portal");
    }
 }

Modified: core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml
===================================================================
--- core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml	2010-03-15 11:40:34 UTC (rev 2065)
+++ core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml	2010-03-15 12:04:18 UTC (rev 2066)
@@ -52,16 +52,23 @@
 
    <component>
       <key>org.exoplatform.services.database.creator.DBCreator</key>
-      <type>org.exoplatform.services.database.creator.DBCreatorImpl</type>
+      <type>org.exoplatform.services.database.creator.TesterDBCreator</type>
       <init-params>
          <properties-param>
-            <name>db-creator-properties</name>
+            <name>db-connection</name>
+            <description>database connection properties</description>
             <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
             <property name="url" value="jdbc:hsqldb:file:target/temp/data/portal" />
 	    <property name="username" value="sa" />
 	    <property name="password" value="" />
-            <property name="script" value="test.sql" />
          </properties-param>
+         <properties-param>
+            <name>db-creation</name>
+            <description>properites for new database creation</description>
+            <property name="scriptPath" value="test.sql" />
+	    <property name="username" value="sa" />
+	    <property name="password" value="" />
+         </properties-param>
       </init-params>
    </component>
 



More information about the exo-jcr-commits mailing list