r9443 - in helma/helma/trunk/src/helma: framework/core scripting scripting/rhino

[email protected]
Newsgroups gmane.comp.java.helma.cvs
Message-ID <[email protected]>
Author: hannes
Date: 2008-12-18 12:32:58 +0100 (Thu, 18 Dec 2008)
New Revision: 9443

Modified:
   helma/helma/trunk/src/helma/framework/core/Application.java
   helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java
   helma/helma/trunk/src/helma/scripting/ScriptingEngine.java
   helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java
Log:
Implement rootObjectPropertyName setting to allow "pure" javascript applications where root object is fetched from a global variable in the scripting engine.

Details at http://dev.helma.org/trac/helma/changeset/9443

Modified: helma/helma/trunk/src/helma/framework/core/Application.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/Application.java	2008-12-17 22:24:51 UTC (rev 9442)
+++ helma/helma/trunk/src/helma/framework/core/Application.java	2008-12-18 11:32:58 UTC (rev 9443)
@@ -24,6 +24,7 @@
 import helma.objectmodel.*;
 import helma.objectmodel.db.*;
 import helma.util.*;
+import helma.scripting.ScriptingEngine;
 
 import java.io.*;
 import java.lang.reflect.*;
@@ -69,6 +70,9 @@
     // otherwise this is managed by the NodeManager and not cached here.
     Object rootObject = null;
     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
     SessionManager sessionMgr;
@@ -834,7 +838,7 @@
     /**
      * 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
         if (rootObjectClass != null) {
             // create custom root element.
@@ -861,9 +865,11 @@
             }
 
             return rootObject;
-        }
-        // no custom root object is defined - use standard helma objectmodel
-        else {
+        } else if (rootObjectPropertyName != null) {
+            // get root object from a global scripting engine property
+            return scriptingEngine.getGlobalProperty(rootObjectPropertyName);
+        } else {
+            // no custom root object is defined - use standard helma objectmodel
             return nmgr.safe.getRootNode();
         }
     }
@@ -1167,7 +1173,7 @@
      *  Return the href to the root of this application.
      */
     public String getRootHref() throws UnsupportedEncodingException {
-        return getNodeHref(getDataRoot(), null);
+        return getNodeHref(null, null);
     }
 
     /**
@@ -1823,6 +1829,7 @@
             }
 
             hrefRootPrototype = props.getProperty("hrefrootprototype");
+            rootObjectPropertyName = props.getProperty("rootobjectpropertyname");
 
             // update the XML-RPC access list, containting prototype.method
             // entries of functions that may be called via XML-RPC

Modified: helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java
===================================================================
--- helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java	2008-12-17 22:24:51 UTC (rev 9442)
+++ helma/helma/trunk/src/helma/framework/core/RequestEvaluator.java	2008-12-18 11:32:58 UTC (rev 9443)
@@ -218,7 +218,7 @@
                         transactor = Transactor.getInstance(app.nmgr);
                         transactor.begin(txname);
 
-                        Object root = app.getDataRoot();
+                        Object root = app.getDataRoot(scriptingEngine);
                         initGlobals(root, requestPath);
 
                         String action = null;

Modified: helma/helma/trunk/src/helma/scripting/ScriptingEngine.java
===================================================================
--- helma/helma/trunk/src/helma/scripting/ScriptingEngine.java	2008-12-17 22:24:51 UTC (rev 9442)
+++ helma/helma/trunk/src/helma/scripting/ScriptingEngine.java	2008-12-18 11:32:58 UTC (rev 9443)
@@ -117,10 +117,17 @@
     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
      * @param thisObject the object
      * @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);
 

Modified: helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java
===================================================================
--- helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java	2008-12-17 22:24:51 UTC (rev 9442)
+++ helma/helma/trunk/src/helma/scripting/rhino/RhinoEngine.java	2008-12-18 11:32:58 UTC (rev 9443)
@@ -425,6 +425,34 @@
     }
 
     /**
+     * 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
      * is a java object) with that name.
      */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.