[exo-jcr-commits] exo-jcr SVN: r2064 - in core/trunk/exo.core.component.database/src: test/java/org/exoplatform/services/database/creator and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Mar 12 11:13:37 EST 2010
Author: tolusha
Date: 2010-03-12 11:13:36 -0500 (Fri, 12 Mar 2010)
New Revision: 2064
Added:
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
Log:
EXOJCR-574: DBCreator
Added: 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 (rev 0)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreationException.java 2010-03-12 16:13:36 UTC (rev 2064)
@@ -0,0 +1,44 @@
+/*
+ * 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);
+ }
+
+}
Added: 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 (rev 0)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreator.java 2010-03-12 16:13:36 UTC (rev 2064)
@@ -0,0 +1,59 @@
+/*
+ * 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 interface DBCreator
+{
+
+ /**
+ * Database template.
+ */
+ public static final String DATABASE_TEMPLATE = "${database}";
+
+ /**
+ * User name template.
+ */
+ public static final String USERNAME_TEMPLATE = "${username}";
+
+ /**
+ * Password template.
+ */
+ 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.
+ *
+ * @param dbName
+ * database name
+ * @param userName
+ * user name
+ * @param password
+ * user's password
+ *
+ * @throws DBCreationException
+ * if any error occurs
+ */
+ void create(String dbName, String userName, String password) throws DBCreationException;
+}
Added: 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 (rev 0)
+++ core/trunk/exo.core.component.database/src/main/java/org/exoplatform/services/database/creator/DBCreatorImpl.java 2010-03-12 16:13:36 UTC (rev 2064)
@@ -0,0 +1,167 @@
+/*
+ * 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.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
+{
+ private static final String DB_CREATOR_PROPERTIES = "db-creator-properties";
+
+ private static final String DRIVER_PROPERTY = "driverClassName";
+
+ private static final String URL_PROPERTY = "url";
+
+ private static final String USERNAME_PROPERTY = "username";
+
+ private static final String PASSWORD_PROPERTY = "password";
+
+ private static final String SCRIPT_PROPERTY = "script";
+
+ // TODO javaDoc
+ private final String driver;
+
+ private final String url;
+
+ private final String userName;
+
+ private final String password;
+
+ private final String script;
+
+ /**
+ * DBCreatorImpl constructor.
+ *
+ * @param contextInit
+ * Initial context initializer
+ * @param params
+ * Initializations params
+ */
+ public DBCreatorImpl(InitParams params)
+ {
+ // TODO null checks
+ PropertiesParam prop = params.getPropertiesParam(DB_CREATOR_PROPERTIES);
+
+ 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);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void create(String dbName, String _userName, String _password) 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);
+ }
+
+ String sql;
+ try
+ {
+ sql = readScriptResource(script);
+ }
+ catch (IOException e)
+ {
+ throw new DBCreationException("Can't read SQL script resource " + script, e);
+ }
+
+ sql = sql.replace(DBCreator.DATABASE_TEMPLATE, dbName);
+ sql = sql.replace(DBCreator.USERNAME_TEMPLATE, _userName);
+ sql = sql.replace(DBCreator.PASSWORD_TEMPLATE, _password);
+
+ try
+ {
+ conn.setAutoCommit(false);
+ conn.createStatement().executeUpdate(sql);
+ conn.commit();
+ }
+ catch (SQLException e)
+ {
+ try
+ {
+ conn.rollback();
+ }
+ catch (SQLException e1)
+ {
+ throw new DBCreationException("Can't perform rollback", e);
+ }
+ throw new DBCreationException("Can't execute SQL script " + sql, e);
+ }
+ }
+
+ /**
+ * Read SQL script from file resource.
+ */
+ 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
+ {
+ 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
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ }
+ }
+ }
+}
Added: 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 (rev 0)
+++ core/trunk/exo.core.component.database/src/test/java/org/exoplatform/services/database/creator/TestDBCreator.java 2010-03-12 16:13:36 UTC (rev 2064)
@@ -0,0 +1,60 @@
+/*
+ * 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.PortalContainer;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
+ * @version $Id: $
+ */
+public class TestDBCreator extends TestCase
+{
+
+ private DBCreator dbCreator;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ PortalContainer container = PortalContainer.getInstance();
+ dbCreator = (DBCreator)container.getComponentInstanceOfType(DBCreator.class);
+
+ assertNotNull(dbCreator);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ public void testCreate() throws Exception
+ {
+ // TODO create DB
+ // dbCreator.create("TestDB", "TestUser", "TestPwd");
+ }
+}
More information about the exo-jcr-commits
mailing list