KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > DebuggerAntLogger


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.debugger;
21
22 import java.io.File JavaDoc;
23 import java.lang.StringBuffer JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.apache.tools.ant.module.api.AntProjectCookie;
32 import org.apache.tools.ant.module.spi.AntLogger;
33 import org.apache.tools.ant.module.spi.AntSession;
34 import org.apache.tools.ant.module.spi.AntEvent;
35 import org.netbeans.api.debugger.DebuggerEngine;
36 import org.netbeans.api.debugger.DebuggerInfo;
37 import org.netbeans.api.debugger.DebuggerManager;
38 import org.netbeans.spi.debugger.SessionProvider;
39 import org.openide.execution.ExecutorTask;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.loaders.DataObject;
43 import org.openide.loaders.DataObjectNotFoundException;
44 import org.openide.util.Lookup;
45         
46         
47 /*
48  * AntTest.java
49  *
50  * Created on 19. leden 2004, 20:03
51  */

52
53 /**
54  *
55  * @author Honza
56  */

57 public class DebuggerAntLogger extends AntLogger {
58     
59     
60     static DebuggerAntLogger getDefault () {
61         Iterator JavaDoc it = Lookup.getDefault ().lookup (
62             new Lookup.Template (AntLogger.class)
63         ).allInstances ().iterator ();
64         while (it.hasNext ()) {
65             AntLogger al = (AntLogger) it.next ();
66             if (al instanceof DebuggerAntLogger) {
67                 return (DebuggerAntLogger) al;
68             }
69         }
70         throw new InternalError JavaDoc ();
71     }
72     
73     /**
74      * Fired only if the build could not even be started.
75      * {@link AntEvent#getException} will be non-null.
76      * The default implementation does nothing.
77      * @param event the associated event object
78      */

79     public void buildInitializationFailed (AntEvent event) {
80 // File script = event.getScriptLocation ();
81
// int lineNumber = event.getLine ();
82
//
83
// AntSession session = event.getSession ();
84
// String message = event.getMessage ();
85
// String target = event.getTargetName ();
86
// String task = event.getTaskName ();
87
// Set properties = event.getPropertyNames ();
88
// Utils.markCurrent (event);
89
// S ystem.out.println(event);
90
}
91     
92     /**
93      * Fired once when a build is started.
94      * The default implementation does nothing.
95      * @param event the associated event object
96      */

97     public void buildStarted (AntEvent event) {
98     }
99     
100     /**
101      * Fired once when a build is finished.
102      * The default implementation does nothing.
103      * @param event the associated event object
104      * @see AntEvent#getException
105      */

106     public void buildFinished (AntEvent event) {
107         AntDebugger d = getDebugger (event.getSession (), event);
108         if (d == null) return;
109         d.buildFinished (event);
110         finishDebugging (d);
111     }
112     
113     /**
114      * Fired when a target is started.
115      * It is <em>not</em> guaranteed that {@link AntEvent#getTargetName}
116      * will be non-null (as can happen in some circumstances with
117      * <code>&lt;import&gt;</code>, for example).
118      * The default implementation does nothing.
119      * @param event the associated event object
120      */

121     public void targetStarted (AntEvent event) {
122         AntDebugger d = getDebugger (event.getSession (), event);
123         if (d == null) return;
124         d.targetStarted (event);
125     }
126     
127     /**
128      * Fired when a target is finished.
129      * It is <em>not</em> guaranteed that {@link AntEvent#getTargetName}
130      * will be non-null.
131      * The default implementation does nothing.
132      * @param event the associated event object
133      */

134     public void targetFinished (AntEvent event) {
135         AntDebugger d = getDebugger (event.getSession (), event);
136         if (d == null) return;
137         d.targetFinished (event);
138     }
139     
140     /**
141      * Fired when a task is started.
142      * It is <em>not</em> guaranteed that {@link AntEvent#getTaskName} or
143      * {@link AntEvent#getTaskStructure} will be non-null, though they will
144      * usually be defined.
145      * {@link AntEvent#getTargetName} might also be null.
146      * The default implementation does nothing.
147      * @param event the associated event object
148      */

149     public void taskStarted (AntEvent event) {
150         AntDebugger d = getDebugger (event.getSession (), event);
151         if (d == null) return;
152         d.taskStarted (event);
153     }
154     
155     /**
156      * Fired when a task is finished.
157      * It is <em>not</em> guaranteed that {@link AntEvent#getTaskName} or
158      * {@link AntEvent#getTaskStructure} will be non-null.
159      * {@link AntEvent#getTargetName} might also be null.
160      * The default implementation does nothing.
161      * @param event the associated event object
162      */

163     public void taskFinished (AntEvent event) {
164         AntDebugger d = getDebugger (event.getSession (), event);
165         if (d == null) return;
166         d.taskFinished (event);
167     }
168
169     /**
170      * Fired when a message is logged.
171      * The task and target fields may or may not be defined.
172      * The default implementation does nothing.
173      * @param event the associated event object
174      */

175     public void messageLogged (AntEvent event) {
176     }
177
178     /**
179      * Mark whether this logger is interested in a given Ant session.
180      * @param session a session which is about to be start
181      * @return true to receive events about it; by default, false
182      */

183     public boolean interestedInSession (AntSession session) {
184         return true;
185     }
186     
187     /**
188      * Mark whether this logger is interested in any Ant script.
189      * If true, no events will be masked due to the script location.
190      * Note that a few events have no defined script and so will only
191      * be delivered to loggers interested in all scripts; typically this
192      * applies to debugging messages when a project is just being configured.
193      * @param session the relevant session
194      * @return true to receive events for all scripts; by default, false
195      */

196     public boolean interestedInAllScripts (AntSession session) {
197         return true;
198     }
199     
200     /**
201      * Mark whether this logger is interested in a given Ant script.
202      * Called only if {@link #interestedInAllScripts} is false.
203      * Only events with a defined script according to {@link AntEvent#getScriptLocation}
204      * which this logger is interested in will be delivered.
205      * Note that a few events have no defined script and so will only
206      * be delivered to loggers interested in all scripts; typically this
207      * applies to debugging messages when a project is just being configured.
208      * Note also that a single session can involve many different scripts.
209      * @param script a particular build script
210      * @param session the relevant session
211      * @return true to receive events sent from this script; by default, false
212      */

213     public boolean interestedInScript (File JavaDoc script, AntSession session) {
214         return true;
215     }
216
217     /**
218      * Mark which kinds of targets this logger is interested in.
219      * This applies to both target start and finish events, as well as any other
220      * events for which {@link AntEvent#getTargetName} is not null, such as task
221      * start and finish events, and message log events.
222      * If {@link #NO_TARGETS}, no events with specific targets will be sent to it.
223      * If a specific list, only events with defined target names included in the list
224      * will be sent to it.
225      * If {@link #ALL_TARGETS}, all events not otherwise excluded will be sent to it.
226      * @param session the relevant session
227      * @return a nonempty (and non-null) list of target names; by default, {@link #NO_TARGETS}
228      */

229     public String JavaDoc[] interestedInTargets (AntSession session) {
230         return ALL_TARGETS;
231     }
232     
233     /**
234      * Mark which kinds of tasks this logger is interested in.
235      * This applies to both task start and finish events, as well as any other
236      * events for which {@link AntEvent#getTaskName} is not null, such as
237      * message log events.
238      * If {@link #NO_TASKS}, no events with specific tasks will be sent to it.
239      * If a specific list, only events with defined task names included in the list
240      * will be sent to it.
241      * If {@link #ALL_TASKS}, all events not otherwise excluded will be sent to it.
242      * @param session the relevant session
243      * @return a nonempty (and non-null) list of task names; by default, {@link #NO_TASKS}
244      */

245     public String JavaDoc[] interestedInTasks (AntSession session) {
246         return ALL_TASKS;
247     }
248     
249     /**
250      * Mark which kinds of message log events this logger is interested in.
251      * This applies only to message log events and no others.
252      * Only events with log levels included in the returned list will be delivered.
253      * @param session the relevant session
254      * @return a list of levels such as {@link AntEvent#LOG_INFO}; by default, an empty list
255      * @see AntSession#getVerbosity
256      */

257     public int[] interestedInLogLevels (AntSession session) {
258         return new int[] {
259             AntEvent.LOG_INFO,
260             AntEvent.LOG_DEBUG,
261             AntEvent.LOG_ERR,
262             AntEvent.LOG_VERBOSE,
263             AntEvent.LOG_WARN
264         };
265     }
266     
267     /** AntSession => AntDebugger */
268     private Map JavaDoc runningDebuggers = new HashMap JavaDoc ();
269     /** AntDebugger => AntSession */
270     private Map JavaDoc runningDebuggers2 = new HashMap JavaDoc ();
271     private Set JavaDoc filesToDebug = new HashSet JavaDoc ();
272     /** File => WeakReference -> ExecutorTask */
273     private Map JavaDoc fileExecutors = new HashMap JavaDoc();
274     
275     void debugFile (File JavaDoc f) {
276         filesToDebug.add (f);
277     }
278     
279     void fileExecutor(File JavaDoc f, ExecutorTask execTask) {
280         fileExecutors.put(f, new WeakReference JavaDoc(execTask));
281     }
282     
283     private void finishDebugging (
284         AntDebugger debugger
285     ) {
286         AntSession session = (AntSession) runningDebuggers2.remove (debugger);
287         runningDebuggers.remove (session);
288     }
289     
290     private AntDebugger getDebugger (AntSession s, AntEvent antEvent) {
291         AntDebugger d = (AntDebugger) runningDebuggers.get (s);
292         if (d != null) return d;
293         
294         if (!filesToDebug.contains (s.getOriginatingScript ()))
295             return null;
296         filesToDebug.remove (s.getOriginatingScript ());
297         Reference JavaDoc execRef = (Reference JavaDoc) fileExecutors.remove(s.getOriginatingScript());
298         ExecutorTask execTask = null;
299         if (execRef != null) {
300             execTask = (ExecutorTask) execRef.get();
301         }
302         
303         // start debugging othervise
304
try {
305             FileObject fo = FileUtil.toFileObject (s.getOriginatingScript ());
306             DataObject dob = DataObject.find (fo);
307             AntProjectCookie antCookie = (AntProjectCookie) dob.getCookie
308                 (AntProjectCookie.class);
309             if (antCookie == null)
310                 throw new NullPointerException JavaDoc ();
311             d = startDebugging (antCookie, antEvent, execTask);
312             runningDebuggers.put (s, d);
313             runningDebuggers2.put (d, s);
314             return d;
315         } catch (DataObjectNotFoundException ex) {
316             ex.printStackTrace ();
317             return null;
318         }
319     }
320
321     private static AntDebugger startDebugging (
322         final AntProjectCookie antCookie,
323         final AntEvent antEvent,
324         final ExecutorTask execTask
325     ) {
326         DebuggerInfo di = DebuggerInfo.create (
327             "AntDebuggerInfo",
328             new Object JavaDoc[] {
329                 new SessionProvider () {
330                     public String JavaDoc getSessionName () {
331                         return antEvent.getSession ().getDisplayName ();
332                     }
333                     
334                     public String JavaDoc getLocationName () {
335                         return "localhost";
336                     }
337                     
338                     public String JavaDoc getTypeID () {
339                         return "AntSession";
340                     }
341
342                     public Object JavaDoc[] getServices () {
343                         return new Object JavaDoc[] {};
344                     }
345                 },
346                 antCookie
347             }
348         );
349         DebuggerEngine[] es = DebuggerManager.getDebuggerManager ().
350             startDebugging (di);
351         AntDebugger debugger = (AntDebugger) es [0].lookupFirst (null, AntDebugger.class);
352         debugger.setExecutor(execTask);
353         return debugger;
354     }
355 }
356
Popular Tags