incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r699373 - in /incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper: SlingContextFactory.java SlingRhinoDebugger.java
Date Fri, 26 Sep 2008 15:44:22 GMT
Author: fmeschbe
Date: Fri Sep 26 08:44:22 2008
New Revision: 699373

URL: http://svn.apache.org/viewvc?rev=699373&view=rev
Log:
SLING-678 support enabling the debugger with a framework property
and have better control over the debugger window (used for SLING-676)

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

Modified: 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=699373&r1=699372&r2=699373&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java
Fri Sep 26 08:44:22 2008
@@ -18,11 +18,13 @@
  */
 package org.apache.sling.scripting.javascript.helper;
 
-import java.io.File;
+import java.lang.reflect.Field;
 
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.ContextFactory;
 import org.mozilla.javascript.tools.debugger.ScopeProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The <code>SlingContextFactory</code> extends the standard Rhino
@@ -32,10 +34,15 @@
  */
 public class SlingContextFactory extends ContextFactory {
 
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
     private SlingRhinoDebugger debugger;
+
     private ScopeProvider scopeProvider;
+
     private boolean debuggerActive;
-    
+
     // 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.
@@ -45,22 +52,38 @@
             initGlobal(new SlingContextFactory(sp));
         }
     }
-    
+
+    public static void teardown() {
+        ContextFactory factory = getGlobal();
+        if (factory instanceof SlingContextFactory) {
+            ((SlingContextFactory) factory).dispose();
+        }
+    }
+
     // private as instances of this class are only used by setup()
-    private SlingContextFactory(ScopeProvider sp) 
-    {
+    private SlingContextFactory(ScopeProvider sp) {
         scopeProvider = sp;
+    }
+
+    private void dispose() {
+        // ensure the debugger is closed
+        exitDebugger();
         
-        // TODO make this configurable via OSGi
-        File f = new File("/tmp/sling.debug");
-        debuggerActive = f.exists();
+        // reset the context factory class for future use
+        ContextFactory newGlobal = new ContextFactory();
+        setField(newGlobal, "hasCustomGlobal", Boolean.FALSE);
+        setField(newGlobal, "global", newGlobal);
+        setField(newGlobal, "sealed", Boolean.FALSE);
+        setField(newGlobal, "listeners", null);
+        setField(newGlobal, "disabledListening", Boolean.FALSE);
+        setField(newGlobal, "applicationClassLoader", null);
     }
-    
+
     @Override
     protected Context makeContext() {
         return new SlingContext();
     }
-    
+
     @Override
     protected boolean hasFeature(Context cx, int featureIndex) {
         if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE) {
@@ -69,7 +92,7 @@
 
         return super.hasFeature(cx, featureIndex);
     }
-    
+
     @Override
     protected void onContextCreated(Context cx) {
         super.onContextCreated(cx);
@@ -77,19 +100,55 @@
     }
 
     private void initDebugger(Context cx) {
-        if(!debuggerActive) {
-            return;
+        if (isDebugging()) {
+            try {
+                if (debugger == null) {
+                    debugger = new SlingRhinoDebugger(
+                        getClass().getSimpleName());
+                    debugger.setScopeProvider(scopeProvider);
+                    debugger.attachTo(this);
+                }
+            } catch (Exception e) {
+                log.warn("initDebugger: Failed setting up the Rhino debugger",
+                    e);
+            }
+        }
+    }
+
+    public void exitDebugger() {
+        if (debugger != null) {
+            debugger.setScopeProvider(null);
+            debugger.detach();
+            debugger.dispose();
+            debugger = null;
         }
+    }
+    
+    void debuggerStopped() {
+        debugger = null;
+    }
+
+    public void setDebugging(boolean enable) {
+        debuggerActive = enable;
+    }
+
+    public boolean isDebugging() {
+        return debuggerActive;
+    }
+
+    private void setField(Object instance, String fieldName, Object value) {
         try {
-            if (debugger == null) {
-                debugger = new SlingRhinoDebugger(getClass().getSimpleName());
-                debugger.setScopeProvider(scopeProvider);
-                debugger.attachTo(this);
+            Field field = instance.getClass().getDeclaredField(fieldName);
+            if (!field.isAccessible()) {
+                field.setAccessible(true);
             }
-        } catch (Exception e) {
-            // TODO log
-            System.err.println("SlingContextFactory.initDebugger(): " + e);
+            field.set(instance, value);
+        } catch (IllegalArgumentException iae) {
+            // don't care, but it is strange anyhow
+        } catch (IllegalAccessException iae) {
+            // don't care, but it is strange anyhow
+        } catch (NoSuchFieldException nsfe) {
+            // don't care, but it is strange anyhow
         }
     }
-    
 }

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingRhinoDebugger.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingRhinoDebugger.java?rev=699373&r1=699372&r2=699373&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingRhinoDebugger.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingRhinoDebugger.java
Fri Sep 26 08:44:22 2008
@@ -16,13 +16,51 @@
  */
 package org.apache.sling.scripting.javascript.helper;
 
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowStateListener;
+
+import org.mozilla.javascript.ContextFactory;
 import org.mozilla.javascript.tools.debugger.Dim;
 import org.mozilla.javascript.tools.debugger.SwingGui;
 
 class SlingRhinoDebugger extends Dim {
+    private SlingContextFactory slingContextFactory;
+
+    private final SwingGui gui;
+
     SlingRhinoDebugger(String windowTitle) {
-        final SwingGui gui = new SwingGui(this, windowTitle);
+        gui = new SwingGui(this, windowTitle);
         gui.pack();
         gui.setVisible(true);
+        gui.setExitAction(new Runnable() {
+            public void run() {
+                if (slingContextFactory != null) {
+                    slingContextFactory.debuggerStopped();
+                }
+            }
+        });
+    }
+
+    @Override
+    public void attachTo(ContextFactory factory) {
+        super.attachTo(factory);
+
+        if (factory instanceof SlingContextFactory) {
+            this.slingContextFactory = (SlingContextFactory) factory;
+        }
+    }
+
+    @Override
+    public void detach() {
+        this.slingContextFactory = null;
+        super.detach();
+    }
+
+    @Override
+    public void dispose() {
+        clearAllBreakpoints();
+        go();
+        gui.dispose();
+        super.dispose();
     }
 }



Mime
View raw message