Implement rootObjectPropertyName setting to allow "pure" javascript applications where root object is fetched from a global variable in the scripting engine.

This commit is contained in:
hns 2008-12-18 11:32:58 +00:00
parent 6d3f6c3165
commit eba343b868
4 changed files with 49 additions and 7 deletions

View file

@ -24,6 +24,7 @@ import helma.main.Server;
import helma.objectmodel.*; import helma.objectmodel.*;
import helma.objectmodel.db.*; import helma.objectmodel.db.*;
import helma.util.*; import helma.util.*;
import helma.scripting.ScriptingEngine;
import java.io.*; import java.io.*;
import java.lang.reflect.*; import java.lang.reflect.*;
@ -69,6 +70,9 @@ public final class Application implements Runnable {
// otherwise this is managed by the NodeManager and not cached here. // otherwise this is managed by the NodeManager and not cached here.
Object rootObject = null; Object rootObject = null;
String rootObjectClass; String rootObjectClass;
// if defined this will cause us to get the root object straight
// from the scripting engine, circumventing all hopobject db fluff
String rootObjectPropertyName;
// The session manager // The session manager
SessionManager sessionMgr; SessionManager sessionMgr;
@ -834,7 +838,7 @@ public final class Application implements Runnable {
/** /**
* This method returns the root object of this application's object tree. * This method returns the root object of this application's object tree.
*/ */
public Object getDataRoot() { public Object getDataRoot(ScriptingEngine scriptingEngine) {
// check if we have a custom root object class // check if we have a custom root object class
if (rootObjectClass != null) { if (rootObjectClass != null) {
// create custom root element. // create custom root element.
@ -861,9 +865,11 @@ public final class Application implements Runnable {
} }
return rootObject; return rootObject;
} } else if (rootObjectPropertyName != null) {
// no custom root object is defined - use standard helma objectmodel // get root object from a global scripting engine property
else { return scriptingEngine.getGlobalProperty(rootObjectPropertyName);
} else {
// no custom root object is defined - use standard helma objectmodel
return nmgr.safe.getRootNode(); return nmgr.safe.getRootNode();
} }
} }
@ -1167,7 +1173,7 @@ public final class Application implements Runnable {
* Return the href to the root of this application. * Return the href to the root of this application.
*/ */
public String getRootHref() throws UnsupportedEncodingException { public String getRootHref() throws UnsupportedEncodingException {
return getNodeHref(getDataRoot(), null); return getNodeHref(null, null);
} }
/** /**
@ -1823,6 +1829,7 @@ public final class Application implements Runnable {
} }
hrefRootPrototype = props.getProperty("hrefrootprototype"); hrefRootPrototype = props.getProperty("hrefrootprototype");
rootObjectPropertyName = props.getProperty("rootobjectpropertyname");
// update the XML-RPC access list, containting prototype.method // update the XML-RPC access list, containting prototype.method
// entries of functions that may be called via XML-RPC // entries of functions that may be called via XML-RPC

View file

@ -218,7 +218,7 @@ public final class RequestEvaluator implements Runnable {
transactor = Transactor.getInstance(app.nmgr); transactor = Transactor.getInstance(app.nmgr);
transactor.begin(txname); transactor.begin(txname);
Object root = app.getDataRoot(); Object root = app.getDataRoot(scriptingEngine);
initGlobals(root, requestPath); initGlobals(root, requestPath);
String action = null; String action = null;

View file

@ -116,11 +116,18 @@ public interface ScriptingEngine {
*/ */
public void abort(); public void abort();
/**
* Get a global property
* @param propertyName the property name
* @return the property value, or null
*/
public Object getGlobalProperty(String propertyName);
/** /**
* Get a property on an object * Get a property on an object
* @param thisObject the object * @param thisObject the object
* @param propertyName the property name * @param propertyName the property name
* @return true the property value, or null * @return the property value, or null
*/ */
public Object getProperty(Object thisObject, String propertyName); public Object getProperty(Object thisObject, String propertyName);

View file

@ -424,6 +424,34 @@ public class RhinoEngine implements ScriptingEngine {
return wrapped.has(propname, wrapped); return wrapped.has(propname, wrapped);
} }
/**
* Get a property from the global object.
* @param propname the property name
* @return the property value if the property is defined, or null
*/
public Object getGlobalProperty(String propname) {
if (propname == null) {
return null;
}
try {
Object prop = core.global.get(propname, global);
if (prop == null
|| prop == Undefined.instance
|| prop == ScriptableObject.NOT_FOUND) {
return null;
} else if (prop instanceof Wrapper) {
return ((Wrapper) prop).unwrap();
} else {
// Do not return functions as properties as this
// is a potential security problem
return (prop instanceof Function) ? null : prop;
}
} catch (Exception esx) {
app.logError("Error getting global property " + propname + ": " + esx);
return null;
}
}
/** /**
* Check if an object has a defined property (public field if it * Check if an object has a defined property (public field if it
* is a java object) with that name. * is a java object) with that name.