r9568 - in helma-ng/trunk/src/org/helma: javascript tools web

[email protected]
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090403074622.657DF3D0D6@mia>
Author: hannes
Date: 2009-04-03 09:46:22 +0200 (Fri, 03 Apr 2009)
New Revision: 9568

Modified:
   helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java
   helma-ng/trunk/src/org/helma/tools/HelmaConfiguration.java
   helma-ng/trunk/src/org/helma/tools/HelmaRunner.java
   helma-ng/trunk/src/org/helma/web/HelmaServlet.java
Log:
Add initial support for sandboxed JS environments.

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

Modified: helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java
===================================================================
--- helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java	2009-04-03 07:46:07 UTC (rev 9567)
+++ helma-ng/trunk/src/org/helma/javascript/RhinoEngine.java	2009-04-03 07:46:22 UTC (rev 9568)
@@ -71,6 +71,9 @@
         // create a new global scope level
         Context cx = contextFactory.enterContext();
         try {
+            if (config.getClassShutter() != null) {
+                cx.setClassShutter(config.getClassShutter());
+            }
             topLevelScope = new ImporterTopLevel(cx);
             Class[] classes = config.getHostClasses();
             if (classes != null) {
@@ -86,7 +89,9 @@
             ScriptableObject.defineProperty(topLevelScope, "__name__", "global",
                     ScriptableObject.DONTENUM);
             evaluate(cx, getScript("global"), topLevelScope);
-            // topLevelScope.sealObject();
+            if (config.isSealed()) {
+                topLevelScope.sealObject();
+            }
         } catch (Exception x) {
             throw new IllegalArgumentException("Error initializing engine", x);
         } finally {

Modified: helma-ng/trunk/src/org/helma/tools/HelmaConfiguration.java
===================================================================
--- helma-ng/trunk/src/org/helma/tools/HelmaConfiguration.java	2009-04-03 07:46:07 UTC (rev 9567)
+++ helma-ng/trunk/src/org/helma/tools/HelmaConfiguration.java	2009-04-03 07:46:22 UTC (rev 9568)
@@ -19,6 +19,8 @@
 import org.apache.log4j.Logger;
 import org.helma.repository.*;
 import org.helma.util.StringUtils;
+import org.helma.tools.launcher.HelmaClassLoader;
+import org.mozilla.javascript.ClassShutter;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -37,9 +39,19 @@
     int optimizationLevel = 0;
     int languageVersion = 180;
     Class<?>[] hostClasses = null;
-    org.helma.tools.launcher.HelmaClassLoader loader;
+    HelmaClassLoader loader;
+    ClassShutter classShutter = null;
+    boolean sealed = false;
 
-    public HelmaConfiguration(Repository helmaHome, String modulePath, String scriptName)
+    /**
+     * Create a new Helma configuration and sets up its module search path.
+     *
+     * @param helmaHome the helma installation directory
+     * @param modulePath the module search path as comma separated string
+     * @param systemModules system module path to append to module path, or null
+     * @throws FileNotFoundException if a moudule path item does not exist
+     */
+    public HelmaConfiguration(Repository helmaHome, String modulePath, String systemModules)
             throws FileNotFoundException {
         repositories = new ArrayList<Repository>();
         home = helmaHome;
@@ -53,10 +65,6 @@
             languageVersion = Integer.parseInt(langVersion);
         }
 
-        // first add repositories from helma.modulepath system property
-        if (modulePath == null) {
-            modulePath = System.getProperty("helma.modulepath");
-        }
         if (modulePath != null) {
             String[] paths = StringUtils.split(modulePath, ",");
             for (int i = 0; i < paths.length; i++) {
@@ -87,15 +95,28 @@
             }
         }
 
-        // next, always add modules from helma home
-        Repository modules = home.getChildRepository("modules");
-        repositories.add(modules);
+        // append system modules path relative to helma home
+        if (systemModules != null) {
+            Repository modules = home.getChildRepository(systemModules);
+            repositories.add(modules);
+        }
 
-        // finally add script's parent directory to repository path,
+        Logger.getLogger("org.helma.tools").debug("Parsed repository list: " + repositories);
+    }
+
+    /**
+     * If the scriptName argument is not null, prepend the script's parent repository
+     * to the module path. Otherwise, prepend the current working directory to the module path.
+     * @param scriptName the name of the script, or null.
+     * @throws FileNotFoundException if the script repository does not exist
+     */
+    public void addScriptRepository(String scriptName) throws FileNotFoundException {
+        // add script's parent directory to repository path,
         // or the current directory if no script is run
         if (scriptName != null) {
             Resource script = new FileResource(new File(scriptName));
-            if (!script.exists()) {
+            // check if the script can be found in the module path
+            if (!script.exists() && !repositories.isEmpty()) {
                 script = getResource(scriptName);
                 if (!script.exists()) {
                     scriptName = scriptName.replace('.', File.separatorChar) + ".js";
@@ -112,8 +133,6 @@
             // no script file, add current directory to module search path
             repositories.add(0, new FileRepository(new File(".")));
         }
-
-        Logger.getLogger("org.helma.tools").debug("Parsed repository list: " + repositories);
     }
 
     /**
@@ -229,4 +248,20 @@
         return list;
     }
 
+    public ClassShutter getClassShutter() {
+        return classShutter;
+    }
+
+    public void setClassShutter(ClassShutter classShutter) {
+        this.classShutter = classShutter;
+    }
+
+    public boolean isSealed() {
+        return sealed;
+    }
+
+    public void setSealed(boolean sealed) {
+        this.sealed = sealed;
+    }
+
 }
\ No newline at end of file

Modified: helma-ng/trunk/src/org/helma/tools/HelmaRunner.java
===================================================================
--- helma-ng/trunk/src/org/helma/tools/HelmaRunner.java	2009-04-03 07:46:07 UTC (rev 9567)
+++ helma-ng/trunk/src/org/helma/tools/HelmaRunner.java	2009-04-03 07:46:22 UTC (rev 9568)
@@ -62,7 +62,9 @@
         }
 
         FileRepository home = new FileRepository(System.getProperty("helma.home", "."));
-        HelmaConfiguration config = new HelmaConfiguration(home, null, scriptName);
+        String modulePath = System.getProperty("helma.modulepath");
+        HelmaConfiguration config = new HelmaConfiguration(home, modulePath, "modules");
+        config.addScriptRepository(scriptName);
         if (optlevel >= -1) {
             config.setOptLevel(optlevel);
         }

Modified: helma-ng/trunk/src/org/helma/web/HelmaServlet.java
===================================================================
--- helma-ng/trunk/src/org/helma/web/HelmaServlet.java	2009-04-03 07:46:07 UTC (rev 9567)
+++ helma-ng/trunk/src/org/helma/web/HelmaServlet.java	2009-04-03 07:46:22 UTC (rev 9568)
@@ -94,13 +94,12 @@
                 }
                 String helmaHome = config.getInitParameter("helmaHome");
                 String modulePath = config.getInitParameter("modulePath");
-                String scriptName = config.getInitParameter("scriptName");
                 Repository home = new FileRepository(helmaHome);
                 if (!home.exists()) {
                     home = new WebappRepository(config.getServletContext(), helmaHome);
                 }
                 HelmaConfiguration conf =
-                        new HelmaConfiguration(home, modulePath, scriptName);
+                        new HelmaConfiguration(home, modulePath, "modules");
                 conf.setHostClasses(classes);
                 engine = new RhinoEngine(conf);
             } catch (ClassNotFoundException x) {
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.