1 19 20 package org.netbeans.modules.ant.debugger; 21 22 import java.io.File ; 23 import java.lang.StringBuffer ; 24 import java.lang.ref.Reference ; 25 import java.lang.ref.WeakReference ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 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 52 53 57 public class DebuggerAntLogger extends AntLogger { 58 59 60 static DebuggerAntLogger getDefault () { 61 Iterator 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 (); 71 } 72 73 79 public void buildInitializationFailed (AntEvent event) { 80 } 91 92 97 public void buildStarted (AntEvent event) { 98 } 99 100 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 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 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 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 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 175 public void messageLogged (AntEvent event) { 176 } 177 178 183 public boolean interestedInSession (AntSession session) { 184 return true; 185 } 186 187 196 public boolean interestedInAllScripts (AntSession session) { 197 return true; 198 } 199 200 213 public boolean interestedInScript (File script, AntSession session) { 214 return true; 215 } 216 217 229 public String [] interestedInTargets (AntSession session) { 230 return ALL_TARGETS; 231 } 232 233 245 public String [] interestedInTasks (AntSession session) { 246 return ALL_TASKS; 247 } 248 249 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 268 private Map runningDebuggers = new HashMap (); 269 270 private Map runningDebuggers2 = new HashMap (); 271 private Set filesToDebug = new HashSet (); 272 273 private Map fileExecutors = new HashMap (); 274 275 void debugFile (File f) { 276 filesToDebug.add (f); 277 } 278 279 void fileExecutor(File f, ExecutorTask execTask) { 280 fileExecutors.put(f, new WeakReference (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 execRef = (Reference ) fileExecutors.remove(s.getOriginatingScript()); 298 ExecutorTask execTask = null; 299 if (execRef != null) { 300 execTask = (ExecutorTask) execRef.get(); 301 } 302 303 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 (); 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 [] { 329 new SessionProvider () { 330 public String getSessionName () { 331 return antEvent.getSession ().getDisplayName (); 332 } 333 334 public String getLocationName () { 335 return "localhost"; 336 } 337 338 public String getTypeID () { 339 return "AntSession"; 340 } 341 342 public Object [] getServices () { 343 return new Object [] {}; 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 |