HER-1984 save script state - implement by adding a map to the application context for arbitrary use, and make sure the app context is available in all scripting environments

* PathSharingContext.java
    new member variable ConcurrentHashMap data and accessor getData()
* ScriptedProcessor.java, ScriptedDecideRule.java
    make appCtx available to scripts; also remove unused member sharedMap
* ActionDirectory.java
    formatting fix
This commit is contained in:
Noah Levitt
2012-01-19 13:31:57 -08:00
parent 55ec01c4e4
commit 803f9ca979
4 changed files with 20 additions and 9 deletions
@@ -23,6 +23,7 @@ import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -178,4 +179,18 @@ public class PathSharingContext extends FileSystemXmlApplicationContext {
}
super.initLifecycleProcessor();
}
protected ConcurrentHashMap<Object, Object> data;
/**
* @return a shared map for arbitrary use during a crawl; for example, could
* be used for state persisting for the duration of the crawl,
* shared among ScriptedProcessor, scripting console, etc scripts
*/
public ConcurrentHashMap<Object, Object> getData() {
if (data == null) {
data = new ConcurrentHashMap<Object, Object>();
}
return data;
}
}
@@ -312,7 +312,7 @@ public class ActionDirectory implements ApplicationContextAware, Lifecycle, Runn
PrintWriter rawOut = new PrintWriter(rawString);
Exception ex = null;
engine.put("rawOut", rawOut);
engine.put("appCtx",appCtx);
engine.put("appCtx", appCtx);
// evaluate and record any exception
try {
@@ -20,8 +20,6 @@
package org.archive.modules;
import java.io.Reader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -106,8 +104,6 @@ implements ApplicationContextAware, InitializingBean {
transient protected ThreadLocal<ScriptEngine> threadEngine =
new ThreadLocal<ScriptEngine>();
protected ScriptEngine sharedEngine;
/** map for optional use by scripts */
public Map<Object,Object> sharedMap = new ConcurrentHashMap<Object,Object>();
/**
* Constructor.
@@ -135,12 +131,14 @@ implements ApplicationContextAware, InitializingBean {
// synchronization is harmless for local thread engine,
// necessary for shared engine
engine.put("curi",curi);
engine.put("appCtx", appCtx);
try {
engine.eval("process(curi)");
} catch (ScriptException e) {
logger.log(Level.WARNING,e.getMessage(),e);
} finally {
engine.put("curi", null);
engine.put("appCtx", null);
}
}
}
@@ -20,8 +20,6 @@
package org.archive.modules.deciderules;
import java.io.Reader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -106,8 +104,6 @@ implements ApplicationContextAware, InitializingBean {
transient protected ThreadLocal<ScriptEngine> threadEngine =
new ThreadLocal<ScriptEngine>();
transient protected ScriptEngine sharedEngine;
/** map for optional use by scripts */
public Map<Object,Object> sharedMap = new ConcurrentHashMap<Object,Object>();
public ScriptedDecideRule() {
}
@@ -128,12 +124,14 @@ implements ApplicationContextAware, InitializingBean {
// necessary for shared engine
try {
engine.put("object",uri);
engine.put("appCtx", appCtx);
return (DecideResult)engine.eval("decisionFor(object)");
} catch (ScriptException e) {
logger.log(Level.WARNING,e.getMessage(),e);
return DecideResult.NONE;
} finally {
engine.put("object", null);
engine.put("appCtx", null);
}
}
}