use of org.alfresco.filesys.repo.rules.operations.DeleteFileOperation in project alfresco-repository by Alfresco.
the class ScenarioCreateShuffleInstance method evaluate.
/**
* Evaluate the next operation
* @param operation Operation
*/
public Command evaluate(Operation operation) {
/**
* Anti-pattern : timeout
*/
Date now = new Date();
if (now.getTime() > startTime.getTime() + getTimeout()) {
if (logger.isDebugEnabled()) {
logger.debug("Instance timed out createName:" + createName);
isComplete = true;
return null;
}
}
/**
* Anti-pattern for all states - delete the file we are
* shuffling
*/
if (createName != null) {
if (operation instanceof DeleteFileOperation) {
DeleteFileOperation d = (DeleteFileOperation) operation;
if (d.getName().equals(createName)) {
if (logger.isDebugEnabled()) {
logger.debug("Anti-pattern : Shuffle file deleted createName:" + createName);
}
isComplete = true;
return null;
}
}
}
switch(internalState) {
case NONE:
// Looking for a create transition
if (operation instanceof CreateFileOperation) {
CreateFileOperation c = (CreateFileOperation) operation;
this.createName = c.getName();
if (logger.isDebugEnabled()) {
logger.debug("entering RENAME state: " + createName);
}
internalState = InternalState.RENAME;
return null;
} else {
// anything else bomb out
if (logger.isDebugEnabled()) {
logger.debug("State error, expected a CREATE");
}
isComplete = true;
}
break;
case RENAME:
/**
* Looking for two renames X(createName) to Y(middle) to Z(end)
*/
if (operation instanceof RenameFileOperation) {
if (logger.isDebugEnabled()) {
logger.debug("Tracking rename: " + operation);
}
RenameFileOperation r = (RenameFileOperation) operation;
renames.put(r.getFrom(), r.getTo());
// Now see if this rename makes a pair.
String middle = renames.get(createName);
if (middle != null) {
if (logger.isDebugEnabled()) {
logger.debug("Got second rename");
}
String end = renames.get(middle);
if (end != null) {
if (logger.isDebugEnabled()) {
logger.debug("Got two renames ");
}
this.move1 = middle;
this.move2 = end;
if (logger.isDebugEnabled()) {
logger.debug("entering DELETE state");
}
internalState = InternalState.DELETE;
/**
* This shuffle reverses the rename out of the way and then copies the
* content only. Finally it moves the temp file into place for the subsequent
* delete.
* a) Rename Z to Y (Reverse previous move)
* b) Copy Content from X to Y
* c) Rename X to Z (move temp file out to old location)
*/
if (logger.isDebugEnabled()) {
logger.debug("Go and shuffle! createName:" + createName + " move1 " + move1 + " move2 " + move2);
}
String[] paths = FileName.splitPath(r.getFromPath());
String oldFolder = paths[0];
// String oldFile = paths[1];
ArrayList<Command> commands = new ArrayList<Command>();
RenameFileCommand r1 = new RenameFileCommand(end, middle, r.getRootNodeRef(), oldFolder + "\\" + end, oldFolder + "\\" + middle);
CopyContentCommand copyContent = new CopyContentCommand(createName, move1, r.getRootNodeRef(), oldFolder + "\\" + createName, oldFolder + "\\" + middle);
RenameFileCommand r2 = new RenameFileCommand(createName, end, r.getRootNodeRef(), oldFolder + "\\" + createName, oldFolder + "\\" + end);
commands.add(r1);
commands.add(copyContent);
commands.add(r2);
return new CompoundCommand(commands);
}
}
}
break;
case DELETE:
if (operation instanceof DeleteFileOperation) {
DeleteFileOperation d = (DeleteFileOperation) operation;
if (d.getName().equals(move2)) {
if (logger.isDebugEnabled()) {
logger.debug("Scenario complete createName:" + createName);
}
isComplete = true;
}
}
/**
* Todo consider create shuffle with backup file which will never
* calls delete - do we need to pattern match on "Backup*".
* At the moment the delete state does nothing - hence
* we can simply set complete here for all situations.
*/
isComplete = true;
break;
}
return null;
}
use of org.alfresco.filesys.repo.rules.operations.DeleteFileOperation in project alfresco-repository by Alfresco.
the class NonTransactionalRuleContentDiskDriver method deleteFile.
@Override
public void deleteFile(SrvSession sess, TreeConnection tree, String name) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("deleteFile name:" + name);
}
try {
ContentContext tctx = (ContentContext) tree.getContext();
NodeRef rootNode = tctx.getRootNode();
DriverState driverState = getDriverState(sess);
String[] paths = FileName.splitPath(name);
String folder = paths[0];
String file = paths[1];
EvaluatorContext ctx = getEvaluatorContext(driverState, folder);
Operation o = new DeleteFileOperation(file, rootNode, name);
Command c = ruleEvaluator.evaluate(ctx, o);
commandExecutor.execute(sess, tree, c);
releaseEvaluatorContextIfEmpty(driverState, ctx, folder);
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ade) {
throw new AccessDeniedException("Unable to delete file " + name, ade);
}
}
use of org.alfresco.filesys.repo.rules.operations.DeleteFileOperation in project alfresco-repository by Alfresco.
the class ScenarioRenameShuffleInstance method evaluate.
/**
* Evaluate the next operation
* @param operation Operation
*/
public Command evaluate(Operation operation) {
/**
* Anti-pattern : timeout
*/
Date now = new Date();
if (now.getTime() > startTime.getTime() + getTimeout()) {
if (logger.isDebugEnabled()) {
logger.debug("Instance timed out");
}
isComplete = true;
return null;
}
switch(state) {
case NONE:
if (operation instanceof RenameFileOperation) {
logger.debug("New scenario initialised");
RenameFileOperation r = (RenameFileOperation) operation;
this.from = r.getFrom();
this.to = r.getTo();
state = InternalState.INITIALISED;
}
break;
case INITIALISED:
if (operation instanceof CreateFileOperation) {
CreateFileOperation c = (CreateFileOperation) operation;
if (from.equals(c.getName())) {
logger.debug("transition to LOOK_FOR_DELETE");
state = InternalState.LOOK_FOR_DELETE;
}
}
break;
case LOOK_FOR_DELETE:
if (operation instanceof DeleteFileOperation) {
DeleteFileOperation d = (DeleteFileOperation) operation;
if (to.equals(d.getName())) {
logger.debug("Rename shuffle complete - fire!");
String[] paths = FileName.splitPath(d.getPath());
String oldFolder = paths[0];
/**
* Shuffle is as follows
* a) Copy content from File to File~
* b) Delete File
* c) Rename File~ to File
*/
ArrayList<Command> commands = new ArrayList<Command>();
CopyContentCommand copyContent = new CopyContentCommand(from, to, d.getRootNodeRef(), oldFolder + "\\" + from, oldFolder + "\\" + to);
RenameFileCommand r1 = new RenameFileCommand(to, from, d.getRootNodeRef(), oldFolder + "\\" + to, oldFolder + "\\" + from);
DeleteFileCommand d1 = new DeleteFileCommand(from, d.getRootNodeRef(), oldFolder + "\\" + from);
commands.add(copyContent);
commands.add(d1);
commands.add(r1);
logger.debug("Scenario complete");
isComplete = true;
return new CompoundCommand(commands);
}
}
}
return null;
}
use of org.alfresco.filesys.repo.rules.operations.DeleteFileOperation in project alfresco-repository by Alfresco.
the class ScenarioTempDeleteShuffle method createInstance.
@Override
public ScenarioInstance createInstance(final EvaluatorContext ctx, Operation operation) {
/**
* This scenario is triggered by a delete of a file matching
* the pattern
*/
if (operation instanceof CreateFileOperation) {
CreateFileOperation c = (CreateFileOperation) operation;
// check whether file is below .TemporaryItems
String path = c.getPath();
// if path contains .TemporaryItems
Matcher d = tempDirPattern.matcher(path);
if (d.matches()) {
logger.debug("pattern matches temp dir folder so this is a new create in a temp dir");
Matcher m = pattern.matcher(c.getName());
if (m.matches()) {
// and how to lock - since we are already have one lock on the scenarios/folder here
// this is a potential deadlock and synchronization bottleneck
Map<String, String> createdTempFiles = (Map<String, String>) ctx.getSessionState().get(SCENARIO_KEY);
if (createdTempFiles == null) {
synchronized (ctx.getSessionState()) {
logger.debug("created new temp file map and added it to the session state");
createdTempFiles = (Map<String, String>) ctx.getSessionState().get(SCENARIO_KEY);
if (createdTempFiles == null) {
createdTempFiles = Collections.synchronizedMap(new MaxSizeMap<String, String>(5, false));
ctx.getSessionState().put(SCENARIO_KEY, createdTempFiles);
}
}
}
createdTempFiles.put(c.getName(), c.getName());
// TODO - Return a different scenario instance here ???
// So it can time out and have anti-patterns etc?
}
}
}
if (operation instanceof MoveFileOperation) {
MoveFileOperation mf = (MoveFileOperation) operation;
// check whether file is below .TemporaryItems
String path = mf.getFromPath();
// if path contains .TemporaryItems
Matcher d = tempDirPattern.matcher(path);
if (d.matches()) {
logger.debug("pattern matches temp dir folder so this is a new create in a temp dir");
Matcher m = pattern.matcher(mf.getFrom());
if (m.matches()) {
// and how to lock - since we are already have one lock on the scenarios/folder here
// this is a potential deadlock and synchronization bottleneck
Map<String, String> createdTempFiles = (Map<String, String>) ctx.getSessionState().get(SCENARIO_KEY);
if (createdTempFiles == null) {
synchronized (ctx.getSessionState()) {
logger.debug("created new temp file map and added it to the session state");
createdTempFiles = (Map<String, String>) ctx.getSessionState().get(SCENARIO_KEY);
if (createdTempFiles == null) {
createdTempFiles = Collections.synchronizedMap(new MaxSizeMap<String, String>(5, false));
ctx.getSessionState().put(SCENARIO_KEY, createdTempFiles);
}
}
}
createdTempFiles.remove(mf.getFrom());
// TODO - Return a different scenario instance here ???
// So it can time out and have anti-patterns etc?
}
}
}
if (operation instanceof DeleteFileOperation) {
DeleteFileOperation c = (DeleteFileOperation) operation;
Matcher m = pattern.matcher(c.getName());
if (m.matches()) {
Map<String, String> createdTempFiles = (Map<String, String>) ctx.getSessionState().get(SCENARIO_KEY);
if (createdTempFiles != null) {
if (createdTempFiles.containsKey(c.getName())) {
if (logger.isDebugEnabled()) {
logger.debug("New Scenario Temp Delete Shuffle Instance:" + c.getName());
}
ScenarioTempDeleteShuffleInstance instance = new ScenarioTempDeleteShuffleInstance();
instance.setTimeout(timeout);
instance.setRanking(ranking);
return instance;
}
}
}
}
// No not interested.
return null;
}
use of org.alfresco.filesys.repo.rules.operations.DeleteFileOperation in project alfresco-repository by Alfresco.
the class ScenarioTempDeleteShuffleInstance method evaluate.
/**
* Evaluate the next operation
* @param operation
*/
public Command evaluate(Operation operation) {
/**
* Anti-pattern : timeout
*/
Date now = new Date();
if (now.getTime() > startTime.getTime() + getTimeout()) {
if (logger.isDebugEnabled()) {
logger.debug("Instance timed out lockName:" + lockName);
isComplete = true;
return null;
}
}
switch(internalState) {
case NONE:
/**
* Looking for target file being deleted
*
* Need to intervene and replace delete with a rename to temp file.
*/
if (operation instanceof DeleteFileOperation) {
DeleteFileOperation d = (DeleteFileOperation) operation;
if (logger.isDebugEnabled()) {
logger.debug("entering DELETE_SUBSTITUTED state: " + lockName);
}
String tempName = ".shuffle" + d.getName();
deletes.put(d.getName(), tempName);
String[] paths = FileName.splitPath(d.getPath());
String currentFolder = paths[0];
RenameFileCommand r1 = new RenameFileCommand(d.getName(), tempName, d.getRootNodeRef(), d.getPath(), currentFolder + "\\" + tempName);
internalState = InternalState.DELETE_SUBSTITUTED;
return r1;
} else {
// anything else bomb out
if (logger.isDebugEnabled()) {
logger.debug("State error, expected a DELETE");
}
isComplete = true;
}
break;
case DELETE_SUBSTITUTED:
/**
* Looking for a move operation of the deleted file
*/
if (operation instanceof MoveFileOperation) {
MoveFileOperation m = (MoveFileOperation) operation;
String targetFile = m.getTo();
if (deletes.containsKey(targetFile)) {
String tempName = deletes.get(targetFile);
String[] paths = FileName.splitPath(m.getToPath());
String currentFolder = paths[0];
/**
* This is where the scenario fires.
* a) Rename the temp file back to the targetFile
* b) Copy content from moved file
* c) Delete rather than move file
*/
logger.debug("scenario fires");
ArrayList<Command> commands = new ArrayList<Command>();
RenameFileCommand r1 = new RenameFileCommand(tempName, targetFile, m.getRootNodeRef(), currentFolder + "\\" + tempName, m.getToPath());
CopyContentCommand copyContent = new CopyContentCommand(m.getFrom(), targetFile, m.getRootNodeRef(), m.getFromPath(), m.getToPath());
DeleteFileCommand d1 = new DeleteFileCommand(m.getFrom(), m.getRootNodeRef(), m.getFromPath());
commands.add(r1);
commands.add(copyContent);
commands.add(d1);
logger.debug("Scenario complete");
isComplete = true;
return new CompoundCommand(commands);
}
}
}
return null;
}
Aggregations