sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r612408 - in /incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript: RhinoJavaScriptEngineFactory.java helper/SlingContextFactory.java
Date Wed, 16 Jan 2008 10:20:31 GMT
Author: fmeschbe
Date: Wed Jan 16 02:20:30 2008
New Revision: 612408

URL: http://svn.apache.org/viewvc?rev=612408&view=rev
Log:
SLING-165 Create a ContextFactory to enable the dynamic scope feature.

Added:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
Modified:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java?rev=612408&r1=612407&r2=612408&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
Wed Jan 16 02:20:30 2008
@@ -21,9 +21,12 @@
 import javax.script.ScriptEngine;
 
 import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+import org.apache.sling.scripting.javascript.helper.SlingContextFactory;
 import org.apache.sling.scripting.javascript.wrapper.ScriptableNode;
+import org.apache.sling.scripting.javascript.wrapper.ScriptablePrintWriter;
 import org.apache.sling.scripting.javascript.wrapper.ScriptableResource;
 import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.ScriptableObject;
 
@@ -41,6 +44,10 @@
     private Scriptable rootScope;
 
     public RhinoJavaScriptEngineFactory() {
+        
+        // initialize the Rhino Context Factory
+        SlingContextFactory.setup();
+        
         Context cx = Context.enter();
         setEngineName(getEngineName() + " (" + cx.getImplementationVersion()
             + ")");
@@ -74,6 +81,7 @@
             try {
                 ScriptableObject.defineClass(rootScope, ScriptableResource.class);
                 ScriptableObject.defineClass(rootScope, ScriptableNode.class);
+                ScriptableObject.defineClass(rootScope, ScriptablePrintWriter.class);
             } catch (Throwable t) {
                 // TODO: log
             }

Added: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java?rev=612408&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
(added)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
Wed Jan 16 02:20:30 2008
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.javascript.helper;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+
+/**
+ * The <code>SlingContextFactory</code> extends the standard Rhino
+ * ContextFactory to provide customized settings, such as having the dynamic
+ * scope feature enabled by default. Other functionality, which may be added
+ * would be something like a configurable maximum script runtime value.
+ */
+public class SlingContextFactory extends ContextFactory {
+
+    // conditionally setup the global ContextFactory to be ours. If
+    // a global context factory has already been set, we have lost
+    // and cannot set this one.
+    public static void setup() {
+        if (!hasExplicitGlobal()) {
+            initGlobal(new SlingContextFactory());
+        }
+    }
+    
+    // private as instances of this class are only used by setup()
+    private SlingContextFactory() {}
+    
+    @Override
+    protected boolean hasFeature(Context cx, int featureIndex) {
+        if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE) {
+            return true;
+        }
+
+        return super.hasFeature(cx, featureIndex);
+    }
+
+}



Mime
View raw message