[exo-jcr-commits] exo-jcr SVN: r2074 - in core/trunk/exo.core.component.database/src: test/java/org/exoplatform/services/database and 1 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Tue Mar 16 09:14:18 EDT 2010
Author: tolusha
Date: 2010-03-16 09:14:17 -0400 (Tue, 16 Mar 2010)
New Revision: 2074
Added:
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorException.java
core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java
Removed:
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/DBCreatorImpl.java
Modified:
core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java
core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml
Log:
EXOJCR-574: DBCreator impl
Deleted: 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-16 13:13:35 UTC (rev 2073)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java 2010-03-16 13:14:17 UTC (rev 2074)
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2010 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.exoplatform.services.database.creator;
-
-/**
- * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
- * @version $Id: $
- */
-public class DBCreationException extends Exception
-{
-
- /**
- * DBCreationException constructor.
- */
- public DBCreationException(Throwable e)
- {
- super(e);
- }
-
- /**
- * DBCreationException constructor.
- */
- public DBCreationException(String message, Throwable e)
- {
- 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-16 13:13:35 UTC (rev 2073)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2010-03-16 13:14:17 UTC (rev 2074)
@@ -18,11 +18,23 @@
*/
package org.exoplatform.services.database.creator;
+import org.exoplatform.container.configuration.ConfigurationException;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.PropertiesParam;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
/**
* @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
- * @version $Id: $
+ * @version $Id: DBCreator.java 111 2010-11-11 11:11:11Z tolusha $
*/
-public interface DBCreator
+public class DBCreator
{
/**
@@ -41,14 +53,210 @@
public static final String PASSWORD_TEMPLATE = "${password}";
/**
- * 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.
+ * Driver class name.
+ */
+ protected final String driver;
+
+ /**
+ * Database url.
+ */
+ protected final String url;
+
+ /**
+ * SA user name.
+ */
+ protected final String userName;
+
+ /**
+ * SA user's password.
+ */
+ protected final String password;
+
+ /**
+ * DB script creation.
+ */
+ protected final String dbScript;
+
+ /**
+ * User name for new DB.
+ */
+ protected final String dbUserName;
+
+ /**
+ * User's password.
+ */
+ protected final String dbPassword;
+
+ /**
+ * DBCreator constructor.
*
+ * @param params
+ * Initializations parameters
+ */
+ public DBCreator(InitParams params) throws ConfigurationException
+ {
+ if (params == null)
+ {
+ throw new ConfigurationException("Initializations parameters expected");
+ }
+
+ PropertiesParam prop = params.getPropertiesParam("db-connection");
+
+ if (prop != null)
+ {
+ this.driver = prop.getProperty("driverClassName");
+ this.url = prop.getProperty("url");
+ this.userName = prop.getProperty("username");
+ this.password = prop.getProperty("password");
+ }
+ else
+ {
+ throw new ConfigurationException("db-connection properties expected in initializations parameters");
+ }
+
+ prop = params.getPropertiesParam("db-creation");
+
+ if (prop != null)
+ {
+ String scriptPath = prop.getProperty("scriptPath");
+ if (scriptPath != null)
+ {
+ try
+ {
+ dbScript = readScriptResource(scriptPath);
+ }
+ catch (IOException e)
+ {
+ throw new ConfigurationException("Can't read script resource " + scriptPath, e);
+ }
+ }
+ else
+ {
+ throw new ConfigurationException("scriptPath expected in initializations parameters");
+ }
+
+ this.dbUserName = prop.getProperty("username");
+ this.dbPassword = prop.getProperty("password");
+ }
+ else
+ {
+ throw new ConfigurationException("db-creation properties expected in initializations parameters");
+ }
+
+ }
+
+ /**
+ * Create database using predefined SQL script. Database name passed as parameter,
+ * user name and password passed via configuration. In SQL script database name, user name
+ * and password defined via templates as ${database}, ${username} and ${password} respectively.
+ *
* @param dbName
* new database name
- * @throws DBCreationException
+ * @throws DBCreatorException
* if any error occurs
*/
- void create(String dbName) throws DBCreationException;
+ public void create(String dbName) throws DBCreatorException
+ {
+ Connection conn = null;
+ try
+ {
+ Class.forName(driver);
+ conn = DriverManager.getConnection(url, userName, password);
+ }
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can't establish the JDBC connection to database " + url, e);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new DBCreatorException("Can't load the JDBC driver " + driver, e);
+ }
+
+ try
+ {
+ conn.setAutoCommit(false);
+
+ for (String scr : dbScript.split(";"))
+ {
+ scr = scr.replace(DATABASE_TEMPLATE, dbName);
+ scr = scr.replace(USERNAME_TEMPLATE, dbUserName);
+ scr = scr.replace(PASSWORD_TEMPLATE, dbPassword);
+
+ String s = cleanWhitespaces(scr.trim());
+ if (s.length() > 0)
+ {
+ conn.createStatement().executeUpdate(s);
+ }
+ }
+ conn.commit();
+ }
+ catch (SQLException e)
+ {
+ try
+ {
+ conn.rollback();
+ }
+ catch (SQLException e1)
+ {
+ throw new DBCreatorException("Can't perform rollback", e1);
+ }
+ throw new DBCreatorException("Can't execute SQL script", e);
+ }
+ finally
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (SQLException e)
+ {
+ throw new DBCreatorException("Can't close connection", e);
+ }
+ }
+ }
+
+ /**
+ * Read SQL script from file resource.
+ */
+ protected String readScriptResource(String path) throws IOException
+ {
+ InputStream is = new FileInputStream(path);
+ InputStreamReader isr = new InputStreamReader(is);
+ try
+ {
+ StringBuilder sbuff = new StringBuilder();
+ char[] buff = new char[is.available()];
+ int r = 0;
+ while ((r = isr.read(buff)) > 0)
+ {
+ sbuff.append(buff, 0, r);
+ }
+
+ return sbuff.toString();
+ }
+ finally
+ {
+ 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--)
+ {
+ if (Character.isWhitespace(cc[ci]))
+ {
+ cc[ci] = ' ';
+ }
+ }
+ return new String(cc);
+ }
+ return string;
+ }
}
Added: core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorException.java
===================================================================
--- core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorException.java (rev 0)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorException.java 2010-03-16 13:14:17 UTC (rev 2074)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.database.creator;
+
+/**
+ * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
+ * @version $Id: DBCreatorException.java 111 2010-11-11 11:11:11Z tolusha $
+ */
+public class DBCreatorException extends Exception
+{
+
+ /**
+ * DBCreationException constructor.
+ */
+ public DBCreatorException(Throwable e)
+ {
+ super(e);
+ }
+
+ /**
+ * DBCreationException constructor.
+ */
+ public DBCreatorException(String message, Throwable e)
+ {
+ super(message, e);
+ }
+}
Deleted: 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-16 13:13:35 UTC (rev 2073)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java 2010-03-16 13:14:17 UTC (rev 2074)
@@ -1,259 +0,0 @@
-/*
- * Copyright (C) 2010 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.exoplatform.services.database.creator;
-
-import org.exoplatform.container.configuration.ConfigurationException;
-import org.exoplatform.container.xml.InitParams;
-import org.exoplatform.container.xml.PropertiesParam;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-
-/**
- * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
- * @version $Id: $
- */
-public class DBCreatorImpl implements DBCreator
-{
- 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";
-
- /**
- * Driver class name.
- */
- protected final String driver;
-
- /**
- * Database url.
- */
- protected final String url;
-
- /**
- * SA user name.
- */
- protected final String userName;
-
- /**
- * SA user's password.
- */
- protected final String password;
-
- /**
- * DB script creation.
- */
- protected final String dbScript;
-
- /**
- * 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 parameters
- */
- public DBCreatorImpl(InitParams params) throws ConfigurationException
- {
- if (params == null)
- {
- throw new ConfigurationException("Initializations parameters expected");
- }
-
- 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) throws DBCreationException
- {
- Connection conn = null;
- try
- {
- Class.forName(driver);
- conn = DriverManager.getConnection(url, userName, password);
- }
- catch (SQLException e)
- {
- throw new DBCreationException("Can't establish the JDBC connection to database " + url, e);
- }
- catch (ClassNotFoundException e)
- {
- throw new DBCreationException("Can't load the JDBC driver " + driver, e);
- }
-
- try
- {
- conn.setAutoCommit(false);
-
- 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);
-
- String s = cleanWhitespaces(scr.trim());
- if (s.length() > 0)
- {
- conn.createStatement().executeUpdate(s);
- }
- }
- conn.commit();
- }
- catch (SQLException e)
- {
- try
- {
- conn.rollback();
- }
- catch (SQLException e1)
- {
- throw new DBCreationException("Can't perform rollback", e1);
- }
- throw new DBCreationException("Can't execute SQL script", e);
- }
- finally
- {
- try
- {
- conn.close();
- }
- catch (SQLException e)
- {
- throw new DBCreationException("Can't close connection", e);
- }
- }
- }
-
- /**
- * Read SQL script from file resource.
- */
- protected String readScriptResource(String path) throws IOException
- {
- InputStream is = new FileInputStream(path);
- InputStreamReader isr = new InputStreamReader(is);
- try
- {
- StringBuilder sbuff = new StringBuilder();
- char[] buff = new char[is.available()];
- int r = 0;
- while ((r = isr.read(buff)) > 0)
- {
- sbuff.append(buff, 0, r);
- }
-
- return sbuff.toString();
- }
- finally
- {
- 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--)
- {
- if (Character.isWhitespace(cc[ci]))
- {
- cc[ci] = ' ';
- }
- }
- return new String(cc);
- }
- return string;
- }
-}
Added: core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java
===================================================================
--- core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java (rev 0)
+++ core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/TestDBCreator.java 2010-03-16 13:14:17 UTC (rev 2074)
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.database;
+
+import org.exoplatform.container.PortalContainer;
+import org.exoplatform.services.database.creator.DBCreator;
+import org.exoplatform.services.database.creator.DBCreatorException;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
+ * @version $Id: TestDBCreator.java 111 2010-11-11 11:11:11Z tolusha $
+ */
+public class TestDBCreator extends TestCase
+{
+
+ protected DBCreator dbCreator;
+
+ public void setUp() throws Exception
+ {
+ PortalContainer container = PortalContainer.getInstance();
+ dbCreator = (DBCreator)container.getComponentInstanceOfType(DBCreator.class);
+ }
+
+ public void testDBCreator() throws Exception
+ {
+ assertNotNull(dbCreator);
+ try
+ {
+ dbCreator.create("testDB");
+ }
+ catch (DBCreatorException e)
+ {
+ fail("Exception should not be thrown.");
+ }
+ }
+}
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-16 13:13:35 UTC (rev 2073)
+++ core/trunk/exo.core.component.database/src/test/resources/conf/standalone/test-configuration.xml 2010-03-16 13:14:17 UTC (rev 2074)
@@ -50,6 +50,28 @@
</component-plugins>
</component>
+ <component>
+ <key>org.exoplatform.services.database.creator.DBCreator</key>
+ <type>org.exoplatform.services.database.creator.DBCreator</type>
+ <init-params>
+ <properties-param>
+ <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="" />
+ </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>
+
<component>
<key>org.exoplatform.services.naming.InitialContextInitializer</key>
<type>org.exoplatform.services.naming.InitialContextInitializer</type>
More information about the exo-jcr-commits
mailing list