Author: tolusha
Date: 2010-09-28 03:16:29 -0400 (Tue, 28 Sep 2010)
New Revision: 3192
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanHelper.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerException.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerService.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/HSQLSingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLMultiDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLSingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/JDBCConfiguration.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MultiDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MySQLSingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleMultiDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleSingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLMultiDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLSingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/SingleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/WorkspaceDBCleaner.java
Removed:
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/DBCleanerException.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/IngresSQLDBCleaner.java
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/OracleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/PgSQLDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/resources/conf/storage/cleanup/
Modified:
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/util/TesterConfigurationHelper.java
Log:
EXOJCR-939: refactoring
Deleted:
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 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,433 +0,0 @@
-/*
- * 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.core.security.JCRRuntimePermissions;
-import org.exoplatform.services.jcr.impl.util.SecurityHelper;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.security.PrivilegedExceptionAction;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * 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
-{
- public final String CLEAN_JCR_SITEM_AS_DEFAULT =
"/*$CLEAN_JCR_SITEM_DEFAULT*/";
-
- protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.DBCleaner");
-
- protected final int MAX_IDS_RETURNED = 100;
-
- protected String GET_CHILD_IDS;
-
- protected String REMOVE_ITEMS;
-
- protected final String containerName;
-
- protected final Pattern dbObjectNamePattern;
-
- protected final Connection connection;
-
- protected String[] scripts;
-
- protected final boolean isMultiDB;
-
- /**
- * Constructor.
- *
- * @param containerName - container name (a.k.a workspace name)
- * @param connection - connection to database where workspace tables is placed
- * @param inputStream - inputStream from script file
- * @param isMultiDB - isMultiDB
- * @throws IOException - if exception occures on parsing script file input stream
- */
- public DBCleaner(String containerName, Connection connection, InputStream inputStream,
boolean isMultiDB)
- throws IOException
- {
- this.dbObjectNamePattern = Pattern.compile(DBInitializer.SQL_OBJECTNAME,
Pattern.CASE_INSENSITIVE);
- this.connection = connection;
- // parse script
- this.scripts = readScriptResource(inputStream);
- this.containerName = containerName;
- this.isMultiDB = isMultiDB;
- prepareQueries();
- }
-
- protected void prepareQueries()
- {
- GET_CHILD_IDS =
- "select ID from JCR_SITEM where CONTAINER_NAME=? and ID not in(select
PARENT_ID from JCR_SITEM where CONTAINER_NAME=?)";
-
- REMOVE_ITEMS = "delete from JCR_SITEM where ID in( ? )";
- }
-
- /**
- * Remove workspace data from database.
- * <ul>
- * <li>If workspace uses multiDB data source - tables associated with this
workspace
- * will be dropped.
- * <li>If workspace uses singleDB data source - all records of this workspace
will
- * be removed.
- * </ul>
- *
- * <p>Connection used by this method will be closed at final.
- *
- * @throws DBCleanerException - if exception during data cleanup occures.
- */
- public void cleanWorkspace() throws DBCleanerException
- {
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- {
- security.checkPermission(JCRRuntimePermissions.MANAGE_REPOSITORY_PERMISSION);
- }
-
- String sql = null;
- Statement st = null;
- try
- {
- connection.setAutoCommit(false);
- st = connection.createStatement();
- for (String scr : scripts)
- {
- String s = cleanWhitespaces(scr.trim());
- if (s.length() > 0)
- {
- if (!canExecuteQuery(connection, sql = s))
- {
- // table from query not found , so try drop other
- continue;
- }
-
- if (LOG.isDebugEnabled())
- {
- LOG.debug("Execute script: \n[" + sql + "]");
- }
- executeQuery(st, sql);
- }
- }
-
- connection.commit();
- }
- catch (SQLException e)
- {
- try
- {
- connection.rollback();
- }
- catch (SQLException rollbackException)
- {
- LOG.error("Can not rollback changes after exception " +
e.getMessage(), rollbackException);
- }
- throw new DBCleanerException(e.getMessage(), e);
- }
- finally
- {
- if (st != null)
- {
- try
- {
- st.close();
- }
- catch (SQLException e)
- {
- LOG.error("Can't close the Statement: " + e);
- }
- }
-
- try
- {
- connection.close();
- }
- catch (SQLException e)
- {
- LOG.error("Error of a connection closing. " + e, e);
- }
- }
- }
-
- /**
- * The default implementation of remove rows from JCR_SITEM table.
- * Some database do not support cascade delete, or need special sittings, so
- * query "delete from JCR_SITEM where CONTAINER_NAME=?" may cause constraint
violation exception.
- * This method takes a leafs (child nodes/properties) and remove them till, no one
record is left.
- *
- * @throws SQLException - SQL exception.
- */
- protected void clearItemsByDefault(Connection connection, String containerName) throws
SQLException
- {
- // Remove only child nodes in cycle, till all nodes will be removed.
- // Such algorithm used to avoid any constraint violation exception related to
foreign key.
- PreparedStatement getChildItems = null;
- final Statement removeItems = connection.createStatement();
-
- try
- {
- getChildItems = connection.prepareStatement(GET_CHILD_IDS);
- getChildItems.setString(1, containerName);
- getChildItems.setString(2, containerName);
-
- getChildItems.setMaxRows(MAX_IDS_RETURNED);
-
- do
- {
- final PreparedStatement getChildIds = getChildItems;
- ResultSet result =
- (ResultSet)SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- return getChildIds.executeQuery();
- }
- });
-
- StringBuilder childListBuilder = new StringBuilder();
- if (result.next())
- {
- childListBuilder.append("'" + result.getString(1) +
"'");
- }
- else
- {
- break;
- }
- while (result.next())
- {
- childListBuilder.append(" , '" + result.getString(1) +
"'");
- }
- // now remove nodes;
- final String q = REMOVE_ITEMS.replace("?",
childListBuilder.toString());
- SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- removeItems.executeUpdate(q);
- return null;
- }
- });
- }
- while (true);
- }
- finally
- {
- if (getChildItems != null)
- {
- getChildItems.close();
- getChildItems = null;
- }
- if (removeItems != null)
- {
- removeItems.close();
- }
- }
- }
-
- /**
- * Check can we safely execute query.
- * If tables used in query does not exists ,we can not execute query.
- */
- protected boolean canExecuteQuery(Connection conn, String sql) throws SQLException
- {
- if (!isMultiDB && sql.equalsIgnoreCase(CLEAN_JCR_SITEM_AS_DEFAULT))
- {
- // check queries used in clearItemsByDefault
- if (!canExecuteQuery(conn, GET_CHILD_IDS))
- {
- return false;
- }
- if (!canExecuteQuery(conn, REMOVE_ITEMS))
- {
- return false;
- }
- return true;
- }
- else
- {
- Matcher tMatcher = dbObjectNamePattern.matcher(sql);
- while (tMatcher.find())
- {
- // got table name
- String tableName = sql.substring(tMatcher.start(), tMatcher.end());
- if (!isTableExists(conn, tableName))
- {
- LOG.error("Table [" + tableName + "] from query [" +
sql
- + "] was not found. So query will not be executed , but will try
execute next one.");
- return false;
- }
- }
- return true;
- }
- }
-
- /**
- * Cleans redundant whitespaces from query.
- */
- protected 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;
- }
-
- /**
- * Execute query.
- */
- protected void executeQuery(final Statement statement, String sql) throws
SQLException
- {
- if (!isMultiDB && sql.equalsIgnoreCase(CLEAN_JCR_SITEM_AS_DEFAULT))
- {
- clearItemsByDefault(statement.getConnection(), containerName);
- }
- else
- {
- // in case of singleDB query - check query for "?" mask and replace
it with containerName
- final String q = (containerName != null) ? sql.replace("?",
"'" + containerName + "'") : sql;
- //super.executeQuery(statement, q);
- SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- statement.executeUpdate(q);
- return null;
- }
- });
- }
- }
-
- protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
- {
- ResultSet trs = conn.getMetaData().getTables(null, null, tableName, null);
- try
- {
- boolean res = false;
- while (trs.next())
- {
- res = true; // check for columns/table type matching etc.
- }
- return res;
- }
- finally
- {
- try
- {
- trs.close();
- }
- catch (SQLException e)
- {
- LOG.error("Can't close the ResultSet: " + e);
- }
- }
- }
-
- /**
- * Extracts SQL queries from script file input stream.
- */
- protected String[] readScriptResource(final InputStream is) throws IOException
- {
- //extract string
- PrivilegedAction<InputStreamReader> actionGetReader = new
PrivilegedAction<InputStreamReader>()
- {
- public InputStreamReader run()
- {
- return new InputStreamReader(is);
- }
- };
- InputStreamReader isr = AccessController.doPrivileged(actionGetReader);
-
- String script = null;
- 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);
- }
-
- script = sbuff.toString();
- }
- finally
- {
- try
- {
- is.close();
- }
- catch (IOException e)
- {
- }
- }
-
- // parse scripts
- String[] scripts = null;
- if (script.startsWith(DBInitializer.SQL_DELIMITER_COMMENT_PREFIX))
- {
- // read custom prefix
- try
- {
- String s =
script.substring(DBInitializer.SQL_DELIMITER_COMMENT_PREFIX.length());
- int endOfDelimIndex = s.indexOf("*/");
- String delim = s.substring(0, endOfDelimIndex).trim();
- s = s.substring(endOfDelimIndex + 2).trim();
- scripts = s.split(delim);
- }
- catch (IndexOutOfBoundsException e)
- {
- LOG.warn("Error of parse SQL-script file. Invalid DELIMITER
configuration. Valid format is '"
- + DBInitializer.SQL_DELIMITER_COMMENT_PREFIX
- + "XXX*/' at begin of the SQL-script file, where XXX - DELIMITER
string." + " Spaces will be trimed. ",
- e);
- LOG.info("Using DELIMITER:[" + DBInitializer.SQL_DELIMITER +
"]");
- scripts = script.split(DBInitializer.SQL_DELIMITER);
- }
- }
- else
- {
- scripts = script.split(DBInitializer.SQL_DELIMITER);
- }
- return scripts;
- }
-}
Deleted:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerException.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerException.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerException.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,9 +0,0 @@
-package org.exoplatform.services.jcr.impl.util.jdbc;
-
-public class DBCleanerException extends Exception
-{
- public DBCleanerException(String message, Throwable e)
- {
- super(message, e);
- }
-}
Deleted:
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 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBCleanerService.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,206 +0,0 @@
-/*
- * 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 org.exoplatform.services.jcr.impl.storage.jdbc.DBConstants;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-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;
-
-/**
- * DBCleanerService deliver tools for clean workspace or repository data from database.
- *
- * 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
-{
- protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.DBCleanerService");
-
- /**
- * Remove workspace data.
- * Tables will be removed in case of multiDB, or only record will be removed in case
of singleDb.
- *
- * @param wsConfig - workspace configuration.
- * @throws RepositoryConfigurationException - exception on parsing workspace
configuration
- * @throws NamingException - exception on parsing workspace configuration or getting
DataSource
- * @throws RepositoryException - exception on parsing workspace configuration or data
cleanup
- * @throws IOException - exception on parsing workspace configuration
- */
- 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);
- }
-
- String dbDialect = wsJDBCConfig.getDbDialect();
- final String sqlPath = getScriptPath(dbDialect, wsJDBCConfig.isMultiDb());
- PrivilegedAction<InputStream> action = new
PrivilegedAction<InputStream>()
- {
- public InputStream run()
- {
- return this.getClass().getResourceAsStream(sqlPath);
- }
- };
- InputStream is = AccessController.doPrivileged(action);
-
- DBCleaner cleaner;
- if (dbDialect == DBConstants.DB_DIALECT_ORACLEOCI)
- {
- LOG.warn(DBConstants.DB_DIALECT_ORACLEOCI + " dialect is
experimental!");
- cleaner = new OracleDBCleaner(wsJDBCConfig.getContainerName(), conn, is,
wsJDBCConfig.isMultiDb());
- }
- else if (dbDialect == DBConstants.DB_DIALECT_ORACLE)
- {
- cleaner = new OracleDBCleaner(wsJDBCConfig.getContainerName(), conn, is,
wsJDBCConfig.isMultiDb());
- }
- else if (dbDialect == DBConstants.DB_DIALECT_PGSQL)
- {
- cleaner = new PgSQLDBCleaner(wsJDBCConfig.getContainerName(), conn, is,
wsJDBCConfig.isMultiDb());
- }
- else if (dbDialect == DBConstants.DB_DIALECT_INGRES)
- {
- cleaner = new IngresSQLDBCleaner(wsJDBCConfig.getContainerName(), conn, is,
wsJDBCConfig.isMultiDb());
- }
- else
- {
- //use default DBCleaner
- cleaner = new DBCleaner(wsJDBCConfig.getContainerName(), conn, is,
wsJDBCConfig.isMultiDb());
- }
-
- // clean workspace
- try
- {
- cleaner.cleanWorkspace();
- }
- catch (DBCleanerException e)
- {
- throw new RepositoryException(e.getMessage(), e);
- }
- }
-
- /**
- * Cleanup repository data from database.
- *
- * @param repoConfig - repository configuration
- * @throws RepositoryConfigurationException - exception on parsing workspace
configuration
- * @throws NamingException - exception on parsing workspace configuration or getting
DataSource
- * @throws RepositoryException - exception on parsing workspace configuration or data
cleanup
- * @throws IOException - exception on parsing workspace configuration
- */
- public static void removeRepositoryData(RepositoryEntry repoConfig) throws
RepositoryConfigurationException,
- NamingException, RepositoryException, IOException
- {
- for (WorkspaceEntry wsEntry : repoConfig.getWorkspaceEntries())
- {
- removeWorkspaceData(wsEntry);
- }
- }
-
- /**
- * Make a path to scripts file according to used database dialect.
- * @param dbDialect - database dialect
- * @param multiDb - is multi db
- * @return Path to script file
- */
- private static String getScriptPath(String dbDialect, boolean multiDb)
- {
- String sqlPath = "/conf/storage/cleanup/jcr-" + (multiDb ? "m"
: "s");
- if (dbDialect == DBConstants.DB_DIALECT_ORACLEOCI)
- {
- LOG.warn(DBConstants.DB_DIALECT_ORACLEOCI + " dialect is
experimental!");
- sqlPath = sqlPath + "jdbc.ora.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_ORACLE)
- {
- sqlPath = sqlPath + "jdbc.ora.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_PGSQL)
- {
- sqlPath = sqlPath + "jdbc.pgsql.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_MYSQL)
- {
- sqlPath = sqlPath + "jdbc.mysql.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_MYSQL_UTF8)
- {
- sqlPath = sqlPath + "jdbc.mysql-utf8.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_MSSQL)
- {
- sqlPath = sqlPath + "jdbc.mssql.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_DB2)
- {
- sqlPath = sqlPath + "jdbc.db2.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_DB2V8)
- {
- sqlPath = sqlPath + "jdbc.db2v8.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_SYBASE)
- {
- sqlPath = sqlPath + "jdbc.sybase.sql";
- }
- else if (dbDialect == DBConstants.DB_DIALECT_HSQLDB)
- {
- sqlPath = sqlPath + "jdbc.sql";
- }
- else
- {
- // generic, DB_HSQLDB
- sqlPath = sqlPath + "jdbc.sql";
- }
- return sqlPath;
- }
-}
Deleted:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/IngresSQLDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/IngresSQLDBCleaner.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/IngresSQLDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,51 +0,0 @@
-/*
- * 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.io.IOException;
-import java.io.InputStream;
-import java.sql.Connection;
-import java.sql.SQLException;
-
-/**
- * Created by The eXo Platform SAS.
- *
- * <br/>Date:
- *
- * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
- * @version $Id: IngresSQLDBCleaner.java 111 2008-11-11 11:11:11Z serg $
- */
-public class IngresSQLDBCleaner extends DBCleaner
-{
- /**
- * Constructor.
- */
- public IngresSQLDBCleaner(String containerName, Connection connection, InputStream
inputStream, boolean isMultiDB)
- throws IOException
- {
- super(containerName, connection, inputStream, isMultiDB);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
- {
- return super.isTableExists(conn, tableName.toUpperCase().toLowerCase());
- }
-}
Deleted:
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 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/JDBCConfiguration.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,289 +0,0 @@
-/*
- * 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
- }
-
-}
Deleted:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/OracleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/OracleDBCleaner.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/OracleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,80 +0,0 @@
-/*
- * 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.io.IOException;
-import java.io.InputStream;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-/**
- * Created by The eXo Platform SAS.
- *
- * <br/>Date:
- *
- * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
- * @version $Id: OracleDBCleaner.java 111 2008-11-11 11:11:11Z serg $
- */
-public class OracleDBCleaner extends DBCleaner
-{
-
- /**
- * Constructor.
- */
- public OracleDBCleaner(String containerName, Connection connection, InputStream
inputStream, boolean isMultiDB)
- throws IOException
- {
- super(containerName, connection, inputStream, isMultiDB);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
- {
- Statement st = null;
- try
- {
- st = conn.createStatement();
- st.executeUpdate("SELECT 1 FROM " + tableName);
- return true;
- }
- catch (SQLException e)
- {
- // check: ORA-00942: table or view does not exist
- if (e.getMessage().indexOf("ORA-00942") >= 0)
- return false;
- throw e;
- }
- finally
- {
- if (st != null)
- {
- try
- {
- st.close();
- }
- catch (SQLException e)
- {
- LOG.error("Can't close the Statement: " + e);
- }
- }
- }
- }
-}
Deleted:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/PgSQLDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/PgSQLDBCleaner.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/PgSQLDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -1,51 +0,0 @@
-/*
- * 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.io.IOException;
-import java.io.InputStream;
-import java.sql.Connection;
-import java.sql.SQLException;
-
-/**
- * Created by The eXo Platform SAS.
- *
- * <br/>Date:
- *
- * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
- * @version $Id: PgSQLDBCLeaner.java 111 2008-11-11 11:11:11Z serg $
- */
-public class PgSQLDBCleaner extends DBCleaner
-{
- /**
- * Constructor.
- */
- public PgSQLDBCleaner(String containerName, Connection connection, InputStream
inputStream, boolean isMultiDB)
- throws IOException
- {
- super(containerName, connection, inputStream, isMultiDB);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
- {
- return super.isTableExists(conn, tableName.toUpperCase().toLowerCase());
- }
-}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanHelper.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanHelper.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,165 @@
+/*
+ * 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.cleaner;
+
+import org.exoplatform.services.jcr.impl.Constants;
+import org.exoplatform.services.jcr.impl.util.SecurityHelper;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.security.PrivilegedExceptionAction;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+public class DBCleanHelper
+{
+
+ /**
+ * Logger.
+ */
+ protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.DBCleanHelper");
+
+ /**
+ * SELECT_ITEMS.
+ */
+ private final String SELECT_ITEMS = "select ID from JCR_SITEM where I_CLASS=1 and
CONTAINER_NAME=? and PARENT_ID=?";
+
+ /**
+ * REMOVE_ITEMS.
+ */
+ private final String REMOVE_ITEMS = "delete from JCR_SITEM where I_CLASS=1 and
CONTAINER_NAME=? and PARENT_ID=?";
+
+ /**
+ * Container name.
+ */
+ protected final String containerName;
+
+ /**
+ * Connection to database.
+ */
+ protected final Connection connection;
+
+ /**
+ * DBCleanerHelper constructor.
+ */
+ public DBCleanHelper(String containerName, Connection connection)
+ {
+ this.connection = connection;
+ this.containerName = containerName;
+ }
+
+ /**
+ * Removing rows from JCR_SITEM table. Some database do not support cascade delete,
+ * or need special sittings, so query "delete from JCR_SITEM where
CONTAINER_NAME=?"
+ * may cause constraint violation exception. In such case will be used deleting like
+ * visitor does. First traverse to the bottom of the tree and then go up to the root
+ * and perform deleting children.
+ *
+ * @throws SQLException
+ * SQL exception.
+ */
+ public void clean() throws DBCleanerException
+ {
+ try
+ {
+ connection.setAutoCommit(false);
+
+ recursiveClean(Constants.ROOT_PARENT_UUID);
+
+ connection.commit();
+ }
+ catch (SQLException e)
+ {
+ try
+ {
+ connection.rollback();
+ }
+ catch (SQLException rollbackException)
+ {
+ LOG.error("Can not rollback changes after exception " +
e.getMessage(), rollbackException);
+ }
+ throw new DBCleanerException(e.getMessage(), e);
+ }
+ }
+
+ private void recursiveClean(String parentID) throws SQLException
+ {
+ PreparedStatement selectItems = null;
+ PreparedStatement removeItems = null;
+ ResultSet result = null;
+
+ try
+ {
+ selectItems = connection.prepareStatement(SELECT_ITEMS);
+ selectItems.setString(1, containerName);
+ selectItems.setString(2, parentID);
+
+ final PreparedStatement selectStatement = selectItems;
+ result = (ResultSet)SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ return selectStatement.executeQuery();
+ }
+ });
+
+ // recursive traversing to the bottom of the tree
+ if (result.next())
+ {
+ do
+ {
+ recursiveClean(result.getString(1));
+ }
+ while (result.next());
+
+ // go up to the root and remove all nodes
+ removeItems = connection.prepareStatement(REMOVE_ITEMS);
+ removeItems.setString(1, containerName);
+ removeItems.setString(2, parentID);
+
+ final PreparedStatement deleteStatement = removeItems;
+ SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ deleteStatement.executeUpdate();
+ return null;
+ }
+ });
+ }
+ }
+ finally
+ {
+ if (selectItems != null)
+ {
+ selectItems.close();
+ }
+
+ if (removeItems != null)
+ {
+ removeItems.close();
+ }
+
+ if (result != null)
+ {
+ result.close();
+ }
+ }
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,37 @@
+/*
+ * 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.cleaner;
+
+/**
+ * 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 interface DBCleaner
+{
+
+ /**
+ * Clean data from DB storage.
+ *
+ * @throws DBCleanerException
+ * if any exception is occurred
+ */
+ void clean() throws DBCleanerException;
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerException.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerException.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerException.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,38 @@
+/*
+ * 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.cleaner;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: DBCleanerException.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class DBCleanerException extends Exception
+{
+
+ /**
+ * Constructor DBCleanerException.
+ *
+ * @param message
+ * error message
+ * @param e
+ * caused exception
+ */
+ public DBCleanerException(String message, Exception e)
+ {
+ super(message, e);
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerService.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerService.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/DBCleanerService.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,188 @@
+/*
+ * 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.cleaner;
+
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+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.jcr.impl.storage.jdbc.JDBCWorkspaceDataContainer;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+/**
+ * DBCleanerService deliver tools for clean workspace or repository data from database.
+ *
+ * @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
+{
+ /**
+ * Logger.
+ */
+ protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.DBCleanerService");
+
+ /**
+ * Remove workspace data.
+ * Tables will be removed in case of multiDB, or only record will be removed in case
of singleDb.
+ *
+ * @param wsEntry
+ * workspace configuration
+ * @throws DBCleanerException
+ * if any exception is occurred
+ */
+ public void cleanWorkspaceData(WorkspaceEntry wsEntry) throws DBCleanerException
+ {
+ String sourceName;
+ try
+ {
+ sourceName =
wsEntry.getContainer().getParameterValue(JDBCWorkspaceDataContainer.SOURCE_NAME);
+ if (sourceName == null)
+ {
+ throw new RepositoryConfigurationException("Parameter " +
JDBCWorkspaceDataContainer.SOURCE_NAME
+ + " not found in workspace configuration " +
wsEntry.getName());
+ }
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ throw new DBCleanerException("Can't define " +
JDBCWorkspaceDataContainer.SOURCE_NAME + " parameter", e);
+ }
+
+ boolean isMultiDb;
+ try
+ {
+ String multiDb =
wsEntry.getContainer().getParameterValue(JDBCWorkspaceDataContainer.MULTIDB);
+ if (multiDb == null)
+ {
+ throw new RepositoryConfigurationException("Parameter " +
JDBCWorkspaceDataContainer.MULTIDB
+ + " not found in workspace configuration " +
wsEntry.getName());
+ }
+
+ isMultiDb = Boolean.parseBoolean(multiDb);
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ throw new DBCleanerException("Can't define " +
JDBCWorkspaceDataContainer.MULTIDB + " parameter", e);
+ }
+
+ DataSource ds;
+ try
+ {
+ ds = (DataSource)new InitialContext().lookup(sourceName);
+ }
+ catch (NamingException e)
+ {
+ throw new DBCleanerException("Can't define data source " +
sourceName, e);
+ }
+
+ Connection conn = null;
+ try
+ {
+ conn = ds.getConnection();
+ }
+ catch (SQLException e)
+ {
+ throw new DBCleanerException("Can't open JDBC connection", e);
+ }
+
+ String dbDialect;
+ try
+ {
+ dbDialect = DialectDetecter.detect(conn.getMetaData());
+ }
+ catch (SQLException e)
+ {
+ throw new DBCleanerException("Can't define DB dialect", e);
+ }
+
+ DBCleaner dbCleaner;
+ if (isMultiDb)
+ {
+ if (dbDialect == DBConstants.DB_DIALECT_ORACLEOCI || dbDialect ==
DBConstants.DB_DIALECT_ORACLE)
+ {
+ dbCleaner = new OracleMultiDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_PGSQL)
+ {
+ dbCleaner = new PgSQLMultiDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_INGRES)
+ {
+ dbCleaner = new IngresSQLMultiDBCleaner(wsEntry.getName(), conn);
+ }
+ else
+ {
+ dbCleaner = new MultiDBCleaner(wsEntry.getName(), conn);
+ }
+ }
+ else
+ {
+ if (dbDialect == DBConstants.DB_DIALECT_ORACLEOCI || dbDialect ==
DBConstants.DB_DIALECT_ORACLE)
+ {
+ dbCleaner = new OracleSingleDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_PGSQL)
+ {
+ dbCleaner = new PgSQLSingleDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_INGRES)
+ {
+ dbCleaner = new IngresSQLSingleDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_HSQLDB)
+ {
+ dbCleaner = new HSQLSingleDBCleaner(wsEntry.getName(), conn);
+ }
+ else if (dbDialect == DBConstants.DB_DIALECT_MYSQL || dbDialect ==
DBConstants.DB_DIALECT_MYSQL_UTF8)
+ {
+ dbCleaner = new MySQLSingleDBCleaner(wsEntry.getName(), conn);
+ }
+ else
+ {
+ dbCleaner = new SingleDBCleaner(wsEntry.getName(), conn);
+ }
+ }
+
+ // clean data
+ dbCleaner.clean();
+ }
+
+ /**
+ * Cleanup repository data from database.
+ *
+ * @param repoEntry
+ * repository configuration
+ * @throws DBCleanerException
+ * if any exception is occurred
+ */
+ public void cleanRepositoryData(RepositoryEntry repoEntry) throws DBCleanerException
+ {
+ for (WorkspaceEntry wsEntry : repoEntry.getWorkspaceEntries())
+ {
+ cleanWorkspaceData(wsEntry);
+ }
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/HSQLSingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/HSQLSingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/HSQLSingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,42 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: HSQLSingleDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class HSQLSingleDBCleaner extends SingleDBCleaner
+{
+
+ /**
+ * HSQLSingleDBCleaner constructor.
+ */
+ public HSQLSingleDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection, true);
+
+ this.scripts =
+ new String[]{
+ "delete from JCR_SVALUE where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SVALUE.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SREF where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SREF.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SITEM where I_CLASS=2 and CONTAINER_NAME=?"};
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLMultiDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLMultiDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLMultiDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,46 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: IngresSQLMultiDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class IngresSQLMultiDBCleaner extends MultiDBCleaner
+{
+
+ /**
+ * IngresSQLMultiDBCleaner constructor.
+ */
+ public IngresSQLMultiDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ return super.isTableExists(conn, tableName.toLowerCase());
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLSingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLSingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/IngresSQLSingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,48 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: IngresSQLDBCleaner.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class IngresSQLSingleDBCleaner extends SingleDBCleaner
+{
+ /**
+ * IngresSQLSingleDBCleaner constructor.
+ */
+ public IngresSQLSingleDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ return super.isTableExists(conn, tableName.toLowerCase());
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/JDBCConfiguration.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/JDBCConfiguration.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/JDBCConfiguration.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -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.cleaner;
+
+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
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MultiDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MultiDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MultiDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,37 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: MultiDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class MultiDBCleaner extends WorkspaceDBCleaner
+{
+
+ /**
+ * MultiDBCleaner constructor.
+ */
+ public MultiDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+
+ this.scripts = new String[]{"DROP TABLE JCR_MREF", "DROP TABLE
JCR_MVALUE", "DROP TABLE JCR_MITEM"};
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MySQLSingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MySQLSingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/MySQLSingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,42 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: MySQLSingleDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class MySQLSingleDBCleaner extends SingleDBCleaner
+{
+
+ /**
+ * MySQLSingleDBCleaner constructor.
+ */
+ public MySQLSingleDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection, true);
+
+ this.scripts =
+ new String[]{
+ "delete from JCR_SVALUE where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SVALUE.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SREF where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SREF.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SITEM where I_CLASS=2 and CONTAINER_NAME=?"};
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleMultiDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleMultiDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleMultiDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,74 @@
+/*
+ * 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
+ *
+ */
+package org.exoplatform.services.jcr.impl.util.jdbc.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: OracleMultiDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class OracleMultiDBCleaner extends MultiDBCleaner
+{
+
+ /**
+ * Constructor OracleMultiDBCleaner.
+ */
+ public OracleMultiDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ Statement st = null;
+ try
+ {
+ st = conn.createStatement();
+ st.executeUpdate("SELECT 1 FROM " + tableName);
+ return true;
+ }
+ catch (SQLException e)
+ {
+ // check: ORA-00942: table or view does not exist
+ if (e.getMessage().indexOf("ORA-00942") >= 0)
+ return false;
+ throw e;
+ }
+ finally
+ {
+ if (st != null)
+ {
+ try
+ {
+ st.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the Statement: " + e);
+ }
+ }
+ }
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleSingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleSingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/OracleSingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,77 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: OracleDBCleaner.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class OracleSingleDBCleaner extends SingleDBCleaner
+{
+
+ /**
+ * OracleSingleDBCleaner constructor.
+ */
+ public OracleSingleDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ Statement st = null;
+ try
+ {
+ st = conn.createStatement();
+ st.executeUpdate("SELECT 1 FROM " + tableName);
+ return true;
+ }
+ catch (SQLException e)
+ {
+ // check: ORA-00942: table or view does not exist
+ if (e.getMessage().indexOf("ORA-00942") >= 0)
+ return false;
+ throw e;
+ }
+ finally
+ {
+ if (st != null)
+ {
+ try
+ {
+ st.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the Statement: " + e);
+ }
+ }
+ }
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLMultiDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLMultiDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLMultiDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,46 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: PgSQLMultiDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class PgSQLMultiDBCleaner extends MultiDBCleaner
+{
+
+ /**
+ * Constructor PgSQLMultiDBCleaner.
+ */
+ public PgSQLMultiDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ return super.isTableExists(conn, tableName.toLowerCase());
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLSingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLSingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/PgSQLSingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,48 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: PgSQLDBCLeaner.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class PgSQLSingleDBCleaner extends SingleDBCleaner
+{
+ /**
+ * PgSQLSingleDBCleaner constructor.
+ */
+ public PgSQLSingleDBCleaner(String containerName, Connection connection)
+ {
+ super(containerName, connection);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ return super.isTableExists(conn, tableName.toLowerCase());
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/SingleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/SingleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/SingleDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,102 @@
+/*
+ * 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.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: SingleDBCleaner.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class SingleDBCleaner extends WorkspaceDBCleaner
+{
+
+ /**
+ * Indicates if need to use clean helper.
+ */
+ protected final boolean postHelpClean;
+
+ /**
+ * DB clean helper.
+ */
+ protected final DBCleanHelper dbCleanHelper;
+
+ /**
+ * SingleDBCleaner constructor.
+ */
+ public SingleDBCleaner(String containerName, Connection connection)
+ {
+ this(containerName, connection, false);
+ }
+
+ /**
+ * SingleDBCleaner constructor.
+ */
+ public SingleDBCleaner(String containerName, Connection connection, boolean
postHelpClean)
+ {
+ super(containerName, connection);
+
+ this.postHelpClean = postHelpClean;
+ this.dbCleanHelper = new DBCleanHelper(containerName, connection);
+ this.scripts =
+ new String[]{
+ "delete from JCR_SVALUE where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SVALUE.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SREF where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SREF.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME=?)",
+ "delete from JCR_SITEM where CONTAINER_NAME=?"};
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void clean() throws DBCleanerException
+ {
+ try
+ {
+ super.clean();
+
+ if (postHelpClean)
+ {
+ dbCleanHelper.clean();
+ }
+ }
+ finally
+ {
+ super.closeConnection();
+ }
+ }
+
+ /**
+ * {@inheritDoc}}
+ */
+ @Override
+ protected void closeConnection()
+ {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void executeQuery(Statement statement, String sql) throws SQLException
+ {
+ final String q = (containerName != null) ? sql.replace("?",
"'" + containerName + "'") : sql;
+ super.executeQuery(statement, q);
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/WorkspaceDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/WorkspaceDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/WorkspaceDBCleaner.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -0,0 +1,254 @@
+/*
+ * 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.cleaner;
+
+import org.exoplatform.services.jcr.core.security.JCRRuntimePermissions;
+import org.exoplatform.services.jcr.impl.util.SecurityHelper;
+import org.exoplatform.services.jcr.impl.util.jdbc.DBInitializer;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.security.PrivilegedExceptionAction;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * The goal of this class is removing workspace data from database.
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: DBCleaner.java 111 2008-11-11 11:11:11Z serg $
+ */
+public abstract class WorkspaceDBCleaner implements DBCleaner
+{
+ /**
+ * Logger.
+ */
+ protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.WorkspaceDBCleaner");
+
+ /**
+ * Container name.
+ */
+ protected final String containerName;
+
+ /**
+ * Connection to database.
+ */
+ protected final Connection connection;
+
+ /**
+ * SQL scripts for data cleaning.
+ */
+ protected String[] scripts;
+
+ /**
+ * Pattern for JCR tables.
+ */
+ protected final Pattern dbObjectNamePattern;
+
+ /**
+ * WorkspaceDBCleaner constructor.
+ *
+ * @param containerName
+ * container name (workspace name)
+ * @param connection
+ * connection to database where workspace tables is placed
+ */
+ public WorkspaceDBCleaner(String containerName, Connection connection)
+ {
+ this.dbObjectNamePattern = Pattern.compile(DBInitializer.SQL_OBJECTNAME,
Pattern.CASE_INSENSITIVE);
+ this.connection = connection;
+ this.containerName = containerName;
+ }
+
+ /**
+ * Remove workspace data from database used specified queries.
+ * <p>Connection used by this method will be closed at final.
+ *
+ * @throws DBCleanerException
+ * if exception during data cleanup occurred
+ */
+ public void clean() throws DBCleanerException
+ {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ {
+ security.checkPermission(JCRRuntimePermissions.MANAGE_REPOSITORY_PERMISSION);
+ }
+
+ String sql = null;
+ Statement st = null;
+ try
+ {
+ connection.setAutoCommit(false);
+ st = connection.createStatement();
+ for (String scr : scripts)
+ {
+ String s = cleanWhitespaces(scr.trim());
+ if (s.length() > 0)
+ {
+ if (!canExecuteQuery(sql = s))
+ {
+ // table from query not found , so try drop other
+ continue;
+ }
+
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("Execute script: \n[" + sql + "]");
+ }
+ executeQuery(st, sql);
+ }
+ }
+
+ connection.commit();
+ }
+ catch (SQLException e)
+ {
+ try
+ {
+ connection.rollback();
+ }
+ catch (SQLException rollbackException)
+ {
+ LOG.error("Can not rollback changes after exception " +
e.getMessage(), rollbackException);
+ }
+ throw new DBCleanerException(e.getMessage(), e);
+ }
+ finally
+ {
+ if (st != null)
+ {
+ try
+ {
+ st.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the Statement: " + e);
+ }
+ }
+
+ closeConnection();
+ }
+ }
+
+ /**
+ * Close connection.
+ *
+ * @throws SQLException
+ */
+ protected void closeConnection()
+ {
+ try
+ {
+ connection.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Error of a connection closing. " + e, e);
+ }
+ }
+
+ /**
+ * Check if we can execute query.
+ * If tables used in query does not exists, we can not execute query.
+ */
+ protected boolean canExecuteQuery(String sql) throws SQLException
+ {
+ Matcher tMatcher = dbObjectNamePattern.matcher(sql);
+ while (tMatcher.find())
+ {
+ // get table name
+ String tableName = sql.substring(tMatcher.start(), tMatcher.end());
+ if (!isTableExists(connection, tableName))
+ {
+ LOG.error("Table [" + tableName + "] from query [" + sql
+ + "] was not found. So query will not be executed.");
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Execute query.
+ */
+ protected void executeQuery(final Statement statement, final String sql) throws
SQLException
+ {
+ SecurityHelper.doPriviledgedSQLExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ statement.executeUpdate(sql);
+ return null;
+ }
+ });
+ }
+
+ /**
+ * Indicates if table exists or not.
+ */
+ protected boolean isTableExists(Connection conn, String tableName) throws
SQLException
+ {
+ ResultSet trs = conn.getMetaData().getTables(null, null, tableName, null);
+ try
+ {
+ boolean res = false;
+ while (trs.next())
+ {
+ res = true; // check for columns/table type matching etc.
+ }
+ return res;
+ }
+ finally
+ {
+ try
+ {
+ trs.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the ResultSet: " + e);
+ }
+ }
+ }
+
+ /**
+ * Cleans redundant whitespaces from query.
+ */
+ 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;
+ }
+
+}
Modified:
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -23,6 +23,7 @@
import org.exoplatform.services.jcr.impl.core.NodeImpl;
import org.exoplatform.services.jcr.impl.core.RepositoryImpl;
import org.exoplatform.services.jcr.impl.core.SessionImpl;
+import org.exoplatform.services.jcr.impl.util.jdbc.cleaner.DBCleanerService;
import org.exoplatform.services.jcr.util.IdGenerator;
import org.exoplatform.services.jcr.util.TesterConfigurationHelper;
@@ -69,6 +70,7 @@
wsEntry =
(WorkspaceEntry)session.getContainer().getComponentInstanceOfType(WorkspaceEntry.class);
}
+ @Override
public void tearDown() throws Exception
{
// drop any table
@@ -170,7 +172,7 @@
assertTrue(res.next());
// remove repository;
- DBCleanerService.removeRepositoryData(repositoryEntry);
+ new DBCleanerService().cleanRepositoryData(repositoryEntry);
// check - does JCR_SITEM become empty
try
@@ -213,7 +215,7 @@
assertTrue(res.next());
// remove repository content
- DBCleanerService.removeRepositoryData(repositoryEntry);
+ new DBCleanerService().cleanRepositoryData(repositoryEntry);
// check - does JCR_SITEM become empty
res = statement.executeQuery("select * from JCR_SITEM where ID='" +
wsName + id + "'");
@@ -249,7 +251,7 @@
assertTrue(res.next());
// remove workspace data from database
-
DBCleanerService.removeWorkspaceData(repositoryEntry.getWorkspaceEntries().get(0));
+ new
DBCleanerService().cleanWorkspaceData(repositoryEntry.getWorkspaceEntries().get(0));
// check - does JCR_SITEM become empty
try
@@ -306,7 +308,7 @@
assertTrue(res.next());
// remove workspace data from database
- DBCleanerService.removeWorkspaceData(workspaceEntry);
+ new DBCleanerService().cleanWorkspaceData(workspaceEntry);
// check - does JCR_SITEM become empty
res = statement.executeQuery("select * from JCR_SITEM where ID='" +
workspaceEntry.getName() + id + "'");
@@ -328,8 +330,8 @@
repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
WorkspaceEntry workspaceEntry =
- helper.getNewWs(repositoryName + "ws", true, DS_NAME,
"target/temp/values/" + IdGenerator.generate(), wsEntry
- .getContainer(), false);
+ helper.getNewWs(repositoryName + "ws", true, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
+ wsEntry.getContainer(), false);
repositoryEntry.addWorkspace(workspaceEntry);
@@ -351,8 +353,8 @@
repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
WorkspaceEntry workspaceEntry =
- helper.getNewWs(repositoryName + "ws", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(), wsEntry
- .getContainer(), false);
+ helper.getNewWs(repositoryName + "ws", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
+ wsEntry.getContainer(), false);
repositoryEntry.addWorkspace(workspaceEntry);
Modified:
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/util/TesterConfigurationHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/util/TesterConfigurationHelper.java 2010-09-27
09:20:22 UTC (rev 3191)
+++
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/util/TesterConfigurationHelper.java 2010-09-28
07:16:29 UTC (rev 3192)
@@ -134,7 +134,7 @@
List params = new ArrayList();
- params.add(new SimpleParameterEntry("sourceName", dsName));
+ params.add(new SimpleParameterEntry("source-name", dsName));
params.add(new SimpleParameterEntry("db-type", "generic"));
params.add(new SimpleParameterEntry("multi-db", isMultiDb ?
"true" : "false"));
params.add(new SimpleParameterEntry("update-storage",
"true"));