use of org.alfresco.util.MaxSizeMap 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;
}
Aggregations