Author: tolusha
Date: 2011-01-19 02:39:43 -0500 (Wed, 19 Jan 2011)
New Revision: 3816
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/CleanException.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/DataCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleanHelper.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/IngresSQLDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/OracleDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/PgSQLDBCleaner.java
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleaner.java
Removed:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/cleaner/
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/cacheable/AbstractCacheableLockManager.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/Backupable.java
Log:
EXOJCR-1146: DBCleanerService should not relate on AbstractCacheableLockManager
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/cacheable/AbstractCacheableLockManager.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/cacheable/AbstractCacheableLockManager.java 2011-01-18
09:18:18 UTC (rev 3815)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/cacheable/AbstractCacheableLockManager.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -17,11 +17,10 @@
package org.exoplatform.services.jcr.impl.core.lock.cacheable;
import org.exoplatform.commons.utils.PrivilegedSystemHelper;
+import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.management.annotations.Managed;
import org.exoplatform.management.annotations.ManagedDescription;
-import org.exoplatform.services.jcr.config.LockManagerEntry;
import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
-import org.exoplatform.services.jcr.config.SimpleParameterEntry;
import org.exoplatform.services.jcr.config.WorkspaceEntry;
import org.exoplatform.services.jcr.dataflow.ChangesLogIterator;
import org.exoplatform.services.jcr.dataflow.CompositeChangesLog;
@@ -42,8 +41,6 @@
import org.exoplatform.services.jcr.impl.core.lock.LockRemover;
import org.exoplatform.services.jcr.impl.core.lock.LockRemoverHolder;
import org.exoplatform.services.jcr.impl.core.lock.SessionLockManager;
-import
org.exoplatform.services.jcr.impl.core.lock.infinispan.ISPNCacheableLockManagerImpl;
-import org.exoplatform.services.jcr.impl.core.lock.jbosscache.CacheableLockManagerImpl;
import org.exoplatform.services.jcr.impl.dataflow.TransientItemData;
import org.exoplatform.services.jcr.impl.dataflow.TransientPropertyData;
import
org.exoplatform.services.jcr.impl.dataflow.persistent.WorkspacePersistentDataManager;
@@ -51,10 +48,13 @@
import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCWorkspaceDataContainer;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.BackupException;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.Backupable;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.CleanException;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.DataCleaner;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.RestoreException;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.BackupTables;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.RestoreTableRule;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.RestoreTables;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.DBCleaner;
import org.exoplatform.services.jcr.observation.ExtendedEvent;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
@@ -65,6 +65,9 @@
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.security.PrivilegedExceptionAction;
+import java.sql.Connection;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -76,6 +79,10 @@
import javax.jcr.RepositoryException;
import javax.jcr.lock.LockException;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
@@ -803,42 +810,6 @@
}
/**
- * Return table name for lock data.
- */
- public static List<String> getLockTableNames(LockManagerEntry lockManagerEntry)
- {
- List<String> tableNames = new ArrayList<String>();
-
- if (lockManagerEntry != null)
- {
- for (SimpleParameterEntry entry : lockManagerEntry.getParameters())
- {
- if
(entry.getName().equals(CacheableLockManagerImpl.JBOSSCACHE_JDBC_TABLE_NAME))
- {
- tableNames.add(entry.getValue());
- tableNames.add(entry.getValue() + "_D");
-
- return tableNames;
- }
- else if
(entry.getName().equals(ISPNCacheableLockManagerImpl.INFINISPAN_JDBC_TABLE_NAME))
- {
- throw new RuntimeException("Not supported");
- }
- }
- }
-
- return tableNames;
- }
-
- /**
- * Return select data script.
- */
- public static String getSelectScript(String tableName)
- {
- return "select * from " + tableName;
- }
-
- /**
* {@inheritDoc}
*/
public void backup(File storageDir) throws BackupException
@@ -883,9 +854,62 @@
RestoreTables restoreTable = new RestoreTables(null, tempDir, maxBufferSize);
restoreTable.restore(storageDir, getDatasourceName(), tables);
}
+
+ /**
+ * {@inheritDoc}
+ */
+ public DataCleaner getDataCleaner() throws CleanException
+ {
+ try
+ {
+ String dsName = getDatasourceName();
+ final DataSource ds = (DataSource)new InitialContext().lookup(dsName);
+ if (ds == null)
+ {
+ throw new NameNotFoundException("Data source " + dsName + "
not found");
+ }
+
+ Connection jdbcConn =
+ SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
+ {
+ public Connection run() throws Exception
+ {
+ return ds.getConnection();
+
+ }
+ });
+
+ List<String> cleanScripts = new ArrayList<String>();
+ for (String tableName : getTableNames())
+ {
+ cleanScripts.add("drop table " + tableName);
+ }
+
+ return new DBCleaner(jdbcConn, cleanScripts);
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ catch (NamingException e)
+ {
+ throw new CleanException(e);
+ }
+ }
+
+ /**
+ * Get list of tables names used by CacheableLockManager.
+ *
+ * @return List
+ */
protected abstract List<String> getTableNames();
+ /**
+ * Get data source name used by CacheableLockManager.
+ *
+ * @return String
+ */
protected abstract String getDatasourceName();
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java 2011-01-18
09:18:18 UTC (rev 3815)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -32,10 +32,17 @@
import org.exoplatform.services.jcr.impl.storage.WorkspaceDataContainerBase;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.BackupException;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.Backupable;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.CleanException;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.DataCleaner;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.RestoreException;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.BackupTables;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.RestoreTableRule;
import org.exoplatform.services.jcr.impl.storage.jdbc.backup.util.RestoreTables;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.DBCleanHelper;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.DBCleaner;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.IngresSQLDBCleaner;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.OracleDBCleaner;
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.PgSQLDBCleaner;
import org.exoplatform.services.jcr.impl.storage.jdbc.db.GenericConnectionFactory;
import org.exoplatform.services.jcr.impl.storage.jdbc.db.HSQLDBConnectionFactory;
import org.exoplatform.services.jcr.impl.storage.jdbc.db.MySQLConnectionFactory;
@@ -66,14 +73,17 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.sql.DataSource;
@@ -1210,4 +1220,90 @@
}
}
+ /**
+ * {@inheritDoc}
+ */
+ public DataCleaner getDataCleaner() throws CleanException
+ {
+ DataCleaner dbCleaner;
+
+ try
+ {
+ final DataSource ds = (DataSource)new InitialContext().lookup(dbSourceName);
+ if (ds == null)
+ {
+ throw new NameNotFoundException("Data source " + dbSourceName +
" not found");
+ }
+
+ Connection jdbcConn =
+ SecurityHelper.doPrivilegedSQLExceptionAction(new
PrivilegedExceptionAction<Connection>()
+ {
+ public Connection run() throws Exception
+ {
+ return ds.getConnection();
+
+ }
+ });
+
+ String dialect = DialectDetecter.detect(jdbcConn.getMetaData());
+
+ List<String> cleanScripts = new ArrayList<String>();
+ if (multiDb)
+ {
+ cleanScripts.add("drop table JCR_MREF");
+ cleanScripts.add("drop table JCR_MVALUE");
+ cleanScripts.add("drop table JCR_MITEM");
+ }
+ else
+ {
+ cleanScripts
+ .add("delete from JCR_SVALUE where exists(select * from JCR_SITEM
where JCR_SITEM.ID=JCR_SVALUE.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME='"
+ + containerName + "')");
+ cleanScripts
+ .add("delete from JCR_SREF where exists(select * from JCR_SITEM where
JCR_SITEM.ID=JCR_SREF.PROPERTY_ID and JCR_SITEM.CONTAINER_NAME='"
+ + containerName + "')");
+ }
+
+ if (!multiDb && dialect.equals(DBConstants.DB_DIALECT_HSQLDB))
+ {
+ cleanScripts.add("delete from JCR_SITEM where I_CLASS=2 and
CONTAINER_NAME='" + containerName + "'");
+
+ dbCleaner = new DBCleaner(jdbcConn, cleanScripts, new
DBCleanHelper(containerName, jdbcConn));
+ }
+ else
+ {
+ if (!multiDb)
+ {
+ cleanScripts.add("delete from JCR_SITEM where
CONTAINER_NAME='" + containerName + "'");
+ }
+
+ if (dialect.equals(DBConstants.DB_DIALECT_PGSQL))
+ {
+ dbCleaner = new PgSQLDBCleaner(jdbcConn, cleanScripts);
+ }
+ else if (dialect.equals(DBConstants.DB_DIALECT_INGRES))
+ {
+ dbCleaner = new IngresSQLDBCleaner(jdbcConn, cleanScripts);
+ }
+ else if (dialect.equals(DBConstants.DB_DIALECT_ORACLE) ||
dialect.equals(DBConstants.DB_DIALECT_ORACLEOCI))
+ {
+ dbCleaner = new OracleDBCleaner(jdbcConn, cleanScripts);
+ }
+ else
+ {
+ dbCleaner = new DBCleaner(jdbcConn, cleanScripts);
+ }
+ }
+
+ return dbCleaner;
+ }
+ catch (NamingException e)
+ {
+ throw new CleanException(e);
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ }
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/Backupable.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/Backupable.java 2011-01-18
09:18:18 UTC (rev 3815)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/Backupable.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -51,4 +51,13 @@
*/
void restore(File storageDir) throws RestoreException;
+ /**
+ * Returns DataCleaner for two-phase cleaning.
+ *
+ * @return DataCleaner
+ * @throws CleanException
+ * if any exception occurred
+ */
+ DataCleaner getDataCleaner() throws CleanException;
+
}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/CleanException.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/CleanException.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/CleanException.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,50 @@
+/*
+ * 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.storage.jdbc.backup;
+
+/**
+ * @author <a href="mailto:anatoliy.bazko@gmail.com">Anatoliy
Bazko</a>
+ * @version $Id: CleanException.java 3679 2010-12-20 09:24:04Z tolusha $
+ */
+public class CleanException extends Exception
+{
+
+ /**
+ * Constructor CleanException.
+ *
+ * @param message
+ * error message
+ * @param cause
+ * caused exception
+ */
+ public CleanException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ /**
+ * Constructor CleanException.
+ *
+ * @param cause
+ * caused exception
+ */
+ public CleanException(Throwable cause)
+ {
+ super(cause);
+ }
+
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/DataCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/DataCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/backup/DataCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,71 @@
+/*
+ * 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.storage.jdbc.backup;
+
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: DataCleaner.java 3193 2010-09-28 07:19:57Z tolusha $
+ */
+public interface DataCleaner
+{
+
+ /**
+ * Clean data from storage. Calls prepare and commit methods.
+ * In case of errors calls rollback.
+ *
+ * @throws CleanException
+ * if any exception is occurred
+ */
+ void clean() throws CleanException;
+
+ /**
+ * Prepare data clean from storage.
+ *
+ * @throws CleanException
+ * if any exception is occurred
+ */
+ void prepare() throws CleanException;
+
+ /**
+ * Commit changes.
+ *
+ * @throws CleanException
+ * if any exception is occurred
+ */
+ void commit() throws CleanException;
+
+ /**
+ * Rollback changes.
+ *
+ * @throws CleanException
+ * if any exception is occurred
+ */
+ void rollback() throws CleanException;
+
+ /**
+ * Close DataCleaner.
+ *
+ * @throws CleanException
+ * if any exception is occurred
+ */
+ void close() throws CleanException;
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleanHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleanHelper.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleanHelper.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,166 @@
+/*
+ * 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.storage.jdbc.cleaner;
+
+import org.exoplatform.commons.utils.SecurityHelper;
+import org.exoplatform.services.jcr.impl.Constants;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.CleanException;
+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 CleanException
+ {
+ 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 CleanException(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.doPrivilegedSQLExceptionAction(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.doPrivilegedSQLExceptionAction(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/storage/jdbc/cleaner/DBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/DBCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,346 @@
+/*
+ * 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.storage.jdbc.cleaner;
+
+import org.exoplatform.commons.utils.SecurityHelper;
+import org.exoplatform.services.jcr.core.security.JCRRuntimePermissions;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.CleanException;
+import org.exoplatform.services.jcr.impl.storage.jdbc.backup.DataCleaner;
+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.ArrayList;
+import java.util.List;
+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 3769 2011-01-04 15:36:06Z areshetnyak $
+ */
+public class DBCleaner implements DataCleaner
+{
+ /**
+ * Logger.
+ */
+ protected final static Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.WorkspaceDBCleaner");
+
+ /**
+ * Connection to database.
+ */
+ protected final Connection connection;
+
+ /**
+ * Pattern for JCR tables.
+ */
+ protected final Pattern dbObjectNamePattern;
+
+ /**
+ * Common clean scripts for database.
+ */
+ protected final List<String> cleanScripts = new ArrayList<String>();
+
+ /**
+ * DB clean helper.
+ */
+ protected final DBCleanHelper dbCleanHelper;
+
+ /**
+ * WorkspaceDBCleaner constructor.
+ *
+ * @param containerName
+ * container name (workspace name)
+ * @param connection
+ * connection to database where workspace tables is placed
+ */
+ public DBCleaner(Connection connection, List<String> cleanScripts)
+ {
+ this(connection, cleanScripts, null);
+ }
+
+ /**
+ * WorkspaceDBCleaner constructor.
+ *
+ * @param containerName
+ * container name (workspace name)
+ * @param connection
+ * connection to database where workspace tables is placed
+ * @param dbCleanHelper
+ * TODO
+ */
+ public DBCleaner(Connection connection, List<String> cleanScripts, DBCleanHelper
dbCleanHelper)
+ {
+ this.dbObjectNamePattern = Pattern.compile(DBInitializer.SQL_OBJECTNAME,
Pattern.CASE_INSENSITIVE);
+ this.connection = connection;
+ this.cleanScripts.addAll(cleanScripts);
+ this.dbCleanHelper = dbCleanHelper;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void prepare() throws CleanException
+ {
+ 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 : getDBCleanScripts())
+ {
+ 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);
+ }
+ }
+
+ if (dbCleanHelper != null)
+ {
+ dbCleanHelper.clean();
+ }
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ finally
+ {
+ if (st != null)
+ {
+ try
+ {
+ st.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.error("Can't close the Statement." + e);
+ }
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void commit() throws CleanException
+ {
+ try
+ {
+ connection.commit();
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void rollback() throws CleanException
+ {
+ try
+ {
+ connection.rollback();
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void close() throws CleanException
+ {
+ try
+ {
+ connection.close();
+ }
+ catch (SQLException e)
+ {
+ throw new CleanException(e);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void clean() throws CleanException
+ {
+ CleanException cleanExc = null;
+
+ try
+ {
+ prepare();
+ commit();
+ }
+ catch (CleanException e)
+ {
+ cleanExc = e;
+ try
+ {
+ rollback();
+ }
+ catch (CleanException rollbackExc)
+ {
+ LOG.error("Can't rollback changes", rollbackExc);
+ }
+ throw cleanExc;
+ }
+ finally
+ {
+ try
+ {
+ close();
+ }
+ catch (CleanException e)
+ {
+ if (cleanExc != null)
+ {
+ LOG.error("Can't close DataCleaner", e);
+ throw cleanExc;
+ }
+ else
+ {
+ throw 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.warn("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.doPrivilegedSQLExceptionAction(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;
+ }
+
+ /**
+ * Get SQL scripts for data cleaning.
+ *
+ * @return
+ * List of sql scripts
+ */
+ protected List<String> getDBCleanScripts()
+ {
+ return cleanScripts;
+ }
+}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/IngresSQLDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/IngresSQLDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/IngresSQLDBCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,50 @@
+/*
+ * 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.storage.jdbc.cleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: IngresSQLDBCleaner.java 3655 2010-12-10 08:25:41Z tolusha $
+ */
+public class IngresSQLDBCleaner extends DBCleaner
+{
+
+ /**
+ * OracleSingleDBCleaner constructor.
+ */
+ public IngresSQLDBCleaner(Connection connection, List<String> cleanScripts)
+ {
+ super(connection, cleanScripts);
+ }
+
+ /**
+ * {@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/storage/jdbc/cleaner/OracleDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/OracleDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/OracleDBCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,80 @@
+/*
+ * 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.storage.jdbc.cleaner;
+
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.DBCleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: OracleDBCleaner.java 3655 2010-12-10 08:25:41Z tolusha $
+ */
+public class OracleDBCleaner extends DBCleaner
+{
+
+ /**
+ * OracleSingleDBCleaner constructor.
+ */
+ public OracleDBCleaner(Connection connection, List<String> cleanScripts)
+ {
+ super(connection, cleanScripts);
+ }
+
+ /**
+ * {@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/storage/jdbc/cleaner/PgSQLDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/PgSQLDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/cleaner/PgSQLDBCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,52 @@
+/*
+ * 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.storage.jdbc.cleaner;
+
+import org.exoplatform.services.jcr.impl.storage.jdbc.cleaner.DBCleaner;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: PgSQLDBCleaner.java 3655 2010-12-10 08:25:41Z tolusha $
+ */
+public class PgSQLDBCleaner extends DBCleaner
+{
+
+ /**
+ * OracleSingleDBCleaner constructor.
+ */
+ public PgSQLDBCleaner(Connection connection, List<String> cleanScripts)
+ {
+ super(connection, cleanScripts);
+ }
+
+ /**
+ * {@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/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleaner.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleaner.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleaner.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -0,0 +1,407 @@
+/*
+ * 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.JcrImplBaseTest;
+import org.exoplatform.services.jcr.RepositoryService;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+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.storage.jdbc.backup.Backupable;
+import org.exoplatform.services.jcr.util.IdGenerator;
+import org.exoplatform.services.jcr.util.TesterConfigurationHelper;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+
+/**
+ * This test uses "testdbcleaner" datasource to create own test repository with
workspace.
+ * So, please, check test-configuration.xml or test-configuration-sjdbc.xml does such
datasource binded.
+ *
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date:
+ *
+ * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
+ * @version $Id: TestRemoveWorkspace.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class TestDBCleaner extends JcrImplBaseTest
+{
+ private final static String DS_NAME = "testdbcleaner";
+
+ private final TesterConfigurationHelper helper;
+
+ private WorkspaceEntry wsEntry;
+
+ public TestDBCleaner()
+ {
+ super();
+ this.helper = TesterConfigurationHelper.getInstence();
+ }
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ wsEntry =
(WorkspaceEntry)session.getContainer().getComponentInstanceOfType(WorkspaceEntry.class);
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ // drop any table
+ DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
+ Connection conn = ds.getConnection();
+ Statement statement = conn.createStatement();
+ try
+ {
+ statement.executeUpdate("drop table JCR_SREF");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_SVALUE");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_SITEM");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_MREF");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_MVALUE");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_MITEM");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_MCONTAINER");
+ }
+ catch (SQLException e)
+ {
+ }
+ try
+ {
+ statement.executeUpdate("drop table JCR_SCONTAINER");
+ }
+ catch (SQLException e)
+ {
+ }
+ if (statement != null)
+ {
+ try
+ {
+ statement.close();
+ }
+ catch (SQLException e)
+ {
+ }
+ }
+ super.tearDown();
+ }
+
+ public void testRemoveRepositoryMultiDB() throws Exception
+ {
+ String repositoryName = "repoTestRemoveMulti";
+
+ RepositoryEntry repositoryEntry = createMultiDB(repositoryName);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+ RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
+ assertTrue(service.canRemoveRepository(repositoryName));
+
+ String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
+ SessionImpl sess = newRepository.getSystemSession(wsName);
+
+ // add nodes to workspaces and check it via datasource
+ NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
+ String id = node.getData().getIdentifier();
+ sess.save();
+ sess.logout();
+
+ DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
+ Connection conn = ds.getConnection();
+ Statement statement = conn.createStatement();
+ ResultSet res = statement.executeQuery("select * from JCR_MITEM where
ID='" + id + "'");
+ assertTrue(res.next());
+
+ // remove content
+ List<Backupable> backupable = new ArrayList<Backupable>();
+ for (String name :
repositoryService.getRepository(repositoryName).getWorkspaceNames())
+ {
+
backupable.addAll(repositoryService.getRepository(repositoryName).getWorkspaceContainer(name)
+ .getComponentInstancesOfType(Backupable.class));
+ }
+
+ for (Backupable component : backupable)
+ {
+ component.getDataCleaner().clean();
+ }
+
+ // check - does JCR_SITEM become empty
+ try
+ {
+ res = statement.executeQuery("select * from JCR_MITEM where ID='"
+ id + "'");
+ fail();
+ }
+ catch (SQLException e)
+ {
+ //ok
+ }
+ statement.close();
+
+ service.removeRepository(repositoryName);
+ }
+
+ public void testRemoveRepositorySingleDB() throws Exception
+ {
+ String repositoryName = "repoTestRemoveSingle";
+
+ RepositoryEntry repositoryEntry = createSingleDB(repositoryName);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+ RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
+ assertTrue(service.canRemoveRepository(repositoryName));
+
+ String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
+ SessionImpl sess = newRepository.getSystemSession(wsName);
+
+ // now add nodes to workspaces and check it via datasource
+ NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
+ String id = node.getData().getIdentifier();
+ sess.save();
+ sess.logout();
+
+ DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
+ Connection conn = ds.getConnection();
+ Statement statement = conn.createStatement();
+ ResultSet res = statement.executeQuery("select * from JCR_SITEM where
ID='" + wsName + id + "'");
+ assertTrue(res.next());
+
+ // remove content
+ List<Backupable> backupable = new ArrayList<Backupable>();
+ for (String name :
repositoryService.getRepository(repositoryName).getWorkspaceNames())
+ {
+
backupable.addAll(repositoryService.getRepository(repositoryName).getWorkspaceContainer(name)
+ .getComponentInstancesOfType(Backupable.class));
+ }
+
+ for (Backupable component : backupable)
+ {
+ component.getDataCleaner().clean();
+ }
+
+ // check - does JCR_SITEM become empty
+ res = statement.executeQuery("select * from JCR_SITEM where ID='" +
wsName + id + "'");
+ assertFalse(res.next());
+ statement.close();
+
+ service.removeRepository(repositoryName);
+ }
+
+ public void testRemoveWorkspaceMultiDB() throws Exception
+ {
+ String repositoryName = "repoTestRemoveMulti";
+
+ RepositoryEntry repositoryEntry = createMultiDB(repositoryName);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+ RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
+ assertTrue(service.canRemoveRepository(repositoryName));
+
+ String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
+ SessionImpl sess = newRepository.getSystemSession(wsName);
+
+ // now add nodes to workspaces and check it via datasource
+ NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
+ String id = node.getData().getIdentifier();
+ sess.save();
+ sess.logout();
+
+ DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
+ Connection conn = ds.getConnection();
+ Statement statement = conn.createStatement();
+ ResultSet res = statement.executeQuery("select * from JCR_MITEM where
ID='" + id + "'");
+ assertTrue(res.next());
+
+ // remove content
+ List<Backupable> backupable =
+ repositoryService.getRepository(repositoryName).getWorkspaceContainer(wsName)
+ .getComponentInstancesOfType(Backupable.class);
+
+ for (Backupable component : backupable)
+ {
+ component.getDataCleaner().clean();
+ }
+
+ // check - does JCR_SITEM become empty
+ try
+ {
+ res = statement.executeQuery("select * from JCR_MITEM where ID='"
+ id + "'");
+ fail();
+ }
+ catch (SQLException e)
+ {
+ //ok
+ }
+ statement.close();
+
+ service.removeRepository(repositoryName);
+ }
+
+ public void testRemoveWorkspaceSingleDB() throws Exception
+ {
+ String repositoryName = "repoTestRemoveSingle";
+
+ RepositoryEntry repositoryEntry = createSingleDB(repositoryName);
+
+ WorkspaceEntry workspaceEntry = repositoryEntry.getWorkspaceEntries().get(0);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+ Session sess =
((RepositoryImpl)service.getRepository(repositoryName)).getSystemSession(workspaceEntry.getName());
+ Node root2 = sess.getRootNode();
+ assertNotNull(root2);
+
+ NodeImpl n = (NodeImpl)root2.addNode("node1");
+ assertTrue(root2.hasNode("node1"));
+
+ String id = n.getData().getIdentifier();
+
+ sess.save();
+
+ n.setProperty("prop", "some value");
+ n.setProperty("prop2", "some value two");
+ sess.save();
+
+ Node n2 = n.addNode("subnode");
+ n2.setProperty("prop", "some value");
+ n2.addNode("subnode1");
+ n2.addNode("subnode2");
+ n2.addNode("subnode2");
+ sess.save();
+ sess.logout();
+
+ DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
+ Connection conn = ds.getConnection();
+ Statement statement = conn.createStatement();
+ ResultSet res =
+ statement.executeQuery("select * from JCR_SITEM where ID='" +
workspaceEntry.getName() + id + "'");
+ assertTrue(res.next());
+
+ // remove content
+ List<Backupable> backupable =
+
repositoryService.getRepository(repositoryName).getWorkspaceContainer(workspaceEntry.getName())
+ .getComponentInstancesOfType(Backupable.class);
+
+ for (Backupable component : backupable)
+ {
+ component.getDataCleaner().clean();
+ }
+
+ // check - does JCR_SITEM become empty
+ res = statement.executeQuery("select * from JCR_SITEM where ID='" +
workspaceEntry.getName() + id + "'");
+ assertFalse(res.next());
+ statement.close();
+
+ service.removeRepository(repositoryName);
+ }
+
+ private RepositoryEntry createMultiDB(String repositoryName) throws Exception
+ {
+ RepositoryEntry repositoryEntry = new RepositoryEntry();
+
+ repositoryEntry.setName(repositoryName);
+ repositoryEntry.setSessionTimeOut(3600000);
+
repositoryEntry.setAuthenticationPolicy("org.exoplatform.services.jcr.impl.core.access.JAASAuthenticator");
+ repositoryEntry.setSecurityDomain("exo-domain");
+ repositoryEntry.setSystemWorkspaceName(repositoryName + "ws");
+ repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
+
+ WorkspaceEntry workspaceEntry =
+ helper.getNewWs(repositoryName + "ws", true, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
+ wsEntry.getContainer(), false);
+
+ repositoryEntry.addWorkspace(workspaceEntry);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+
+ service.createRepository(repositoryEntry);
+ return repositoryEntry;
+ }
+
+ private RepositoryEntry createSingleDB(String repositoryName) throws Exception
+ {
+ RepositoryEntry repositoryEntry = new RepositoryEntry();
+
+ repositoryEntry.setName(repositoryName);
+ repositoryEntry.setSessionTimeOut(3600000);
+
repositoryEntry.setAuthenticationPolicy("org.exoplatform.services.jcr.impl.core.access.JAASAuthenticator");
+ repositoryEntry.setSecurityDomain("exo-domain");
+ repositoryEntry.setSystemWorkspaceName(repositoryName + "ws");
+ repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
+
+ WorkspaceEntry workspaceEntry =
+ helper.getNewWs(repositoryName + "ws", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
+ wsEntry.getContainer(), false);
+
+ repositoryEntry.addWorkspace(workspaceEntry);
+
+ WorkspaceEntry secondWs =
+ helper.getNewWs(repositoryName + "ws2", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
+ wsEntry.getContainer(), false);
+ repositoryEntry.addWorkspace(secondWs);
+
+ RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
+
+ service.createRepository(repositoryEntry);
+ return repositoryEntry;
+ }
+}
Deleted:
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 2011-01-18
09:18:18 UTC (rev 3815)
+++
jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/impl/util/jdbc/TestDBCleanerService.java 2011-01-19
07:39:43 UTC (rev 3816)
@@ -1,371 +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.JcrImplBaseTest;
-import org.exoplatform.services.jcr.RepositoryService;
-import org.exoplatform.services.jcr.config.RepositoryEntry;
-import org.exoplatform.services.jcr.config.WorkspaceEntry;
-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;
-
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import javax.jcr.Node;
-import javax.jcr.Session;
-import javax.naming.InitialContext;
-import javax.sql.DataSource;
-
-/**
- * This test uses "testdbcleaner" datasource to create own test repository with
workspace.
- * So, please, check test-configuration.xml or test-configuration-sjdbc.xml does such
datasource binded.
- *
- * Created by The eXo Platform SAS.
- *
- * <br/>Date:
- *
- * @author <a href="karpenko.sergiy(a)gmail.com">Karpenko Sergiy</a>
- * @version $Id: TestRemoveWorkspace.java 111 2008-11-11 11:11:11Z serg $
- */
-public class TestDBCleanerService extends JcrImplBaseTest
-{
- private final static String DS_NAME = "testdbcleaner";
-
- private final TesterConfigurationHelper helper;
-
- private WorkspaceEntry wsEntry;
-
- public TestDBCleanerService()
- {
- super();
- this.helper = TesterConfigurationHelper.getInstence();
- }
-
- @SuppressWarnings("deprecation")
- @Override
- public void setUp() throws Exception
- {
- super.setUp();
- wsEntry =
(WorkspaceEntry)session.getContainer().getComponentInstanceOfType(WorkspaceEntry.class);
- }
-
- @Override
- public void tearDown() throws Exception
- {
- // drop any table
- DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
- Connection conn = ds.getConnection();
- Statement statement = conn.createStatement();
- try
- {
- statement.executeUpdate("drop table JCR_SREF");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_SVALUE");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_SITEM");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_MREF");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_MVALUE");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_MITEM");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_MCONTAINER");
- }
- catch (SQLException e)
- {
- }
- try
- {
- statement.executeUpdate("drop table JCR_SCONTAINER");
- }
- catch (SQLException e)
- {
- }
- if (statement != null)
- {
- try
- {
- statement.close();
- }
- catch (SQLException e)
- {
- }
- }
- super.tearDown();
- }
-
- public void testRemoveRepositoryMultiDB() throws Exception
- {
- String repositoryName = "repoTestRemoveMulti";
-
- RepositoryEntry repositoryEntry = createMultiDB(repositoryName);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
- RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
- assertTrue(service.canRemoveRepository(repositoryName));
-
- String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
- SessionImpl sess = newRepository.getSystemSession(wsName);
-
- // add nodes to workspaces and check it via datasource
- NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
- String id = node.getData().getIdentifier();
- sess.save();
- sess.logout();
-
- DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
- Connection conn = ds.getConnection();
- Statement statement = conn.createStatement();
- ResultSet res = statement.executeQuery("select * from JCR_MITEM where
ID='" + id + "'");
- assertTrue(res.next());
-
- // remove repository;
- new DBCleanerService().cleanRepositoryData(repositoryEntry);
-
- // check - does JCR_SITEM become empty
- try
- {
- res = statement.executeQuery("select * from JCR_MITEM where ID='"
+ id + "'");
- fail();
- }
- catch (SQLException e)
- {
- //ok
- }
- statement.close();
-
- service.removeRepository(repositoryName);
- }
-
- public void testRemoveRepositorySingleDB() throws Exception
- {
- String repositoryName = "repoTestRemoveSingle";
-
- RepositoryEntry repositoryEntry = createSingleDB(repositoryName);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
- RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
- assertTrue(service.canRemoveRepository(repositoryName));
-
- String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
- SessionImpl sess = newRepository.getSystemSession(wsName);
-
- // now add nodes to workspaces and check it via datasource
- NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
- String id = node.getData().getIdentifier();
- sess.save();
- sess.logout();
-
- DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
- Connection conn = ds.getConnection();
- Statement statement = conn.createStatement();
- ResultSet res = statement.executeQuery("select * from JCR_SITEM where
ID='" + wsName + id + "'");
- assertTrue(res.next());
-
- // remove repository content
- new DBCleanerService().cleanRepositoryData(repositoryEntry);
-
- // check - does JCR_SITEM become empty
- res = statement.executeQuery("select * from JCR_SITEM where ID='" +
wsName + id + "'");
- assertFalse(res.next());
- statement.close();
-
- service.removeRepository(repositoryName);
- }
-
- public void testRemoveWorkspaceMultiDB() throws Exception
- {
- String repositoryName = "repoTestRemoveMulti";
-
- RepositoryEntry repositoryEntry = createMultiDB(repositoryName);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
- RepositoryImpl newRepository =
(RepositoryImpl)service.getRepository(repositoryName);
- assertTrue(service.canRemoveRepository(repositoryName));
-
- String wsName = repositoryEntry.getWorkspaceEntries().get(0).getName();
- SessionImpl sess = newRepository.getSystemSession(wsName);
-
- // now add nodes to workspaces and check it via datasource
- NodeImpl node = (NodeImpl)sess.getRootNode().addNode("testNode");
- String id = node.getData().getIdentifier();
- sess.save();
- sess.logout();
-
- DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
- Connection conn = ds.getConnection();
- Statement statement = conn.createStatement();
- ResultSet res = statement.executeQuery("select * from JCR_MITEM where
ID='" + id + "'");
- assertTrue(res.next());
-
- // remove workspace data from database
- new
DBCleanerService().cleanWorkspaceData(repositoryEntry.getWorkspaceEntries().get(0));
-
- // check - does JCR_SITEM become empty
- try
- {
- res = statement.executeQuery("select * from JCR_MITEM where ID='"
+ id + "'");
- fail();
- }
- catch (SQLException e)
- {
- //ok
- }
- statement.close();
-
- service.removeRepository(repositoryName);
- }
-
- public void testRemoveWorkspaceSingleDB() throws Exception
- {
- String repositoryName = "repoTestRemoveSingle";
-
- RepositoryEntry repositoryEntry = createSingleDB(repositoryName);
-
- WorkspaceEntry workspaceEntry = repositoryEntry.getWorkspaceEntries().get(0);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
- Session sess =
((RepositoryImpl)service.getRepository(repositoryName)).getSystemSession(workspaceEntry.getName());
- Node root2 = sess.getRootNode();
- assertNotNull(root2);
-
- NodeImpl n = (NodeImpl)root2.addNode("node1");
- assertTrue(root2.hasNode("node1"));
-
- String id = n.getData().getIdentifier();
-
- sess.save();
-
- n.setProperty("prop", "some value");
- n.setProperty("prop2", "some value two");
- sess.save();
-
- Node n2 = n.addNode("subnode");
- n2.setProperty("prop", "some value");
- n2.addNode("subnode1");
- n2.addNode("subnode2");
- n2.addNode("subnode2");
- sess.save();
- sess.logout();
-
- DataSource ds = (DataSource)new InitialContext().lookup(DS_NAME);
- Connection conn = ds.getConnection();
- Statement statement = conn.createStatement();
- ResultSet res =
- statement.executeQuery("select * from JCR_SITEM where ID='" +
workspaceEntry.getName() + id + "'");
- assertTrue(res.next());
-
- // remove workspace data from database
- new DBCleanerService().cleanWorkspaceData(workspaceEntry);
-
- // check - does JCR_SITEM become empty
- res = statement.executeQuery("select * from JCR_SITEM where ID='" +
workspaceEntry.getName() + id + "'");
- assertFalse(res.next());
- statement.close();
-
- service.removeRepository(repositoryName);
- }
-
- private RepositoryEntry createMultiDB(String repositoryName) throws Exception
- {
- RepositoryEntry repositoryEntry = new RepositoryEntry();
-
- repositoryEntry.setName(repositoryName);
- repositoryEntry.setSessionTimeOut(3600000);
-
repositoryEntry.setAuthenticationPolicy("org.exoplatform.services.jcr.impl.core.access.JAASAuthenticator");
- repositoryEntry.setSecurityDomain("exo-domain");
- repositoryEntry.setSystemWorkspaceName(repositoryName + "ws");
- repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
-
- WorkspaceEntry workspaceEntry =
- helper.getNewWs(repositoryName + "ws", true, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
- wsEntry.getContainer(), false);
-
- repositoryEntry.addWorkspace(workspaceEntry);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
-
- service.createRepository(repositoryEntry);
- return repositoryEntry;
- }
-
- private RepositoryEntry createSingleDB(String repositoryName) throws Exception
- {
- RepositoryEntry repositoryEntry = new RepositoryEntry();
-
- repositoryEntry.setName(repositoryName);
- repositoryEntry.setSessionTimeOut(3600000);
-
repositoryEntry.setAuthenticationPolicy("org.exoplatform.services.jcr.impl.core.access.JAASAuthenticator");
- repositoryEntry.setSecurityDomain("exo-domain");
- repositoryEntry.setSystemWorkspaceName(repositoryName + "ws");
- repositoryEntry.setDefaultWorkspaceName(repositoryName + "ws");
-
- WorkspaceEntry workspaceEntry =
- helper.getNewWs(repositoryName + "ws", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
- wsEntry.getContainer(), false);
-
- repositoryEntry.addWorkspace(workspaceEntry);
-
- WorkspaceEntry secondWs =
- helper.getNewWs(repositoryName + "ws2", false, DS_NAME,
"target/temp/values/" + IdGenerator.generate(),
- wsEntry.getContainer(), false);
- repositoryEntry.addWorkspace(secondWs);
-
- RepositoryService service =
(RepositoryService)container.getComponentInstanceOfType(RepositoryService.class);
-
- service.createRepository(repositoryEntry);
- return repositoryEntry;
- }
-}