View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.awt.event.ActionEvent;
22  
23  import javax.swing.WindowConstants;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.mozilla.javascript.ContextFactory;
28  import org.mozilla.javascript.Scriptable;
29  import org.mozilla.javascript.tools.debugger.Dim;
30  import org.mozilla.javascript.tools.debugger.ScopeProvider;
31  import org.mozilla.javascript.tools.debugger.SwingGui;
32  import org.mozilla.javascript.tools.shell.Global;
33  
34  
35  /**
36   * Alfresco implementation of Rhino JavaScript debugger
37   * 
38   * @author davidc
39   */
40  public class ScriptDebugger implements Runnable
41  {
42      private static final Log logger = LogFactory.getLog(ScriptDebugger.class);
43      
44      private ContextFactory factory = null;
45      private SwingGui gui = null;
46      protected Dim dim = null;
47      
48      
49      protected void initDebugger()
50      {
51          dim = new Dim();
52      }
53      
54      /**
55       * Start the Debugger
56       */
57      public void start()
58      {
59          if (logger.isDebugEnabled())
60          {
61              activate();
62              show();
63          }
64      }
65  
66      /**
67       * Activate the Debugger
68       */
69      public synchronized void activate()
70      {
71          factory = ContextFactory.getGlobal();
72          Global global = new Global();
73          global.init(factory);
74          global.setIn(System.in);
75          global.setOut(System.out);
76          global.setErr(System.err);        
77          initDebugger();
78          ScopeProvider sp = new AlfrescoScopeProvider((Scriptable)global);
79          dim.setScopeProvider(sp);
80          gui = new AlfrescoGui(dim, getTitle(), this);
81          gui.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
82          gui.setExitAction(this);
83      }
84      
85      protected String getTitle()
86      {
87          return "Alfresco web-tier JavaScript Debugger";
88      }
89  
90      
91      /**
92       * Show the debugger
93       */
94      public synchronized void show()
95      {
96          if (!isActive())
97          {
98              activate();
99          }
100         
101         dim.setBreakOnExceptions(true);
102         dim.setBreak();
103         dim.attachTo(factory);
104         gui.pack();
105         gui.setSize(600, 460);
106         gui.setVisible(true);
107     }
108     
109 
110     /**
111      * Hide the Debugger
112      */
113     public synchronized void hide()
114     {
115         if (isVisible())
116         {
117             dim.detach();
118             gui.dispose();
119         }
120     }
121     
122     /**
123      * Is Debugger visible?
124      * 
125      * @return
126      */
127     public boolean isVisible()
128     {
129         return isActive() && gui.isVisible();
130     }
131     
132     /**
133      * Is Debugger active?
134      * 
135      * @return
136      */
137     public boolean isActive()
138     {
139         return gui != null;
140     }
141     
142     /**
143      * Exit action.
144      */
145     public void run()
146     {
147         dim.detach();
148         gui.dispose();
149     }
150     
151     
152     private static class AlfrescoGui extends SwingGui
153     {
154         private static final long serialVersionUID = 5053205080777378416L;
155         private ScriptDebugger debugger;
156         
157         public AlfrescoGui(Dim dim, String title, ScriptDebugger debugger)
158         {
159             super(dim, title);
160             this.debugger = debugger;
161         }
162 
163         public void actionPerformed(ActionEvent e)
164         {
165             String cmd = e.getActionCommand();
166             if (cmd.equals("Exit"))
167             {
168                 debugger.hide();
169             }
170             else
171             {
172                 super.actionPerformed(e);
173             }
174         }
175     }
176     
177     
178     public static class AlfrescoScopeProvider implements ScopeProvider
179     {
180         AlfrescoScopeProvider(Scriptable scope)
181         {
182             this.scope = scope;
183         }
184         
185         /**
186          * The scope object to expose
187          */
188         private Scriptable scope;
189         
190         /**
191          * Returns the scope for script evaluations.
192          */
193         public Scriptable getScope()
194         {
195             return scope;
196         }
197     }
198 }