* Set/Get current RequestEvaluator explicitly via ThreadLocal

* Prevent NullpointerException when scripting engine is initialized
  from non-request-evaluator thread
This commit is contained in:
hns 2006-10-17 09:28:18 +00:00
parent 0470c24db3
commit 1b32fa7cee
3 changed files with 20 additions and 12 deletions

View file

@ -102,6 +102,9 @@ public final class Application implements IPathElement, Runnable {
long requestTimeout = 60000; long requestTimeout = 60000;
ThreadGroup threadgroup; ThreadGroup threadgroup;
// threadlocal variable for the current RequestEvaluator
ThreadLocal currentEvaluator = new ThreadLocal();
// Map of requesttrans -> active requestevaluators // Map of requesttrans -> active requestevaluators
Hashtable activeRequests; Hashtable activeRequests;
@ -1192,18 +1195,15 @@ public final class Application implements IPathElement, Runnable {
* @return the RequestEvaluator belonging to the current thread * @return the RequestEvaluator belonging to the current thread
*/ */
public RequestEvaluator getCurrentRequestEvaluator() { public RequestEvaluator getCurrentRequestEvaluator() {
Thread thread = Thread.currentThread(); return (RequestEvaluator) currentEvaluator.get();
int l = allThreads.size(); }
for (int i = 0; i < l; i++) { /**
RequestEvaluator r = (RequestEvaluator) allThreads.get(i); * Set the current RequestEvaluator for the calling thread.
* @param eval the RequestEvaluator belonging to the current thread
if ((r != null) && (r.getThread() == thread)) { */
return r; protected void setCurrentRequestEvaluator(RequestEvaluator eval) {
} currentEvaluator.set(eval);
}
return null;
} }
/** /**

View file

@ -122,9 +122,10 @@ public final class Prototype {
props.addResource(repository.getResource("type.properties")); props.addResource(repository.getResource("type.properties"));
if (update) { if (update) {
RequestEvaluator eval = app.getCurrentRequestEvaluator(); RequestEvaluator eval = app.getCurrentRequestEvaluator();
ScriptingEngine engine = eval == null ? null : eval.scriptingEngine;
Iterator it = repository.getAllResources().iterator(); Iterator it = repository.getAllResources().iterator();
while (it.hasNext()) { while (it.hasNext()) {
checkResource((Resource) it.next(), eval.scriptingEngine); checkResource((Resource) it.next(), engine);
} }
} }
} }

View file

@ -79,6 +79,7 @@ public final class RequestEvaluator implements Runnable {
/** /**
* Create a new RequestEvaluator for this application. * Create a new RequestEvaluator for this application.
* @param app the application
*/ */
public RequestEvaluator(Application app) { public RequestEvaluator(Application app) {
this.app = app; this.app = app;
@ -89,6 +90,7 @@ public final class RequestEvaluator implements Runnable {
String engineClassName = app.getProperty("scriptingEngine", String engineClassName = app.getProperty("scriptingEngine",
"helma.scripting.rhino.RhinoEngine"); "helma.scripting.rhino.RhinoEngine");
try { try {
app.setCurrentRequestEvaluator(this);
Class clazz = app.getClassLoader().loadClass(engineClassName); Class clazz = app.getClassLoader().loadClass(engineClassName);
scriptingEngine = (ScriptingEngine) clazz.newInstance(); scriptingEngine = (ScriptingEngine) clazz.newInstance();
@ -112,6 +114,8 @@ public final class RequestEvaluator implements Runnable {
} else { } else {
throw new RuntimeException(t.toString()); throw new RuntimeException(t.toString());
} }
} finally {
app.setCurrentRequestEvaluator(null);
} }
} }
} }
@ -151,6 +155,7 @@ public final class RequestEvaluator implements Runnable {
// initialize scripting engine // initialize scripting engine
initScriptingEngine(); initScriptingEngine();
app.setCurrentRequestEvaluator(this);
// update scripting prototypes // update scripting prototypes
scriptingEngine.updatePrototypes(); scriptingEngine.updatePrototypes();
@ -559,6 +564,8 @@ public final class RequestEvaluator implements Runnable {
res.reportError(app.getName(), error); res.reportError(app.getName(), error);
done = true; done = true;
} }
} finally {
app.setCurrentRequestEvaluator(null);
} }
} }