Author: sergiykarpenko
Date: 2010-09-10 11:19:42 -0400 (Fri, 10 Sep 2010)
New Revision: 3104
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerService.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/JDBCConfiguration.java
Log:
EXOJCR-939: DBCleanerService added
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleaner.java 2010-09-10
15:19:42 UTC (rev 3104)
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2003-2010 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.util.jdbc;
+
+import java.sql.Connection;
+
+/**
+ * The goal of this class is remove workspace data from database.
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: DBCleaner.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class DBCleaner
+{
+
+ protected String REMOVE_ITEMS;
+
+ protected String REMOVE_VALUES;
+
+ protected String REMOVE_REFERENCES;
+
+ protected String DROP_JCR_MITEM_TABLE;
+
+ protected String DROP_JCR_MVALUE_TABLE;
+
+ protected String DROP_MREF_TABLE;
+
+ private final Connection connection;
+
+ private final String containerName;
+
+ /**
+ * Constructor.
+ *
+ * @param containerName - workspace name
+ * @param connection - SQL conneciton
+ */
+ public DBCleaner(Connection connection, String containerName)
+ {
+ this.connection = connection;
+ this.containerName = containerName;
+ prepareQueries();
+ }
+
+ protected void prepareQueries()
+ {
+
+ }
+
+ public void removeWorkspace()
+ {
+
+ }
+
+ public void cleanupWorkspace()
+ {
+ //for single db support
+ REMOVE_ITEMS = "delete from JCR_SITEM where CONTAINER_NAME=?";
+
+ REMOVE_VALUES = "delete from JCR_SVALUE where PROPERTY_ID=?";
+ REMOVE_REFERENCES = "delete from JCR_SREF where PROPERTY_ID=?";
+
+ // for multi db support
+ DROP_JCR_MITEM_TABLE = "DROP TABLE JCR_MITEM";
+ DROP_JCR_MVALUE_TABLE = "DROP TABLE JCR_MVALUE";
+ DROP_MREF_TABLE = "DROP TABLE JCR_MREF";
+
+ // DROP TABLE orders;
+ // DROP DATABASE mydatabase;
+ // DROP VIEW viewname;
+ // DROP INDEX orders.indexname;
+ //
+ // -- FOR USE WITH ALTER COMMANDS
+ // DROP COLUMN column_name
+ // DROP FOREIGN KEY (foreign_key_name)
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerService.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerService.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerService.java 2010-09-10
15:19:42 UTC (rev 3104)
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2003-2010 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.util.jdbc;
+
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import javax.jcr.RepositoryException;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: DBCleanerService.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class DBCleanerService
+{
+
+ public static void removeWorkspaceData(WorkspaceEntry wsConfig) throws
RepositoryConfigurationException,
+ NamingException, RepositoryException, IOException
+ {
+ JDBCConfiguration wsJDBCConfig = new JDBCConfiguration(wsConfig);
+
+ DataSource ds = (DataSource)new
InitialContext().lookup(wsJDBCConfig.getDbSourceName());
+ Connection conn = null;
+ try
+ {
+ conn =
+ ds != null ? ds.getConnection() : (wsJDBCConfig.getDbUserName() != null ?
DriverManager.getConnection(
+ wsJDBCConfig.getDbUrl(), wsJDBCConfig.getDbUserName(),
wsJDBCConfig.getDbPassword()) : DriverManager
+ .getConnection(wsJDBCConfig.getDbUrl()));
+
+ }
+ catch (SQLException e)
+ {
+ String err =
+ "Error of JDBC connection open. SQLException: " + e.getMessage() +
", SQLState: " + e.getSQLState()
+ + ", VendorError: " + e.getErrorCode();
+ throw new RepositoryException(err, e);
+ }
+
+ DBCleaner cleaner = new DBCleaner(conn, wsJDBCConfig.getContainerName());
+ // check is multi db
+ if (wsJDBCConfig.isMultiDb())
+ {
+ //remove table
+ cleaner.removeWorkspace();
+ }
+ else
+ {
+ // clean up all record of this container
+ cleaner.cleanupWorkspace();
+ }
+ }
+
+ public static void removeRepositoryData(RepositoryEntry repoConfig) throws
RepositoryConfigurationException,
+ NamingException, RepositoryException, IOException
+ {
+ for (WorkspaceEntry wsEntry : repoConfig.getWorkspaceEntries())
+ {
+ removeWorkspaceData(wsEntry);
+ }
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/JDBCConfiguration.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/JDBCConfiguration.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/JDBCConfiguration.java 2010-09-10
15:19:42 UTC (rev 3104)
@@ -0,0 +1,289 @@
+/*
+ * Copyright (C) 2003-2010 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.util.jdbc;
+
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.impl.storage.jdbc.DBConstants;
+import org.exoplatform.services.jcr.impl.storage.jdbc.DialectDetecter;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import javax.jcr.RepositoryException;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: JDBCConfiguration.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class JDBCConfiguration
+{
+
+ protected static final Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.JDBCConfiguration");
+
+ public final static String SOURCE_NAME = "source-name";
+
+ public final static String MULTIDB = "multi-db";
+
+ public final static String SINGLEDB = "single-db";
+
+ /**
+ * Describe which type of RDBMS will be used (DB creation metadata etc.)
+ */
+ public final static String DB_DIALECT = "dialect";
+
+ public final static String DB_DRIVER = "driverClassName";
+
+ public final static String DB_URL = "url";
+
+ public final static String DB_USERNAME = "username";
+
+ public final static String DB_PASSWORD = "password";
+
+ public final static String DB_FORCE_QUERY_HINTS = "force.query.hints";
+
+ protected final String containerName;
+
+ protected final String dbSourceName;
+
+ protected final boolean multiDb;
+
+ protected final String dbDriver;
+
+ protected final String dbDialect;
+
+ protected final String dbUrl;
+
+ protected final String dbUserName;
+
+ protected final String dbPassword;
+
+ public JDBCConfiguration(WorkspaceEntry wsConfig) throws
RepositoryConfigurationException, NamingException,
+ RepositoryException, IOException
+ {
+ this.containerName = wsConfig.getName();
+ this.multiDb =
Boolean.parseBoolean(wsConfig.getContainer().getParameterValue(MULTIDB));
+
+ // ------------- Database config ------------------
+ String pDbDialect = null;
+ try
+ {
+ pDbDialect =
validateDialect(wsConfig.getContainer().getParameterValue(DB_DIALECT));
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ pDbDialect = DBConstants.DB_DIALECT_GENERIC;
+ }
+
+ String pDbDriver = null;
+ String pDbUrl = null;
+ String pDbUserName = null;
+ String pDbPassword = null;
+ try
+ {
+ pDbDriver = wsConfig.getContainer().getParameterValue(DB_DRIVER);
+
+ // username/passwd may not pesent
+ try
+ {
+ pDbUserName = wsConfig.getContainer().getParameterValue(DB_USERNAME);
+ pDbPassword = wsConfig.getContainer().getParameterValue(DB_PASSWORD);
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ pDbUserName = pDbPassword = null;
+ }
+
+ pDbUrl = wsConfig.getContainer().getParameterValue(DB_URL); // last here!
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ }
+
+ if (pDbUrl != null)
+ {
+ this.dbDriver = pDbDriver;
+ this.dbUrl = pDbUrl;
+ this.dbUserName = pDbUserName;
+ this.dbPassword = pDbPassword;
+ this.dbSourceName = null;
+ LOG.info("Connect to JCR database as user '" + this.dbUserName +
"'");
+
+ if (pDbDialect == DBConstants.DB_DIALECT_GENERIC ||
DBConstants.DB_DIALECT_AUTO.equalsIgnoreCase(pDbDialect))
+ {
+ // try to detect via JDBC metadata
+ Connection jdbcConn = null;
+ try
+ {
+ jdbcConn =
+ dbUserName != null ? DriverManager.getConnection(dbUrl, dbUserName,
dbPassword) : DriverManager
+ .getConnection(dbUrl);
+
+ this.dbDialect = DialectDetecter.detect(jdbcConn.getMetaData());
+ }
+ catch (SQLException e)
+ {
+ throw new RepositoryException(e);
+ }
+ finally
+ {
+ if (jdbcConn != null)
+ {
+ try
+ {
+ jdbcConn.close();
+ }
+ catch (SQLException e)
+ {
+ throw new RepositoryException(e);
+ }
+ }
+ }
+ }
+ else
+ {
+ this.dbDialect = pDbDialect;
+ }
+ }
+ else
+ {
+ this.dbDriver = null;
+ this.dbUrl = null;
+ this.dbUserName = null;
+ this.dbPassword = null;
+
+ String sn;
+ try
+ {
+ sn = wsConfig.getContainer().getParameterValue(SOURCE_NAME);
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ sn = wsConfig.getContainer().getParameterValue("sourceName"); //
TODO for backward comp,
+ // remove in rel.2.0
+ }
+ this.dbSourceName = sn;
+
+ if (pDbDialect == DBConstants.DB_DIALECT_GENERIC)
+ {
+ // try to detect via JDBC metadata
+ DataSource ds = (DataSource)new InitialContext().lookup(dbSourceName);
+ if (ds != null)
+ {
+ Connection jdbcConn = null;
+ try
+ {
+ jdbcConn = ds.getConnection();
+ this.dbDialect = DialectDetecter.detect(jdbcConn.getMetaData());
+ }
+ catch (SQLException e)
+ {
+ throw new RepositoryException(e);
+ }
+ finally
+ {
+ if (jdbcConn != null)
+ {
+ try
+ {
+ jdbcConn.close();
+ }
+ catch (SQLException e)
+ {
+ throw new RepositoryException(e);
+ }
+ }
+ }
+ }
+ else
+ {
+ throw new RepositoryException("Datasource '" + dbSourceName
+ "' is not bound in this context.");
+ }
+ }
+ else
+ {
+ this.dbDialect = pDbDialect;
+ }
+ }
+ LOG.info("Using a dialect '" + this.dbDialect + "'");
+ }
+
+ public String getContainerName()
+ {
+ return containerName;
+ }
+
+ public String getDbSourceName()
+ {
+ return dbSourceName;
+ }
+
+ public boolean isMultiDb()
+ {
+ return multiDb;
+ }
+
+ public String getDbDriver()
+ {
+ return dbDriver;
+ }
+
+ public String getDbDialect()
+ {
+ return dbDialect;
+ }
+
+ public String getDbUrl()
+ {
+ return dbUrl;
+ }
+
+ public String getDbUserName()
+ {
+ return dbUserName;
+ }
+
+ public String getDbPassword()
+ {
+ return dbPassword;
+ }
+
+ protected String validateDialect(String confParam)
+ {
+ for (String dbType : DBConstants.DB_DIALECTS)
+ {
+ if (dbType.equalsIgnoreCase(confParam))
+ {
+ return dbType;
+ }
+ }
+
+ return DBConstants.DB_DIALECT_GENERIC; // by default
+ }
+
+}