* Set/Get current RequestEvaluator explicitly via ThreadLocal

* Prevent NullpointerException when scripting engine is initialized
  from non-request-evaluator thread
  (merge from helma_1_5 branch)
This commit is contained in:
hns 2006-10-17 11:44:51 +00:00
parent db45092717
commit cbcc04c1fb
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;
ThreadGroup threadgroup;
// threadlocal variable for the current RequestEvaluator
ThreadLocal currentEvaluator = new ThreadLocal();
// Map of requesttrans -> active requestevaluators
Hashtable activeRequests;
@ -1192,18 +1195,15 @@ public final class Application implements IPathElement, Runnable {
* @return the RequestEvaluator belonging to the current thread
*/
public RequestEvaluator getCurrentRequestEvaluator() {
Thread thread = Thread.currentThread();
int l = allThreads.size();
return (RequestEvaluator) currentEvaluator.get();
}
for (int i = 0; i < l; i++) {
RequestEvaluator r = (RequestEvaluator) allThreads.get(i);
if ((r != null) && (r.getThread() == thread)) {
return r;
}
}
return null;
/**
* Set the current RequestEvaluator for the calling thread.
* @param eval the RequestEvaluator belonging to the current thread
*/
protected void setCurrentRequestEvaluator(RequestEvaluator eval) {
currentEvaluator.set(eval);
}
/**

View file

@ -122,9 +122,10 @@ public final class Prototype {
props.addResource(repository.getResource("type.properties"));
if (update) {
RequestEvaluator eval = app.getCurrentRequestEvaluator();
ScriptingEngine engine = eval == null ? null : eval.scriptingEngine;
Iterator it = repository.getAllResources().iterator();
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.
* @param app the application
*/
public RequestEvaluator(Application app) {
this.app = app;
@ -89,6 +90,7 @@ public final class RequestEvaluator implements Runnable {
String engineClassName = app.getProperty("scriptingEngine",
"helma.scripting.rhino.RhinoEngine");
try {
app.setCurrentRequestEvaluator(this);
Class clazz = app.getClassLoader().loadClass(engineClassName);
scriptingEngine = (ScriptingEngine) clazz.newInstance();
@ -112,6 +114,8 @@ public final class RequestEvaluator implements Runnable {
} else {
throw new RuntimeException(t.toString());
}
} finally {
app.setCurrentRequestEvaluator(null);
}
}
}
@ -151,6 +155,7 @@ public final class RequestEvaluator implements Runnable {
// initialize scripting engine
initScriptingEngine();
app.setCurrentRequestEvaluator(this);
// update scripting prototypes
scriptingEngine.updatePrototypes();
@ -559,6 +564,8 @@ public final class RequestEvaluator implements Runnable {
res.reportError(app.getName(), error);
done = true;
}
} finally {
app.setCurrentRequestEvaluator(null);
}
}