1 19 20 package org.apache.tools.ant.module.bridge.impl; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.LinkedHashSet ; 34 import java.util.LinkedList ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 import org.apache.tools.ant.BuildEvent; 41 import org.apache.tools.ant.BuildException; 42 import org.apache.tools.ant.BuildListener; 43 import org.apache.tools.ant.Location; 44 import org.apache.tools.ant.Project; 45 import org.apache.tools.ant.RuntimeConfigurable; 46 import org.apache.tools.ant.Target; 47 import org.apache.tools.ant.Task; 48 import org.apache.tools.ant.module.bridge.AntBridge; 49 import org.apache.tools.ant.module.run.Hyperlink; 50 import org.apache.tools.ant.module.run.LoggerTrampoline; 51 import org.apache.tools.ant.module.spi.AntEvent; 52 import org.apache.tools.ant.module.spi.AntLogger; 53 import org.apache.tools.ant.module.spi.AntSession; 54 import org.apache.tools.ant.module.spi.TaskStructure; 55 import org.netbeans.api.progress.ProgressHandle; 56 import org.openide.ErrorManager; 57 import org.openide.awt.StatusDisplayer; 58 import org.openide.util.Lookup; 59 import org.openide.util.NbBundle; 60 import org.openide.util.NbCollections; 61 import org.openide.util.RequestProcessor; 62 import org.openide.util.WeakSet; 63 import org.openide.windows.OutputListener; 64 import org.openide.windows.OutputWriter; 65 66 75 final class NbBuildLogger implements BuildListener, LoggerTrampoline.AntSessionImpl { 76 77 private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(NbBuildLogger.class.getName()); 78 79 private static final int EM_LEVEL = Boolean.getBoolean(NbBuildLogger.class.getName() + ".LOG_AT_WARNING") ? ErrorManager.WARNING : ErrorManager.INFORMATIONAL; 81 private static final boolean LOGGABLE = ERR.isLoggable(EM_LEVEL); 82 83 private final AntSession thisSession; 84 85 private final File origScript; 86 private String [] targets = null; 87 private final OutputWriter out; 88 private final OutputWriter err; 89 private final int verbosity; 90 private final String displayName; 91 private final Runnable interestingOutputCallback; 92 private final ProgressHandle handle; 93 private final RequestProcessor.Task sleepTask = RequestProcessor.getDefault().create(new Runnable () { 94 public void run() { 95 handle.suspend(""); 96 } 97 }); 98 private static final int SLEEP_DELAY = 5000; 99 100 private final Map <AntLogger,Object > customData = new HashMap <AntLogger,Object >(); 101 102 private List <AntLogger> interestedLoggers = null; 103 private Map <File ,Collection <AntLogger>> interestedLoggersByScript = new HashMap <File ,Collection <AntLogger>>(); 104 private Map <String ,Collection <AntLogger>> interestedLoggersByTarget = new HashMap <String ,Collection <AntLogger>>(); 105 private Map <String ,Collection <AntLogger>> interestedLoggersByTask = new HashMap <String ,Collection <AntLogger>>(); 106 private Map <Integer ,Collection <AntLogger>> interestedLoggersByLevel = new HashMap <Integer ,Collection <AntLogger>>(); 107 108 private final Set <Project> projectsWithProperties = Collections.synchronizedSet(new WeakSet<Project>()); 109 110 private final Set <Throwable > consumedExceptions = new WeakSet<Throwable >(); 111 112 113 private boolean stop = false; 114 115 private boolean running = true; 116 117 122 private final Map <String ,Map <String ,String >> knownImportedTargets = Collections.synchronizedMap(new HashMap <String ,Map <String ,String >>()); 123 127 private String currentlyParsedMainScript = null; 128 132 private String currentlyParsedImportedScript = null; 133 136 private Task lastTask = null; 137 138 public NbBuildLogger(File origScript, OutputWriter out, OutputWriter err, int verbosity, String displayName, Runnable interestingOutputCallback, ProgressHandle handle) { 139 thisSession = LoggerTrampoline.ANT_SESSION_CREATOR.makeAntSession(this); 140 this.origScript = origScript; 141 this.out = out; 142 this.err = err; 143 this.verbosity = verbosity; 144 this.displayName = displayName; 145 this.interestingOutputCallback = interestingOutputCallback; 146 this.handle = handle; 147 if (LOGGABLE) { 148 ERR.log(EM_LEVEL, "---- Initializing build of " + origScript + " \"" + displayName + "\" at verbosity " + verbosity + " ----"); 149 } 150 } 151 152 153 public void stop() { 154 stop = true; 155 } 156 157 158 private void checkForStop() { 159 if (stop) { 160 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(NbBuildLogger.class, "MSG_stopped", displayName)); 161 throw new ThreadDeath (); 162 } 163 if (running) { 164 handle.switchToIndeterminate(); 165 sleepTask.schedule(SLEEP_DELAY); 166 } 167 } 168 169 174 public void shutdown() { 175 running = false; 176 out.close(); 177 err.close(); 178 handle.finish(); 179 sleepTask.cancel(); 180 } 181 182 private void verifyRunning() { 183 if (!running) { 184 throw new IllegalStateException ("AntSession/AntEvent/TaskStructure method called after completion of Ant process"); } 186 } 187 188 194 private synchronized void initInterestedLoggers() { 195 if (interestedLoggers == null) { 196 interestedLoggers = new ArrayList <AntLogger>(); 197 for (AntLogger l : Lookup.getDefault().lookupAll(AntLogger.class)) { 198 if (l.interestedInSession(thisSession)) { 199 interestedLoggers.add(l); 200 } 201 } 202 if (LOGGABLE) { 203 ERR.log(EM_LEVEL, "getInterestedLoggers: loggers=" + interestedLoggers); 204 } 205 } 206 } 207 208 @SuppressWarnings ("unchecked") private final Collection <AntLogger>[] interestedLoggersByVariousCriteria = new Collection [4]; 210 private static final Comparator <Collection <AntLogger>> INTERESTED_LOGGERS_SORTER = new Comparator <Collection <AntLogger>>() { 211 public int compare(Collection <AntLogger> c1, Collection <AntLogger> c2) { 212 int x = c1.size() - c2.size(); if (x != 0) { 214 return x; 215 } else { 216 return System.identityHashCode(c1) - System.identityHashCode(c2); 217 } 218 } 219 }; 220 223 private synchronized Collection <AntLogger> getInterestedLoggersByEvent(AntEvent e) { 224 initInterestedLoggers(); 225 interestedLoggersByVariousCriteria[0] = getInterestedLoggersByScript(e.getScriptLocation()); 227 interestedLoggersByVariousCriteria[1] = getInterestedLoggersByTarget(e.getTargetName()); 228 interestedLoggersByVariousCriteria[2] = getInterestedLoggersByTask(e.getTaskName()); 229 interestedLoggersByVariousCriteria[3] = getInterestedLoggersByLevel(e.getLogLevel()); 230 Arrays.sort(interestedLoggersByVariousCriteria, INTERESTED_LOGGERS_SORTER); 231 if (LOGGABLE) { 232 ERR.log(EM_LEVEL, "getInterestedLoggersByVariousCriteria: event=" + e + " loggers=" + Arrays.asList(interestedLoggersByVariousCriteria)); 233 } 234 List <AntLogger> loggers = new LinkedList <AntLogger>(interestedLoggersByVariousCriteria[0]); 237 for (int i = 1; i < 4; i++) { 238 loggers.retainAll(interestedLoggersByVariousCriteria[i]); 239 } 240 if (LOGGABLE) { 241 ERR.log(EM_LEVEL, "getInterestedLoggersByEvent: event=" + e + " loggers=" + loggers); 242 } 243 return loggers; 244 } 245 246 private synchronized Collection <AntLogger> getInterestedLoggersByScript(File script) { 247 Collection <AntLogger> c = interestedLoggersByScript.get(script); 248 if (c == null) { 249 c = new LinkedHashSet <AntLogger>(interestedLoggers.size()); 250 interestedLoggersByScript.put(script, c); 251 for (AntLogger l : interestedLoggers) { 252 if (l.interestedInAllScripts(thisSession) || (script != null && l.interestedInScript(script, thisSession))) { 253 c.add(l); 254 } 255 } 256 if (LOGGABLE) { 257 ERR.log(EM_LEVEL, "getInterestedLoggersByScript: script=" + script + " loggers=" + c); 258 } 259 } 260 return c; 261 } 262 263 private synchronized Collection <AntLogger> getInterestedLoggersByTarget(String target) { 264 Collection <AntLogger> c = interestedLoggersByTarget.get(target); 265 if (c == null) { 266 c = new LinkedHashSet <AntLogger>(interestedLoggers.size()); 267 interestedLoggersByTarget.put(target, c); 268 for (AntLogger l : interestedLoggers) { 269 String [] targets = l.interestedInTargets(thisSession); 270 if (targets == AntLogger.ALL_TARGETS || 271 (target != null && Arrays.asList(targets).contains(target)) || 272 (target == null && targets == AntLogger.NO_TARGETS)) { 273 c.add(l); 274 } 275 } 276 if (LOGGABLE) { 277 ERR.log(EM_LEVEL, "getInterestedLoggersByTarget: target=" + target + " loggers=" + c); 278 } 279 } 280 return c; 281 } 282 283 private synchronized Collection <AntLogger> getInterestedLoggersByTask(String task) { 284 Collection <AntLogger> c = interestedLoggersByTask.get(task); 285 if (c == null) { 286 c = new LinkedHashSet <AntLogger>(interestedLoggers.size()); 287 interestedLoggersByTask.put(task, c); 288 for (AntLogger l : interestedLoggers) { 289 String [] tasks = l.interestedInTasks(thisSession); 290 if (tasks == AntLogger.ALL_TASKS || 291 (task != null && Arrays.asList(tasks).contains(task)) || 292 (task == null && tasks == AntLogger.NO_TASKS)) { 293 c.add(l); 294 } 295 } 296 if (LOGGABLE) { 297 ERR.log(EM_LEVEL, "getInterestedLoggersByTask: task=" + task + " loggers=" + c); 298 } 299 } 300 return c; 301 } 302 303 private synchronized Collection <AntLogger> getInterestedLoggersByLevel(int level) { 304 Collection <AntLogger> c = interestedLoggersByLevel.get(level); 305 if (c == null) { 306 c = new LinkedHashSet <AntLogger>(interestedLoggers.size()); 307 interestedLoggersByLevel.put(level, c); 308 for (AntLogger l : interestedLoggers) { 309 if (level == -1) { 310 c.add(l); 311 } else { 312 int[] levels = l.interestedInLogLevels(thisSession); 313 for (int _level : levels) { 314 if (_level == level) { 315 c.add(l); 316 break; 317 } 318 } 319 } 320 } 321 if (LOGGABLE) { 322 ERR.log(EM_LEVEL, "getInterestedLoggersByLevel: level=" + level + " loggers=" + c); 323 } 324 } 325 return c; 326 } 327 328 synchronized void setActualTargets(String [] targets) { 329 this.targets = targets; 330 } 331 332 void buildInitializationFailed(BuildException be) { 333 initInterestedLoggers(); 334 AntEvent ev = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(be)); 335 if (LOGGABLE) { 336 ERR.log(EM_LEVEL, "buildInitializationFailed: " + ev); 337 } 338 for (AntLogger l : getInterestedLoggersByScript(null)) { 339 l.buildInitializationFailed(ev); 340 } 341 } 342 343 public void buildStarted(BuildEvent ev) { 344 AntBridge.suspendDelegation(); 345 try { 346 checkForStop(); 347 initInterestedLoggers(); 348 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 349 if (LOGGABLE) { 350 ERR.log(EM_LEVEL, "buildStarted: " + e); 351 } 352 for (AntLogger l : interestedLoggers) { 353 try { 354 l.buildStarted(e); 355 } catch (RuntimeException x) { 356 ERR.notify(EM_LEVEL, x); 357 } 358 } 359 } finally { 360 AntBridge.resumeDelegation(); 361 } 362 } 363 364 public void buildFinished(BuildEvent ev) { 365 AntBridge.suspendDelegation(); 366 try { 367 stop = false; lastTask = null; 370 initInterestedLoggers(); AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 372 if (LOGGABLE) { 373 ERR.log(EM_LEVEL, "buildFinished: " + e); 374 if (e.getException() != null) { 375 ERR.notify(EM_LEVEL, e.getException()); 376 } 377 } 378 for (AntLogger l : interestedLoggers) { 379 try { 380 l.buildFinished(e); 381 } catch (RuntimeException x) { 382 ERR.notify(EM_LEVEL, x); 383 } catch (Error x) { 384 ERR.notify(EM_LEVEL, x); 385 } 386 } 387 } finally { 388 AntBridge.resumeDelegation(); 389 } 390 } 391 392 public void targetStarted(BuildEvent ev) { 393 AntBridge.suspendDelegation(); 394 try { 395 checkForStop(); 396 lastTask = null; 397 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 398 if (LOGGABLE) { 399 ERR.log(EM_LEVEL, "targetStarted: " + e); 400 } 401 for (AntLogger l : getInterestedLoggersByEvent(e)) { 402 try { 403 l.targetStarted(e); 404 } catch (RuntimeException x) { 405 ERR.notify(EM_LEVEL, x); 406 } 407 } 408 Project p = ev.getProject(); 410 String projectName = null; 411 if (p != null) { 412 projectName = p.getName(); 413 } 414 String targetName = e.getTargetName(); 415 if (targetName != null) { 416 String message; 417 if (projectName != null) { 418 message = NbBundle.getMessage(NbBuildLogger.class, "MSG_progress_target", projectName, targetName); 419 } else { 420 message = targetName; 421 } 422 428 handle.progress(message); 429 } 430 } finally { 431 AntBridge.resumeDelegation(); 432 } 433 } 434 435 public void targetFinished(BuildEvent ev) { 436 AntBridge.suspendDelegation(); 437 try { 438 checkForStop(); 439 lastTask = null; 440 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 441 if (LOGGABLE) { 442 ERR.log(EM_LEVEL, "targetFinished: " + e); 443 } 444 for (AntLogger l : getInterestedLoggersByEvent(e)) { 445 try { 446 l.targetFinished(e); 447 } catch (RuntimeException x) { 448 ERR.notify(EM_LEVEL, x); 449 } 450 } 451 } finally { 452 AntBridge.resumeDelegation(); 453 } 454 } 455 456 public void taskStarted(BuildEvent ev) { 457 AntBridge.suspendDelegation(); 458 try { 459 checkForStop(); 460 lastTask = ev.getTask(); 461 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 462 if (LOGGABLE) { 463 ERR.log(EM_LEVEL, "taskStarted: " + e); 464 } 465 for (AntLogger l : getInterestedLoggersByEvent(e)) { 466 try { 467 l.taskStarted(e); 468 } catch (RuntimeException x) { 469 ERR.notify(EM_LEVEL, x); 470 } 471 } 472 if ("input".equals(e.getTaskName())) { TaskStructure s = e.getTaskStructure(); 474 if (s != null) { 475 String def = s.getAttribute("defaultvalue"); if (def != null) { 477 NbInputHandler.setDefaultValue(e.evaluate(def)); 478 } 479 } 480 } 481 } finally { 482 AntBridge.resumeDelegation(); 483 } 484 } 485 486 public void taskFinished(BuildEvent ev) { 487 AntBridge.suspendDelegation(); 488 try { 489 checkForStop(); 490 lastTask = null; 491 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, false)); 492 if (LOGGABLE) { 493 ERR.log(EM_LEVEL, "taskFinished: " + e); 494 } 495 for (AntLogger l : getInterestedLoggersByEvent(e)) { 496 try { 497 l.taskFinished(e); 498 } catch (RuntimeException x) { 499 ERR.notify(EM_LEVEL, x); 500 } 501 } 502 NbInputHandler.setDefaultValue(null); 503 } finally { 504 AntBridge.resumeDelegation(); 505 } 506 } 507 508 516 private static final Pattern PARSING_BUILDFILE_MESSAGE = 517 Pattern.compile("parsing buildfile (.+) with URI = (?:.+)"); 519 527 private static final Pattern IMPORTING_FILE_MESSAGE = 528 Pattern.compile("Importing file (?:.+) from (.+)"); 530 538 private static final Pattern PARSED_TARGET_MESSAGE = 539 Pattern.compile(" \\+Target: (.+)"); 541 public void messageLogged(BuildEvent ev) { 542 AntBridge.suspendDelegation(); 543 try { 544 checkForStop(); 545 AntEvent e = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new Event(ev, true)); 546 if (LOGGABLE) { 547 ERR.log(EM_LEVEL, "messageLogged: " + e); 548 } 549 for (AntLogger l : getInterestedLoggersByEvent(e)) { 550 try { 551 l.messageLogged(e); 552 } catch (RuntimeException x) { 553 ERR.notify(EM_LEVEL, x); 554 } 555 } 556 String msg = ev.getMessage(); 558 if (msg.contains("ant.PropertyHelper") || msg.contains("ant.projectHelper")) { projectsWithProperties.add(ev.getProject()); 563 } 564 if (targetGetLocation == null) { 565 Matcher matcher; 569 if ((matcher = PARSING_BUILDFILE_MESSAGE.matcher(msg)).matches()) { 570 if (currentlyParsedMainScript != null) { 571 currentlyParsedImportedScript = matcher.group(1); 572 } 573 if (LOGGABLE) { 574 ERR.log(EM_LEVEL, "Got PARSING_BUILDFILE_MESSAGE: " + currentlyParsedImportedScript); 575 } 576 lastTask = null; 577 } else if ((matcher = IMPORTING_FILE_MESSAGE.matcher(msg)).matches()) { 578 currentlyParsedMainScript = matcher.group(1); 579 currentlyParsedImportedScript = null; 580 if (LOGGABLE) { 581 ERR.log(EM_LEVEL, "Got IMPORTING_FILE_MESSAGE: " + currentlyParsedMainScript); 582 } 583 lastTask = null; 584 } else if ((matcher = PARSED_TARGET_MESSAGE.matcher(msg)).matches()) { 585 if (currentlyParsedMainScript != null && currentlyParsedImportedScript != null) { 586 Map <String ,String > targetLocations = knownImportedTargets.get(currentlyParsedMainScript); 587 if (targetLocations == null) { 588 targetLocations = new HashMap <String ,String >(); 589 knownImportedTargets.put(currentlyParsedMainScript, targetLocations); 590 } 591 targetLocations.put(matcher.group(1), currentlyParsedImportedScript); 592 } 593 if (LOGGABLE) { 594 ERR.log(EM_LEVEL, "Got PARSED_TARGET_MESSAGE: " + matcher.group(1)); 595 } 596 lastTask = null; 597 } 598 } 599 } finally { 600 AntBridge.resumeDelegation(); 601 } 602 } 603 604 public File getOriginatingScript() { 605 verifyRunning(); 606 return origScript; 607 } 608 609 public String [] getOriginatingTargets() { 610 verifyRunning(); 611 return targets != null ? targets : new String [0]; 612 } 613 614 public synchronized Object getCustomData(AntLogger logger) { 615 verifyRunning(); 616 return customData.get(logger); 617 } 618 619 public synchronized void putCustomData(AntLogger logger, Object data) { 620 verifyRunning(); 621 customData.put(logger, data); 622 } 623 624 public void println(String message, boolean error, OutputListener listener) { 625 verifyRunning(); 626 if (LOGGABLE) { 627 ERR.log(EM_LEVEL, "println: error=" + error + " listener=" + listener + " message=" + message); 628 } 629 OutputWriter ow = error ? err : out; 630 try { 631 if (listener != null) { 632 boolean important = (message.indexOf("[deprecation]") == -1 && 634 message.indexOf("warning") == -1 && 635 message.indexOf("stopped") == -1); 636 ow.println(message, listener, important); 637 interestingOutputCallback.run(); 638 } else { 639 ow.println(message); 640 } 641 } catch (IOException e) { 642 ERR.notify(e); 643 } 644 } 645 646 public void deliverMessageLogged(AntEvent originalEvent, String message, int level) { 647 verifyRunning(); 648 if (originalEvent == null) { 649 throw new IllegalArgumentException ("Must pass an original event to deliverMessageLogged"); } 651 if (message == null) { 652 throw new IllegalArgumentException ("Must pass a message to deliverMessageLogged"); } 654 if (level < AntEvent.LOG_ERR || level > AntEvent.LOG_DEBUG) { 655 throw new IllegalArgumentException ("Unknown log level for reposted log event: " + level); } 657 if (LOGGABLE) { 658 ERR.log(EM_LEVEL, "deliverMessageLogged: level=" + level + " message=" + message); 659 } 660 AntEvent newEvent = LoggerTrampoline.ANT_EVENT_CREATOR.makeAntEvent(new RepostedEvent(originalEvent, message, level)); 661 for (AntLogger l : getInterestedLoggersByEvent(newEvent)) { 662 try { 663 l.messageLogged(newEvent); 664 } catch (RuntimeException x) { 665 ERR.notify(EM_LEVEL, x); 666 } 667 } 668 } 669 670 public synchronized void consumeException(Throwable t) throws IllegalStateException { 671 verifyRunning(); 672 if (isExceptionConsumed(t)) { 673 throw new IllegalStateException ("Already consumed " + t); } 675 consumedExceptions.add(t); 676 } 677 678 public synchronized boolean isExceptionConsumed(Throwable t) { 679 verifyRunning(); 680 if (consumedExceptions.contains(t)) { 681 return true; 682 } 683 Throwable nested = t.getCause(); 685 if (nested != null && isExceptionConsumed(nested)) { 686 consumedExceptions.add(t); 688 return true; 689 } 690 return false; 691 } 692 693 public int getVerbosity() { 694 verifyRunning(); 695 return verbosity; 696 } 697 698 String getDisplayNameNoLock() { 699 return displayName; 700 } 701 702 public String getDisplayName() { 703 verifyRunning(); 704 return displayName; 705 } 706 707 public OutputListener createStandardHyperlink(URL file, String message, int line1, int column1, int line2, int column2) { 708 verifyRunning(); 709 return new Hyperlink(file, message, line1, column1, line2, column2); 710 } 711 712 private static final Method targetGetLocation; private static final Method locationGetFileName; private static final Method locationGetLineNumber; private static final Method runtimeConfigurableGetAttributeMap; private static final Method runtimeConfigurableGetChildren; private static final Method runtimeConfigurableGetText; static { 720 Method _targetGetLocation = null; 721 try { 722 _targetGetLocation = Target.class.getMethod("getLocation"); if (AntBridge.getInterface().getAntVersion().indexOf("1.6.2") != -1) { _targetGetLocation = null; 726 } 727 } catch (NoSuchMethodException e) { 728 } catch (Exception e) { 730 ERR.notify(EM_LEVEL, e); 731 } 732 targetGetLocation = _targetGetLocation; 733 Method _locationGetFileName = null; 734 try { 735 _locationGetFileName = Location.class.getMethod("getFileName"); } catch (NoSuchMethodException e) { 737 } catch (Exception e) { 739 ERR.notify(EM_LEVEL, e); 740 } 741 locationGetFileName = _locationGetFileName; 742 Method _locationGetLineNumber = null; 743 try { 744 _locationGetLineNumber = Location.class.getMethod("getLineNumber"); } catch (NoSuchMethodException e) { 746 } catch (Exception e) { 748 ERR.notify(EM_LEVEL, e); 749 } 750 locationGetLineNumber = _locationGetLineNumber; 751 Method _runtimeConfigurableGetAttributeMap = null; 752 try { 753 _runtimeConfigurableGetAttributeMap = RuntimeConfigurable.class.getMethod("getAttributeMap"); } catch (NoSuchMethodException e) { 755 } catch (Exception e) { 757 ERR.notify(EM_LEVEL, e); 758 } 759 runtimeConfigurableGetAttributeMap = _runtimeConfigurableGetAttributeMap; 760 Method _runtimeConfigurableGetChildren = null; 761 try { 762 _runtimeConfigurableGetChildren = RuntimeConfigurable.class.getMethod("getChildren"); } catch (NoSuchMethodException e) { 764 } catch (Exception e) { 766 ERR.notify(EM_LEVEL, e); 767 } 768 runtimeConfigurableGetChildren = _runtimeConfigurableGetChildren; 769 Method _runtimeConfigurableGetText = null; 770 try { 771 _runtimeConfigurableGetText = RuntimeConfigurable.class.getMethod("getText"); } catch (NoSuchMethodException e) { 773 } catch (Exception e) { 775 ERR.notify(EM_LEVEL, e); 776 } 777 runtimeConfigurableGetText = _runtimeConfigurableGetText; 778 } 779 780 784 private Location getLocationOfTarget(Target target, Project project) { 785 if (targetGetLocation != null) { 786 try { 787 return (Location) targetGetLocation.invoke(target); 788 } catch (Exception e) { 789 ERR.notify(EM_LEVEL, e); 790 } 791 } 792 if (LOGGABLE) { 794 ERR.log(EM_LEVEL, "knownImportedTargets: " + knownImportedTargets); 795 } 796 if (project != null) { 797 String file = project.getProperty("ant.file"); if (file != null) { 799 Map <String ,String > targetLocations = knownImportedTargets.get(file); 800 if (targetLocations != null) { 801 String importedFile = targetLocations.get(target.getName()); 802 if (importedFile != null) { 803 return new Location(importedFile); 805 } 806 } 807 } 808 } 809 return null; 811 } 812 813 private static String getFileNameOfLocation(Location loc) { 814 if (locationGetFileName != null) { 815 try { 816 return (String ) locationGetFileName.invoke(loc); 817 } catch (Exception e) { 818 ERR.notify(EM_LEVEL, e); 819 } 820 } 821 String locs = loc.toString(); 823 int x = locs.indexOf(':'); 825 if (x != -1) { 826 return locs.substring(0, x); 827 } else { 828 return null; 829 } 830 } 831 832 private static int getLineNumberOfLocation(Location loc) { 833 if (locationGetLineNumber != null) { 834 try { 835 return (Integer ) locationGetLineNumber.invoke(loc); 836 } catch (Exception e) { 837 ERR.notify(EM_LEVEL, e); 838 } 839 } 840 String locs = loc.toString(); 842 int x = locs.indexOf(':'); 844 if (x != -1) { 845 int x2 = locs.indexOf(':', x + 1); 846 if (x2 != -1) { 847 String line = locs.substring(x + 1, x2); 848 try { 849 return Integer.parseInt(line); 850 } catch (NumberFormatException e) { 851 } 853 } 854 } 855 return 0; 856 } 857 858 @SuppressWarnings ("unchecked") 859 private static Map <String ,String > getAttributeMapOfRuntimeConfigurable(RuntimeConfigurable rc) { 860 if (runtimeConfigurableGetAttributeMap != null) { 861 try { 862 return (Map <String ,String >) runtimeConfigurableGetAttributeMap.invoke(rc); 863 } catch (Exception e) { 864 ERR.notify(EM_LEVEL, e); 865 } 866 } 867 return Collections.emptyMap(); 868 } 869 870 @SuppressWarnings ("unchecked") 871 private static Enumeration <RuntimeConfigurable> getChildrenOfRuntimeConfigurable(RuntimeConfigurable rc) { 872 if (runtimeConfigurableGetChildren != null) { 873 try { 874 return (Enumeration <RuntimeConfigurable>) runtimeConfigurableGetChildren.invoke(rc); 875 } catch (Exception e) { 876 ERR.notify(EM_LEVEL, e); 877 } 878 } 879 return Collections.enumeration(Collections.<RuntimeConfigurable>emptySet()); 880 } 881 882 private static String getTextOfRuntimeConfigurable(RuntimeConfigurable rc) { 883 if (runtimeConfigurableGetText != null) { 884 try { 885 return ((StringBuffer ) runtimeConfigurableGetText.invoke(rc)).toString(); 886 } catch (Exception e) { 887 ERR.notify(EM_LEVEL, e); 888 } 889 } 890 return ""; 891 } 892 893 896 private final class Event implements LoggerTrampoline.AntEventImpl { 897 898 private boolean consumed = false; 899 private final BuildEvent e; 900 private final Throwable exception; 901 private final int level; 902 private File scriptLocation; 903 904 909 public Event(BuildEvent e, boolean msgLogged) { 910 this.e = e; 911 exception = e.getException(); 912 if (msgLogged) { 913 level = e.getPriority(); 914 } else { 915 level = -1; 916 } 917 } 918 919 923 public Event(Throwable exception) { 924 e = null; 925 this.exception = exception; 926 level = -1; 927 } 928 929 public AntSession getSession() { 930 verifyRunning(); 931 return thisSession; 932 } 933 934 public void consume() throws IllegalStateException { 935 verifyRunning(); 936 if (consumed) { 937 throw new IllegalStateException ("Event already consumed"); } 939 consumed = true; 940 } 941 942 public boolean isConsumed() { 943 verifyRunning(); 944 return consumed; 945 } 946 947 public File getScriptLocation() { 948 verifyRunning(); 949 if (scriptLocation != null) { 950 return scriptLocation; 951 } 952 if (e == null) { 953 return null; 954 } 955 Task task = e.getTask(); 956 if (task != null) { 957 Location l = task.getLocation(); 958 if (l != null) { 959 String file = getFileNameOfLocation(l); 960 if (file != null) { 961 return scriptLocation = new File (file); 962 } 963 } 964 } 965 Target target = e.getTarget(); 966 Project project = getProjectIfPropertiesDefined(); 967 if (target != null) { 968 Location l = getLocationOfTarget(target, project); 969 if (l != null) { 970 String file = getFileNameOfLocation(l); 971 if (file != null) { 972 return scriptLocation = new File (file); 973 } 974 } 975 } 976 if (project != null) { 977 String file = project.getProperty("ant.file"); if (file != null) { 979 return scriptLocation = new File (file); 980 } 981 } 982 synchronized (NbBuildLogger.this) { 984 if (lastTask != null) { 985 Location l = lastTask.getLocation(); 986 if (l != null) { 987 String file = getFileNameOfLocation(l); 988 if (file != null) { 989 return scriptLocation = new File (file); 990 } 991 } 992 } 993 } 994 return null; 995 } 996 997 private Project getProjectIfPropertiesDefined() { 998 Project project = e.getProject(); 999 if (project != null && projectsWithProperties.contains(project)) { 1000 return project; 1001 } else { 1002 return null; 1003 } 1004 } 1005 1006 public int getLine() { 1007 verifyRunning(); 1008 if (e == null) { 1009 return -1; 1010 } 1011 Task task = e.getTask(); 1012 if (task != null) { 1013 Location l = task.getLocation(); 1014 if (l != null) { 1015 int line = getLineNumberOfLocation(l); 1016 if (line > 0) { 1017 return line; 1018 } 1019 } 1020 } 1021 Target target = e.getTarget(); 1022 if (target != null) { 1023 Location l = getLocationOfTarget(target, getProjectIfPropertiesDefined()); 1024 if (l != null) { 1025 int line = getLineNumberOfLocation(l); 1026 if (line > 0) { 1027 return line; 1028 } 1029 } 1030 } 1031 synchronized (NbBuildLogger.this) { 1033 if (lastTask != null) { 1034 Location l = lastTask.getLocation(); 1035 if (l != null) { 1036 int line = getLineNumberOfLocation(l); 1037 if (line > 0) { 1038 return line; 1039 } 1040 } 1041 } 1042 } 1043 return -1; 1044 } 1045 1046 public String getTargetName() { 1047 verifyRunning(); 1048 if (e == null) { 1049 return null; 1050 } 1051 Target target = e.getTarget(); 1052 if (target != null) { 1053 String name = target.getName(); 1054 if (name != null && name.length() > 0) { 1055 return name; 1056 } 1057 } 1058 synchronized (NbBuildLogger.this) { 1060 if (lastTask != null) { 1061 target = lastTask.getOwningTarget(); 1062 if (target != null) { 1063 String name = target.getName(); 1064 if (name != null && name.length() > 0) { 1065 return name; 1066 } 1067 } 1068 } 1069 } 1070 return null; 1071 } 1072 1073 public String getTaskName() { 1074 verifyRunning(); 1075 if (e == null) { 1076 return null; 1077 } 1078 Task task = e.getTask(); 1079 if (task != null) { 1080 return task.getRuntimeConfigurableWrapper().getElementTag(); 1081 } 1082 synchronized (NbBuildLogger.this) { 1084 if (lastTask != null) { 1085 return lastTask.getRuntimeConfigurableWrapper().getElementTag(); 1086 } 1087 } 1088 return null; 1089 } 1090 1091 public TaskStructure getTaskStructure() { 1092 verifyRunning(); 1093 Task task = e.getTask(); 1094 if (task != null) { 1095 return LoggerTrampoline.TASK_STRUCTURE_CREATOR.makeTaskStructure(new TaskStructureImpl(task.getRuntimeConfigurableWrapper())); 1096 } 1097 synchronized (NbBuildLogger.this) { 1099 if (lastTask != null) { 1100 return LoggerTrampoline.TASK_STRUCTURE_CREATOR.makeTaskStructure(new TaskStructureImpl(lastTask.getRuntimeConfigurableWrapper())); 1101 } 1102 } 1103 return null; 1104 } 1105 1106 public String getMessage() { 1107 verifyRunning(); 1108 if (e == null) { 1109 return null; 1110 } 1111 return e.getMessage(); 1112 } 1113 1114 public int getLogLevel() { 1115 verifyRunning(); 1116 return level; 1117 } 1118 1119 public Throwable getException() { 1120 verifyRunning(); 1121 return exception; 1122 } 1123 1124 public String getProperty(String name) { 1125 verifyRunning(); 1126 Project project = getProjectIfPropertiesDefined(); 1127 if (project != null) { 1128 Object o = project.getProperty(name); 1129 if (o instanceof String ) { 1130 return (String ) o; 1131 } else { 1132 return null; 1133 } 1134 } else { 1135 return null; 1136 } 1137 } 1138 1139 public Set <String > getPropertyNames() { 1140 verifyRunning(); 1141 Project project = getProjectIfPropertiesDefined(); 1142 if (project != null) { 1143 return NbCollections.checkedSetByFilter(project.getProperties().keySet(), String .class, true); 1144 } else { 1145 return Collections.emptySet(); 1146 } 1147 } 1148 1149 public String evaluate(String text) { 1150 verifyRunning(); 1151 Project project = getProjectIfPropertiesDefined(); 1152 if (project != null) { 1153 return project.replaceProperties(text); 1154 } else { 1155 return text; 1156 } 1157 } 1158 1159 @Override 1160 public String toString() { 1161 return "Event[target=" + getTargetName() + ",task=" + getTaskName() + ",message=" + getMessage() + ",scriptLocation=" + getScriptLocation() + ",exception=" + exception + ",level=" + level + ",consumed=" + consumed + "]"; } 1163 1164 } 1165 1166 1170 private final class RepostedEvent implements LoggerTrampoline.AntEventImpl { 1171 1172 private final AntEvent originalEvent; 1173 private final String message; 1174 private final int level; 1175 private boolean consumed = false; 1176 1177 public RepostedEvent(AntEvent originalEvent, String message, int level) { 1178 this.originalEvent = originalEvent; 1179 this.message = message; 1180 this.level = level; 1181 } 1182 1183 public void consume() throws IllegalStateException { 1184 verifyRunning(); 1185 if (consumed) { 1186 throw new IllegalStateException ("Event already consumed"); } 1188 consumed = true; 1189 } 1190 1191 public boolean isConsumed() { 1192 verifyRunning(); 1193 return consumed; 1194 } 1195 1196 public AntSession getSession() { 1197 return originalEvent.getSession(); 1198 } 1199 1200 public File getScriptLocation() { 1201 return originalEvent.getScriptLocation(); 1202 } 1203 1204 public int getLine() { 1205 return originalEvent.getLine(); 1206 } 1207 1208 public String getTargetName() { 1209 return originalEvent.getTargetName(); 1210 } 1211 1212 public String getTaskName() { 1213 return originalEvent.getTaskName(); 1214 } 1215 1216 public TaskStructure getTaskStructure() { 1217 return originalEvent.getTaskStructure(); 1218 } 1219 1220 public String getMessage() { 1221 verifyRunning(); 1222 return message; 1223 } 1224 1225 public int getLogLevel() { 1226 verifyRunning(); 1227 return level; 1228 } 1229 1230 public Throwable getException() { 1231 verifyRunning(); 1232 return null; 1233 } 1234 1235 public String getProperty(String name) { 1236 return originalEvent.getProperty(name); 1237 } 1238 1239 public Set <String > getPropertyNames() { 1240 return originalEvent.getPropertyNames(); 1241 } 1242 1243 public String evaluate(String text) { 1244 return originalEvent.evaluate(text); 1245 } 1246 1247 @Override 1248 public String toString() { 1249 return "RepostedEvent[consumed=" + consumed + ",level=" + level + ",message=" + message + "]"; } 1251 1252 } 1253 1254 1258 private final class TaskStructureImpl implements LoggerTrampoline.TaskStructureImpl { 1259 1260 private final RuntimeConfigurable rc; 1261 1262 public TaskStructureImpl(RuntimeConfigurable rc) { 1263 this.rc = rc; 1264 } 1265 1266 public String getName() { 1267 verifyRunning(); 1268 String name = rc.getElementTag(); 1269 if (name != null) { 1270 return name; 1271 } else { 1272 return ""; 1274 } 1275 } 1276 1277 public String getAttribute(String name) { 1278 verifyRunning(); 1279 return getAttributeMapOfRuntimeConfigurable(rc).get(name); 1280 } 1281 1282 public Set <String > getAttributeNames() { 1283 verifyRunning(); 1284 return getAttributeMapOfRuntimeConfigurable(rc).keySet(); 1285 } 1286 1287 public String getText() { 1288 verifyRunning(); 1289 String s = getTextOfRuntimeConfigurable(rc); 1290 if (s.length() > 0) { 1291 return s; 1293 } else { 1294 return null; 1295 } 1296 } 1297 1298 public TaskStructure[] getChildren() { 1299 verifyRunning(); 1300 List <TaskStructure> structures = new ArrayList <TaskStructure>(); 1301 for (RuntimeConfigurable subrc : NbCollections.iterable(getChildrenOfRuntimeConfigurable(rc))) { 1302 structures.add(LoggerTrampoline.TASK_STRUCTURE_CREATOR.makeTaskStructure(new TaskStructureImpl(subrc))); 1303 } 1304 return structures.toArray(new TaskStructure[structures.size()]); 1305 } 1306 1307 } 1308 1309} 1310 | Popular Tags |