Finished (with hacks) MOB-30: Purge Deleted Content

- Only for use with RM use-cases
 - Switch on with property: system.content.eagerOrphanCleanup=true

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14088 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-04-27 13:03:18 +00:00
parent 137df81980
commit 31541e5409
10 changed files with 1248 additions and 618 deletions

View File

@@ -28,50 +28,110 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.avm.AVMNodeDAO;
import org.alfresco.repo.avm.AVMNodeDAO.ContentUrlHandler;
import org.alfresco.repo.content.ContentServicePolicies;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.copy.CopyServicePolicies;
import org.alfresco.repo.domain.ContentUrlDAO;
import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.node.db.NodeDaoService.NodePropertyHandler;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.repo.transaction.TransactionalResourceHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyCheck;
import org.alfresco.util.VmShutdownListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This component is responsible for finding orphaned content in a given
* content store or stores. Deletion handlers can be provided to ensure
* that the content is moved to another location prior to being removed
* from the store(s) being cleaned.
* This component is responsible cleaning up orphaned content.
* <p/>
* Clean-up happens at two levels.<p/>
* <u><b>Eager cleanup:</b></u> (since 3.2)<p/>
* If {@link #setEagerOrphanCleanup(boolean) eager cleanup} is activated, then this
* component listens to all content property change events and recorded for post-transaction
* processing. All orphaned content is deleted from the registered store(s). Note that
* any {@link #setListeners(List) listeners} are called as normal; backup or scrubbing
* procedures should be plugged in as listeners if this is required.
* <p/>
* <u><b>Lazy cleanup:</b></u><p/>
* This is triggered by means of a {@link ContentStoreCleanupJob Quartz job}. This is
* a heavy-weight process that effectively compares the database metadata with the
* content URLs controlled by the various stores. Once again, the listeners are called
* appropriately.
* <p/>
* <u><b>How backup policies are affected:</b></u><p/>
* When restoring the system from a backup, the type of restore required is dictated by
* the cleanup policy being enforced. If eager cleanup is active, the system must<br/>
* (a) have a listeners configured to backup the deleted content
* e.g. {@link DeletedContentBackupCleanerListener}, or <br/>
* (b) ensure consistent backups across the database and content stores: backup
* when the system is not running; use a DB-based content store. This is the
* recommended route when running with eager cleanup.
* <p/>
* Lazy cleanup protects the content for a given period (e.g. 7 days) giving plenty of
* time for a backup to be taken; this allows hot backup without needing metadata-content
* consistency to be enforced.
*
* @author Derek Hulley
*/
public class ContentStoreCleaner
extends TransactionListenerAdapter
implements CopyServicePolicies.OnCopyCompletePolicy,
NodeServicePolicies.BeforeDeleteNodePolicy,
ContentServicePolicies.OnContentPropertyUpdatePolicy
{
/**
* Content URLs to delete once the transaction commits.
* @see #onContentPropertyUpdate(NodeRef, QName, ContentData, ContentData)
* @see #afterCommit()
*/
private static final String KEY_POST_COMMIT_DELETION_URLS = "ContentStoreCleaner.PostCommitDeletionUrls";
/**
* Content URLs to delete if the transaction rolls b ack.
* @see #onContentPropertyUpdate(NodeRef, QName, ContentData, ContentData)
* @see #afterRollback()
*/
private static final String KEY_POST_ROLLBACK_DELETION_URLS = "ContentStoreCleaner.PostRollbackDeletionUrls";
private static Log logger = LogFactory.getLog(ContentStoreCleaner.class);
/** kept to notify the thread that it should quit */
private static VmShutdownListener vmShutdownListener = new VmShutdownListener("ContentStoreCleaner");
private DictionaryService dictionaryService;
private PolicyComponent policyComponent;
private ContentService contentService;
private NodeDaoService nodeDaoService;
private AVMNodeDAO avmNodeDAO;
private ContentUrlDAO contentUrlDAO;
private TransactionService transactionService;
private List<ContentStore> stores;
private boolean eagerOrphanCleanup;
private List<ContentStoreCleanerListener> listeners;
private int protectDays;
@@ -90,6 +150,22 @@ public class ContentStoreCleaner
this.dictionaryService = dictionaryService;
}
/**
* @param policyComponent used to register to listen for content updates
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* @param contentService service to copy content binaries
*/
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
/**
* @param nodeDaoService used to get the property values
*/
@@ -130,6 +206,15 @@ public class ContentStoreCleaner
this.stores = stores;
}
/**
*
* @param eagerOrphanCleanup <tt>true</tt> to clean up content
*/
public void setEagerOrphanCleanup(boolean eagerOrphanCleanup)
{
this.eagerOrphanCleanup = eagerOrphanCleanup;
}
/**
* @param listeners the listeners that can react to deletions
*/
@@ -149,12 +234,42 @@ public class ContentStoreCleaner
this.protectDays = protectDays;
}
/**
* Initializes the cleaner based on the {@link #setEagerOrphanCleanup(boolean) eagerCleanup} flag.
*/
public void init()
{
checkProperties();
if (!eagerOrphanCleanup)
{
// Don't register for anything because eager cleanup is disabled
return;
}
// Register for the updates
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onContentPropertyUpdate"),
this,
new JavaBehaviour(this, "onContentPropertyUpdate"));
// TODO: This is a RM-specific hack. Once content properties are separated out, the
// following should be accomplished with a trigger to clean up orphaned content.
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"),
ContentModel.TYPE_CONTENT,
new JavaBehaviour(this, "beforeDeleteNode"));
this.policyComponent.bindClassBehaviour(
QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyComplete"),
ContentModel.TYPE_CONTENT,
new JavaBehaviour(this, "onCopyComplete"));
}
/**
* Perform basic checks to ensure that the necessary dependencies were injected.
*/
private void checkProperties()
{
PropertyCheck.mandatory(this, "dictionaryService", dictionaryService);
PropertyCheck.mandatory(this, "policyComponent", policyComponent);
PropertyCheck.mandatory(this, "contentService", contentService);
PropertyCheck.mandatory(this, "nodeDaoService", nodeDaoService);
PropertyCheck.mandatory(this, "avmNodeDAO", avmNodeDAO);
PropertyCheck.mandatory(this, "contentUrlDAO", contentUrlDAO);
@@ -173,7 +288,185 @@ public class ContentStoreCleaner
"It is possible that in-transaction content will be deleted.");
}
}
/**
* Makes sure that copied files get a new, unshared binary.
*/
public void onCopyComplete(
QName classRef,
NodeRef sourceNodeRef,
NodeRef targetNodeRef,
boolean copyToNewNode,
Map<NodeRef, NodeRef> copyMap)
{
// Get the cm:content of the source
ContentReader sourceReader = contentService.getReader(sourceNodeRef, ContentModel.PROP_CONTENT);
if (sourceReader == null || !sourceReader.exists())
{
// No point attempting to duplicate missing content. We're only interested in cleanin up.
return;
}
// Get the cm:content of the target
ContentReader targetReader = contentService.getReader(targetNodeRef, ContentModel.PROP_CONTENT);
if (targetReader == null || !targetReader.exists())
{
// The target's content is not present, so we don't copy anything over
return;
}
else if (!targetReader.getContentUrl().equals(sourceReader.getContentUrl()))
{
// The target already has a different binary
return;
}
// Create new content for the target node. This will behave just like an update to the node
// but occurs in the same txn as the copy process. Clearly this is a hack and is only
// able to work when properties are copied with all the proper copy-related policies being
// triggered.
ContentWriter targetWriter = contentService.getWriter(targetNodeRef, ContentModel.PROP_CONTENT, true);
targetWriter.putContent(sourceReader);
// This will have triggered deletion of the source node's content because the target node
// is being updated. Force the source node's content to be protected.
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_COMMIT_DELETION_URLS);
urlsToDelete.remove(sourceReader.getContentUrl());
}
/**
* Records the content URLs of <b>cm:content</b> for post-commit cleanup.
*/
public void beforeDeleteNode(NodeRef nodeRef)
{
// Get the cm:content property
Pair<Long, NodeRef> nodePair = nodeDaoService.getNodePair(nodeRef);
if (nodePair == null)
{
return;
}
ContentData contentData = (ContentData) nodeDaoService.getNodeProperty(
nodePair.getFirst(), ContentModel.PROP_CONTENT);
if (contentData == null || !ContentData.hasContent(contentData))
{
return;
}
String contentUrl = contentData.getContentUrl();
// Bind it to the list
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_COMMIT_DELETION_URLS);
urlsToDelete.add(contentUrl);
AlfrescoTransactionSupport.bindListener(this);
}
/**
* Checks for {@link #setEagerOrphanCleanup(boolean) eager cleanup} and pushes the old content URL into
* a list for post-transaction deletion.
*/
public void onContentPropertyUpdate(
NodeRef nodeRef,
QName propertyQName,
ContentData beforeValue,
ContentData afterValue)
{
boolean registerListener = false;
// Bind in URLs to delete when txn commits
if (beforeValue != null && ContentData.hasContent(beforeValue))
{
// Register the URL to delete
String contentUrl = beforeValue.getContentUrl();
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_COMMIT_DELETION_URLS);
urlsToDelete.add(contentUrl);
// Register to listen for transaction commit
registerListener = true;
}
// Bind in URLs to delete when txn rolls back
if (afterValue != null && ContentData.hasContent(afterValue))
{
// Register the URL to delete
String contentUrl = afterValue.getContentUrl();
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_ROLLBACK_DELETION_URLS);
urlsToDelete.add(contentUrl);
// Register to listen for transaction rollback
registerListener = true;
}
// Register listener
if (registerListener)
{
AlfrescoTransactionSupport.bindListener(this);
}
}
/**
* Cleans up all newly-orphaned content
*/
@Override
public void afterCommit()
{
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_COMMIT_DELETION_URLS);
// Debug
if (logger.isDebugEnabled())
{
logger.debug("Post-commit deletion of old content URLs: ");
int count = 0;
for (String contentUrl : urlsToDelete)
{
if (count == 10)
{
logger.debug(" " + (urlsToDelete.size() - 10) + " more ...");
}
else
{
logger.debug(" Deleting content URL: " + contentUrl);
}
count++;
}
}
// Delete
for (String contentUrl : urlsToDelete)
{
for (ContentStore store : stores)
{
for (ContentStoreCleanerListener listener : listeners)
{
listener.beforeDelete(store, contentUrl);
}
store.delete(contentUrl);
}
}
}
@Override
public void afterRollback()
{
Set<String> urlsToDelete = TransactionalResourceHelper.getSet(KEY_POST_ROLLBACK_DELETION_URLS);
// Debug
if (logger.isDebugEnabled())
{
logger.debug("Post-rollback deletion of new content URLs: ");
int count = 0;
for (String contentUrl : urlsToDelete)
{
if (count == 10)
{
logger.debug(" " + (urlsToDelete.size() - 10) + " more ...");
}
else
{
logger.debug(" Deleting content URL: " + contentUrl);
}
count++;
}
}
// Delete
for (String contentUrl : urlsToDelete)
{
for (ContentStore store : stores)
{
for (ContentStoreCleanerListener listener : listeners)
{
listener.beforeDelete(store, contentUrl);
}
store.delete(contentUrl);
}
}
}
private void removeContentUrlsPresentInMetadata()
{
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
@@ -273,7 +566,6 @@ public class ContentStoreCleaner
{
public void handle(String contentUrl)
{
boolean listenersCalled = false;
for (ContentStore store : stores)
{
if (vmShutdownListener.isVmShuttingDown())
@@ -287,15 +579,9 @@ public class ContentStoreCleaner
logger.debug(" Deleting content URL: " + contentUrl);
}
}
// Only transfer the URL to the listeners once
if (!listenersCalled && listeners.size() > 0)
for (ContentStoreCleanerListener listener : listeners)
{
listenersCalled = true;
ContentReader reader = store.getReader(contentUrl);
for (ContentStoreCleanerListener listener : listeners)
{
listener.beforeDelete(reader);
}
listener.beforeDelete(store, contentUrl);
}
// Delete
store.delete(contentUrl);

View File

@@ -24,17 +24,29 @@
*/
package org.alfresco.repo.content.cleanup;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
/**
* A listener that can be plugged into a
* {@link org.alfresco.repo.content.cleanup.ContentStoreCleaner cleaner} to
* move soon-to-be-deleted content to a new location.
* move pre-process any content that is about to be deleted from a store.
* <p>
* Implementations may backup the content or even perform scrubbing or obfuscation
* tasks on the content. In either case, this interface is called when the content
* really will disappear i.e. there is no potential rollback of this operation.
*
* @author Derek Hulley
*/
public interface ContentStoreCleanerListener
{
public void beforeDelete(ContentReader reader) throws ContentIOException;
/**
* Handle the notification that a store is about to be deleted
*
* @param sourceStore the store from which the content will be deleted
* @param contentUrl the URL of the content to be deleted
*
* @since 3.2
*/
public void beforeDelete(ContentStore sourceStore, String contentUrl) throws ContentIOException;
}

View File

@@ -30,7 +30,6 @@ import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Date;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.AbstractContentStore;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.EmptyContentReader;
@@ -38,9 +37,6 @@ import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.filestore.FileContentReader;
import org.alfresco.repo.content.filestore.FileContentStore;
import org.alfresco.repo.content.filestore.FileContentWriter;
import org.alfresco.repo.domain.Node;
import org.alfresco.repo.domain.PropertyValue;
import org.alfresco.repo.domain.hibernate.NodeImpl;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.node.db.NodeDaoService.NodePropertyHandler;
import org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor;
@@ -52,13 +48,11 @@ import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.tools.Repository;
import org.alfresco.tools.ToolException;
import org.alfresco.util.GUID;
import org.alfresco.util.TempFileProvider;
import org.alfresco.util.VmShutdownListener;
import org.apache.commons.lang.mutable.MutableInt;
@@ -217,7 +211,7 @@ public class ContentStoreCleanerScalabilityRunner extends Repository
ContentStoreCleanerListener listener = new ContentStoreCleanerListener()
{
private int count = 0;
public void beforeDelete(ContentReader reader) throws ContentIOException
public void beforeDelete(ContentStore store, String contentUrl) throws ContentIOException
{
count++;
if (count % 1000 == 0)

View File

@@ -24,25 +24,41 @@
*/
package org.alfresco.repo.content.cleanup;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import junit.framework.TestCase;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.avm.AVMNodeDAO;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.filestore.FileContentStore;
import org.alfresco.repo.domain.ContentUrlDAO;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.EqualsHelper;
import org.alfresco.util.GUID;
import org.alfresco.util.Pair;
import org.alfresco.util.TempFileProvider;
import org.springframework.context.ConfigurableApplicationContext;
@@ -55,6 +71,10 @@ public class ContentStoreCleanerTest extends TestCase
{
private static ConfigurableApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private ContentService contentService;
private NodeService nodeService;
private CopyService copyService;
private TransactionService transactionService;
private ContentStoreCleaner cleaner;
private ContentStore store;
private ContentStoreCleanerListener listener;
@@ -63,9 +83,16 @@ public class ContentStoreCleanerTest extends TestCase
@Override
public void setUp() throws Exception
{
AuthenticationUtil.setRunAsUserSystem();
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean("ServiceRegistry");
contentService = serviceRegistry.getContentService();
nodeService = serviceRegistry.getNodeService();
copyService = serviceRegistry.getCopyService();
transactionService = serviceRegistry.getTransactionService();
TransactionService transactionService = serviceRegistry.getTransactionService();
DictionaryService dictionaryService = serviceRegistry.getDictionaryService();
PolicyComponent policyComponent = (PolicyComponent) ctx.getBean("policyComponent");
NodeDaoService nodeDaoService = (NodeDaoService) ctx.getBean("nodeDaoService");
AVMNodeDAO avmNodeDAO = (AVMNodeDAO) ctx.getBean("avmNodeDAO");
ContentUrlDAO contentUrlDAO = (ContentUrlDAO) ctx.getBean("contentUrlDAO");
@@ -81,11 +108,171 @@ public class ContentStoreCleanerTest extends TestCase
cleaner = new ContentStoreCleaner();
cleaner.setTransactionService(transactionService);
cleaner.setDictionaryService(dictionaryService);
cleaner.setPolicyComponent(policyComponent);
cleaner.setNodeDaoService(nodeDaoService);
cleaner.setAvmNodeDAO(avmNodeDAO);
cleaner.setContentUrlDAO(contentUrlDAO);
cleaner.setStores(Collections.singletonList(store));
cleaner.setListeners(Collections.singletonList(listener));
cleaner.setEagerOrphanCleanup(false);
}
public void tearDown() throws Exception
{
AuthenticationUtil.clearCurrentSecurityContext();
}
public void testEagerCleanupOnCommit() throws Exception
{
// Get the context-defined cleaner
ContentStoreCleaner cleaner = (ContentStoreCleaner) ctx.getBean("contentStoreCleaner");
// Force eager cleanup
cleaner.setEagerOrphanCleanup(true);
cleaner.init();
// Create a new file
RetryingTransactionCallback<NodeRef> makeContentCallback = new RetryingTransactionCallback<NodeRef>()
{
public NodeRef execute() throws Throwable
{
// Create some content
StoreRef storeRef = nodeService.createStore("test", "testEagerCleanupOnCommit-" + GUID.generate());
NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
Map<QName, Serializable> properties = Collections.singletonMap(ContentModel.PROP_NAME, (Serializable)"test.txt");
NodeRef contentNodeRef = nodeService.createNode(
rootNodeRef,
ContentModel.ASSOC_CHILDREN,
ContentModel.ASSOC_CHILDREN,
ContentModel.TYPE_CONTENT,
properties).getChildRef();
ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("INITIAL CONTENT");
// Done
return contentNodeRef;
}
};
final NodeRef contentNodeRef = transactionService.getRetryingTransactionHelper().doInTransaction(makeContentCallback);
ContentReader contentReader = contentService.getReader(contentNodeRef, ContentModel.PROP_CONTENT);
assertTrue("Expect content to exist", contentReader.exists());
// Now update the node, but force a failure i.e. txn rollback
final List<String> newContentUrls = new ArrayList<String>();
RetryingTransactionCallback<String> failUpdateCallback = new RetryingTransactionCallback<String>()
{
public String execute() throws Throwable
{
ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("CONTENT FOR FAIL");
// This will have updated the metadata, so we can fail now
newContentUrls.add(writer.getContentUrl());
// Done
throw new RuntimeException("FAIL");
}
};
try
{
transactionService.getRetryingTransactionHelper().doInTransaction(failUpdateCallback);
fail("Transaction was meant to fail");
}
catch (RuntimeException e)
{
// Expected
}
// Make sure that the new content is not there
// The original content must still be there
assertEquals("Expected one content URL to play with", 1, newContentUrls.size());
ContentReader readerMissing = contentService.getRawReader(newContentUrls.get(0));
assertFalse("Newly created content should have been removed.", readerMissing.exists());
assertTrue("Original content should still be there.", contentReader.exists());
// Now update the node successfully
RetryingTransactionCallback<String> successUpdateCallback = new RetryingTransactionCallback<String>()
{
public String execute() throws Throwable
{
ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("CONTENT FOR SUCCESS");
// Done
return writer.getContentUrl();
}
};
String newContentUrl = transactionService.getRetryingTransactionHelper().doInTransaction(successUpdateCallback);
// Make sure that the new content is there
// The original content was disposed of
ContentReader contentReaderNew = contentService.getRawReader(newContentUrl);
assertTrue("Newly created content should be present.", contentReaderNew.exists());
assertFalse("Original content should have been removed.", contentReader.exists());
// Now delete the node
RetryingTransactionCallback<Object> deleteNodeCallback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
nodeService.deleteNode(contentNodeRef);
// Done
return null;
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(deleteNodeCallback);
// The new content must have disappeared
assertFalse("Newly created content should be removed.", contentReaderNew.exists());
}
/**
* TODO: This test must be replaced with one that checks that the raw content binary lives
* as long as there is a reference to it. Once the RM-hacks are removed, content
* will once again be shared and must therefore be cleaned up based on unlinking of
* references.
*/
public void testEagerCleanupAfterCopy() throws Exception
{
// Get the context-defined cleaner
ContentStoreCleaner cleaner = (ContentStoreCleaner) ctx.getBean("contentStoreCleaner");
// Force eager cleanup
cleaner.setEagerOrphanCleanup(true);
cleaner.init();
// Create a new file, copy it
RetryingTransactionCallback<Pair<NodeRef, NodeRef>> copyFileCallback = new RetryingTransactionCallback<Pair<NodeRef, NodeRef>>()
{
public Pair<NodeRef, NodeRef> execute() throws Throwable
{
// Create some content
StoreRef storeRef = nodeService.createStore("test", "testEagerCleanupAfterCopy-" + GUID.generate());
NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
Map<QName, Serializable> properties = Collections.singletonMap(ContentModel.PROP_NAME, (Serializable)"test.txt");
NodeRef contentNodeRef = nodeService.createNode(
rootNodeRef,
ContentModel.ASSOC_CHILDREN,
ContentModel.ASSOC_CHILDREN,
ContentModel.TYPE_CONTENT,
properties).getChildRef();
ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("INITIAL CONTENT");
// Now copy it
NodeRef copiedNodeRef = copyService.copy(
contentNodeRef,
rootNodeRef,
ContentModel.ASSOC_CHILDREN,
ContentModel.ASSOC_CHILDREN);
// Done
return new Pair<NodeRef, NodeRef>(contentNodeRef, copiedNodeRef);
}
};
Pair<NodeRef, NodeRef> nodeRefPair = transactionService.getRetryingTransactionHelper().doInTransaction(copyFileCallback);
// Check that the readers of the content have different URLs
ContentReader contentReaderSource = contentService.getReader(nodeRefPair.getFirst(), ContentModel.PROP_CONTENT);
assertNotNull("Expected reader for source cm:content", contentReaderSource);
assertTrue("Expected content for source cm:content", contentReaderSource.exists());
ContentReader contentReaderCopy = contentService.getReader(nodeRefPair.getSecond(), ContentModel.PROP_CONTENT);
assertNotNull("Expected reader for copy cm:content", contentReaderCopy);
assertTrue("Expected content for copy cm:content", contentReaderCopy.exists());
String contentUrlSource = contentReaderSource.getContentUrl();
String contentUrlCopy = contentReaderCopy.getContentUrl();
assertFalse("Source and copy must have different URLs",
EqualsHelper.nullSafeEquals(contentUrlSource, contentUrlCopy));
}
public void testImmediateRemoval() throws Exception
@@ -145,9 +332,9 @@ public class ContentStoreCleanerTest extends TestCase
private class DummyCleanerListener implements ContentStoreCleanerListener
{
public void beforeDelete(ContentReader reader) throws ContentIOException
public void beforeDelete(ContentStore store, String contentUrl) throws ContentIOException
{
deletedUrls.add(reader.getContentUrl());
deletedUrls.add(contentUrl);
}
}
}

View File

@@ -24,6 +24,7 @@
*/
package org.alfresco.repo.content.cleanup;
import org.alfresco.repo.content.ContentContext;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
@@ -57,18 +58,26 @@ public class DeletedContentBackupCleanerListener implements ContentStoreCleanerL
this.store = store;
}
public void beforeDelete(ContentReader reader) throws ContentIOException
public void beforeDelete(ContentStore sourceStore, String contentUrl) throws ContentIOException
{
ContentContext context = new ContentContext(null, contentUrl);
ContentReader reader = sourceStore.getReader(contentUrl);
if (!reader.exists())
{
// Nothing to copy over
return;
}
// write the content into the target store
ContentWriter writer = store.getWriter(null, reader.getContentUrl());
ContentWriter writer = store.getWriter(context);
// copy across
writer.putContent(reader);
// done
if (logger.isDebugEnabled())
{
logger.debug("Moved content before deletion: \n" +
" URL: " + reader.getContentUrl() + "\n" +
" Store: " + store);
" URL: " + contentUrl + "\n" +
" Source: " + sourceStore + "\n" +
" Target: " + store);
}
}
}