From 784e374f78582263646ea9258e54e9f206881139 Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 5 Nov 2009 10:15:53 +0000 Subject: [PATCH] Better fix for bug #684: Retrieve ScriptingEngine if necessary in Application.getDataRoot(), and make sure root objects set via Application.setDataRoot() are actually used. --- src/helma/framework/core/Application.java | 72 ++++++++++++++--------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index 7a6695c0..becedb7a 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -25,6 +25,7 @@ import helma.objectmodel.*; import helma.objectmodel.db.*; import helma.util.*; import helma.scripting.ScriptingEngine; +import helma.scripting.ScriptingException; import java.io.*; import java.lang.reflect.*; @@ -841,52 +842,65 @@ public final class Application implements Runnable { * This method returns the root object of this application's object tree. */ public Object getDataRoot() throws Exception { - return getDataRoot(getCurrentRequestEvaluator().getScriptingEngine()); + return getDataRoot(null); } /** * This method returns the root object of this application's object tree. */ - public Object getDataRoot(ScriptingEngine scriptingEngine) throws Exception { + protected Object getDataRoot(ScriptingEngine engine) throws Exception { + if (rootObject != null) { + return rootObject; + } // check if we have a custom root object class if (rootObjectClass != null) { // create custom root element. - if (rootObject == null) { - try { - if (classMapping.containsKey("root.factory.class") && - classMapping.containsKey("root.factory.method")) { - String rootFactory = classMapping.getProperty("root.factory.class"); - Class c = typemgr.getClassLoader().loadClass(rootFactory); - Method m = c.getMethod(classMapping.getProperty("root.factory.method"), - (Class[]) null); - - rootObject = m.invoke(c, (Object[]) null); - } else { - String rootClass = classMapping.getProperty("root"); - Class c = typemgr.getClassLoader().loadClass(rootClass); - - rootObject = c.newInstance(); - } - } catch (Exception e) { - throw new RuntimeException("Error creating root object: " + - e.toString()); + try { + if (classMapping.containsKey("root.factory.class") && + classMapping.containsKey("root.factory.method")) { + String rootFactory = classMapping.getProperty("root.factory.class"); + Class c = typemgr.getClassLoader().loadClass(rootFactory); + Method m = c.getMethod( + classMapping.getProperty("root.factory.method"), + (Class[]) null); + rootObject = m.invoke(c, (Object[]) null); + } else { + String rootClass = classMapping.getProperty("root"); + Class c = typemgr.getClassLoader().loadClass(rootClass); + rootObject = c.newInstance(); } + } catch (Exception e) { + throw new RuntimeException("Error creating root object: " + + e.toString()); } - return rootObject; - } else if (rootObjectPropertyName != null) { - // get root object from a global scripting engine property - return scriptingEngine.getGlobalProperty(rootObjectPropertyName); - } else if (rootObjectFunctionName != null) { - // get root object from a global script engine function - return scriptingEngine.invoke(null, rootObjectFunctionName, - RequestEvaluator.EMPTY_ARGS, ScriptingEngine.ARGS_WRAP_DEFAULT, true); + } else if (rootObjectPropertyName != null || rootObjectFunctionName != null) { + // get root object from a global scripting engine property or function + if (engine == null) { + RequestEvaluator reval = getEvaluator(); + try { + return getDataRootFromEngine(reval.getScriptingEngine()); + } finally { + releaseEvaluator(reval); + } + } else { + return getDataRootFromEngine(engine); + } } else { // no custom root object is defined - use standard helma objectmodel return nmgr.getRootNode(); } } + private Object getDataRootFromEngine(ScriptingEngine engine) + throws ScriptingException { + return rootObjectPropertyName != null ? + engine.getGlobalProperty(rootObjectPropertyName) : + engine.invoke(null, rootObjectFunctionName, + RequestEvaluator.EMPTY_ARGS, + ScriptingEngine.ARGS_WRAP_DEFAULT, true); + } + /** * Return the prototype of the object to be used as this application's root object */