1 18 package org.apache.tools.ant; 19 20 import org.apache.tools.ant.util.LoaderUtils; 21 import org.apache.tools.ant.util.FileUtils; 22 import org.apache.tools.ant.util.JAXPUtils; 23 import org.apache.tools.ant.util.ProxySetup; 24 import org.apache.tools.ant.util.JavaEnvUtils; 25 import org.apache.tools.ant.launch.Launcher; 26 import org.xml.sax.XMLReader ; 27 28 import javax.xml.parsers.SAXParserFactory ; 29 import javax.xml.parsers.SAXParser ; 30 import java.io.File ; 31 import java.io.FilenameFilter ; 32 import java.io.PrintStream ; 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 import java.io.FileOutputStream ; 36 import java.util.Enumeration ; 37 import java.util.Properties ; 38 import java.util.Calendar ; 39 import java.util.TimeZone ; 40 import java.lang.reflect.Method ; 41 import java.lang.reflect.InvocationTargetException ; 42 43 50 public final class Diagnostics { 51 52 57 private static final int BIG_DRIFT_LIMIT = 10000; 58 62 private static final int TEST_FILE_SIZE = 32; 63 private static final int KILOBYTE = 1024; 64 private static final int SECONDS_PER_MILLISECOND = 1000; 65 private static final int SECONDS_PER_MINUTE = 60; 66 private static final int MINUTES_PER_HOUR = 60; 67 private static final String TEST_CLASS 68 = "org.apache.tools.ant.taskdefs.optional.Test"; 69 70 74 protected static final String ERROR_PROPERTY_ACCESS_BLOCKED 75 = "Access to this property blocked by a security manager"; 76 77 78 private Diagnostics() { 79 } 81 82 87 public static boolean isOptionalAvailable() { 88 try { 89 Class.forName(TEST_CLASS); 90 } catch (ClassNotFoundException e) { 91 return false; 92 } 93 return true; 94 } 95 96 101 public static void validateVersion() throws BuildException { 102 try { 103 Class optional 104 = Class.forName(TEST_CLASS); 105 String coreVersion = getImplementationVersion(Main.class); 106 String optionalVersion = getImplementationVersion(optional); 107 108 if (coreVersion != null && !coreVersion.equals(optionalVersion)) { 109 throw new BuildException("Invalid implementation version " 110 + "between Ant core and Ant optional tasks.\n" 111 + " core : " + coreVersion + "\n" 112 + " optional: " + optionalVersion); 113 } 114 } catch (ClassNotFoundException e) { 115 ignoreThrowable(e); 117 } 118 } 119 120 126 public static File [] listLibraries() { 127 String home = System.getProperty(MagicNames.ANT_HOME); 128 if (home == null) { 129 return null; 130 } 131 File libDir = new File (home, "lib"); 132 return listJarFiles(libDir); 133 134 } 135 136 141 private static File [] listJarFiles(File libDir) { 142 FilenameFilter filter = new FilenameFilter () { 143 public boolean accept(File dir, String name) { 144 return name.endsWith(".jar"); 145 } 146 }; 147 148 File [] files = libDir.listFiles(filter); 149 return files; 150 } 151 152 156 public static void main(String [] args) { 157 doReport(System.out); 158 } 159 160 161 167 private static String getImplementationVersion(Class clazz) { 168 Package pkg = clazz.getPackage(); 169 return pkg.getImplementationVersion(); 170 } 171 172 176 private static String getXmlParserName() { 177 SAXParser saxParser = getSAXParser(); 178 if (saxParser == null) { 179 return "Could not create an XML Parser"; 180 } 181 182 String saxParserName = saxParser.getClass().getName(); 184 return saxParserName; 185 } 186 187 191 private static SAXParser getSAXParser() { 192 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 193 if (saxParserFactory == null) { 194 return null; 195 } 196 SAXParser saxParser = null; 197 try { 198 saxParser = saxParserFactory.newSAXParser(); 199 } catch (Exception e) { 200 ignoreThrowable(e); 202 } 203 return saxParser; 204 } 205 206 210 211 private static String getXMLParserLocation() { 212 SAXParser saxParser = getSAXParser(); 213 if (saxParser == null) { 214 return null; 215 } 216 String location = getClassLocation(saxParser.getClass()); 217 return location; 218 } 219 220 private static String getNamespaceParserName() { 221 try { 222 XMLReader reader = JAXPUtils.getNamespaceXMLReader(); 223 return reader.getClass().getName(); 224 } catch (BuildException e) { 225 ignoreThrowable(e); 227 return null; 228 } 229 } 230 231 private static String getNamespaceParserLocation() { 232 try { 233 XMLReader reader = JAXPUtils.getNamespaceXMLReader(); 234 return getClassLocation(reader.getClass()); 235 } catch (BuildException e) { 236 ignoreThrowable(e); 238 return null; 239 } 240 } 241 242 247 private static void ignoreThrowable(Throwable thrown) { 248 } 249 250 255 256 private static String getClassLocation(Class clazz) { 257 File f = LoaderUtils.getClassSource(clazz); 258 return f == null ? null : f.getAbsolutePath(); 259 } 260 261 262 266 public static void doReport(PrintStream out) { 267 out.println("------- Ant diagnostics report -------"); 268 out.println(Main.getAntVersion()); 269 header(out, "Implementation Version"); 270 271 out.println("core tasks : " + getImplementationVersion(Main.class)); 272 273 Class optional = null; 274 try { 275 optional = Class.forName(TEST_CLASS); 276 out.println("optional tasks : " 277 + getImplementationVersion(optional)); 278 } catch (ClassNotFoundException e) { 279 ignoreThrowable(e); 280 out.println("optional tasks : not available"); 281 } 282 283 header(out, "ANT PROPERTIES"); 284 doReportAntProperties(out); 285 286 header(out, "ANT_HOME/lib jar listing"); 287 doReportAntHomeLibraries(out); 288 289 header(out, "USER_HOME/.ant/lib jar listing"); 290 doReportUserHomeLibraries(out); 291 292 header(out, "Tasks availability"); 293 doReportTasksAvailability(out); 294 295 header(out, "org.apache.env.Which diagnostics"); 296 doReportWhich(out); 297 298 header(out, "XML Parser information"); 299 doReportParserInfo(out); 300 301 header(out, "System properties"); 302 doReportSystemProperties(out); 303 304 header(out, "Temp dir"); 305 doReportTempDir(out); 306 307 header(out, "Locale information"); 308 doReportLocale(out); 309 310 header(out, "Proxy information"); 311 doReportProxy(out); 312 313 out.println(); 314 } 315 316 private static void header(PrintStream out, String section) { 317 out.println(); 318 out.println("-------------------------------------------"); 319 out.print(" "); 320 out.println(section); 321 out.println("-------------------------------------------"); 322 } 323 324 328 private static void doReportSystemProperties(PrintStream out) { 329 Properties sysprops = null; 330 try { 331 sysprops = System.getProperties(); 332 } catch (SecurityException e) { 333 ignoreThrowable(e); 334 out.println("Access to System.getProperties() blocked " 335 + "by a security manager"); 336 } 337 for (Enumeration keys = sysprops.propertyNames(); 338 keys.hasMoreElements();) { 339 String key = (String ) keys.nextElement(); 340 String value = getProperty(key); 341 out.println(key + " : " + value); 342 } 343 } 344 345 352 private static String getProperty(String key) { 353 String value; 354 try { 355 value = System.getProperty(key); 356 } catch (SecurityException e) { 357 value = ERROR_PROPERTY_ACCESS_BLOCKED; 358 } 359 return value; 360 } 361 362 366 private static void doReportAntProperties(PrintStream out) { 367 Project p = new Project(); 368 p.initProperties(); 369 out.println(MagicNames.ANT_VERSION + ": " + p.getProperty(MagicNames.ANT_VERSION)); 370 out.println(MagicNames.ANT_JAVA_VERSION + ": " 371 + p.getProperty(MagicNames.ANT_JAVA_VERSION)); 372 out.println(MagicNames.ANT_LIB + ": " + p.getProperty(MagicNames.ANT_LIB)); 373 out.println(MagicNames.ANT_HOME + ": " + p.getProperty(MagicNames.ANT_HOME)); 374 } 375 376 380 private static void doReportAntHomeLibraries(PrintStream out) { 381 out.println(MagicNames.ANT_HOME + ": " + System.getProperty(MagicNames.ANT_HOME)); 382 File [] libs = listLibraries(); 383 printLibraries(libs, out); 384 } 385 386 391 private static void doReportUserHomeLibraries(PrintStream out) { 392 String home = System.getProperty(Launcher.USER_HOMEDIR); 393 out.println("user.home: " + home); 394 File libDir = new File (home, Launcher.USER_LIBDIR); 395 File [] libs = listJarFiles(libDir); 396 printLibraries(libs, out); 397 } 398 399 404 private static void printLibraries(File [] libs, PrintStream out) { 405 if (libs == null) { 406 out.println("No such directory."); 407 return; 408 } 409 for (int i = 0; i < libs.length; i++) { 410 out.println(libs[i].getName() 411 + " (" + libs[i].length() + " bytes)"); 412 } 413 } 414 415 416 420 private static void doReportWhich(PrintStream out) { 421 Throwable error = null; 422 try { 423 Class which = Class.forName("org.apache.env.Which"); 424 Method method 425 = which.getMethod("main", new Class []{String [].class}); 426 method.invoke(null, new Object []{new String []{}}); 427 } catch (ClassNotFoundException e) { 428 out.println("Not available."); 429 out.println("Download it at http://xml.apache.org/commons/"); 430 } catch (InvocationTargetException e) { 431 error = e.getTargetException() == null ? e : e.getTargetException(); 432 } catch (Throwable e) { 433 error = e; 434 } 435 if (error != null) { 437 out.println("Error while running org.apache.env.Which"); 438 error.printStackTrace(); 439 } 440 } 441 442 450 private static void doReportTasksAvailability(PrintStream out) { 451 InputStream is = Main.class.getResourceAsStream( 452 MagicNames.TASKDEF_PROPERTIES_RESOURCE); 453 if (is == null) { 454 out.println("None available"); 455 } else { 456 Properties props = new Properties (); 457 try { 458 props.load(is); 459 for (Enumeration keys = props.keys(); keys.hasMoreElements();) { 460 String key = (String ) keys.nextElement(); 461 String classname = props.getProperty(key); 462 try { 463 Class.forName(classname); 464 props.remove(key); 465 } catch (ClassNotFoundException e) { 466 out.println(key + " : Not Available " 467 + "(the implementation class is not present)"); 468 } catch (NoClassDefFoundError e) { 469 String pkg = e.getMessage().replace('/', '.'); 470 out.println(key + " : Missing dependency " + pkg); 471 } catch (LinkageError e) { 472 out.println(key + " : Initialization error"); 473 } 474 } 475 if (props.size() == 0) { 476 out.println("All defined tasks are available"); 477 } else { 478 out.println("A task being missing/unavailable should only " 479 + "matter if you are trying to use it"); 480 } 481 } catch (IOException e) { 482 out.println(e.getMessage()); 483 } 484 } 485 } 486 487 491 private static void doReportParserInfo(PrintStream out) { 492 String parserName = getXmlParserName(); 493 String parserLocation = getXMLParserLocation(); 494 printParserInfo(out, "XML Parser", parserName, parserLocation); 495 printParserInfo(out, "Namespace-aware parser", 496 getNamespaceParserName(), 497 getNamespaceParserLocation()); 498 } 499 500 private static void printParserInfo(PrintStream out, 501 String parserType, 502 String parserName, 503 String parserLocation) { 504 if (parserName == null) { 505 parserName = "unknown"; 506 } 507 if (parserLocation == null) { 508 parserLocation = "unknown"; 509 } 510 out.println(parserType + " : " + parserName); 511 out.println(parserType + " Location: " + parserLocation); 512 } 513 514 520 private static void doReportTempDir(PrintStream out) { 521 String tempdir = System.getProperty("java.io.tmpdir"); 522 if (tempdir == null) { 523 out.println("Warning: java.io.tmpdir is undefined"); 524 return; 525 } 526 out.println("Temp dir is " + tempdir); 527 File tempDirectory = new File (tempdir); 528 if (!tempDirectory.exists()) { 529 out.println("Warning, java.io.tmpdir directory does not exist: " 530 + tempdir); 531 return; 532 } 533 long now = System.currentTimeMillis(); 535 File tempFile = null; 536 FileOutputStream fileout = null; 537 try { 538 tempFile = File.createTempFile("diag", "txt", tempDirectory); 539 fileout = new FileOutputStream (tempFile); 541 byte[] buffer = new byte[KILOBYTE]; 542 for (int i = 0; i < TEST_FILE_SIZE; i++) { 543 fileout.write(buffer); 544 } 545 fileout.close(); 546 fileout = null; 547 long filetime = tempFile.lastModified(); 548 tempFile.delete(); 549 out.println("Temp dir is writeable"); 550 long drift = filetime - now; 551 out.println("Temp dir alignment with system clock is " + drift + " ms"); 552 if (Math.abs(drift) > BIG_DRIFT_LIMIT) { 553 out.println("Warning: big clock drift -maybe a network filesystem"); 554 } 555 } catch (IOException e) { 556 ignoreThrowable(e); 557 out.println("Failed to create a temporary file in the temp dir " 558 + tempdir); 559 out.println("File " + tempFile + " could not be created/written to"); 560 } finally { 561 FileUtils.close(fileout); 562 if (tempFile != null && tempFile.exists()) { 563 tempFile.delete(); 564 } 565 } 566 } 567 568 572 private static void doReportLocale(PrintStream out) { 573 Calendar cal = Calendar.getInstance(); 575 TimeZone tz = cal.getTimeZone(); 576 out.println("Timezone " + tz.getDisplayName() 577 + " offset=" + tz.getOffset(cal.get(Calendar.ERA), 578 cal.get(Calendar.YEAR), 579 cal.get(Calendar.MONTH), 580 cal.get(Calendar.DAY_OF_MONTH), 581 cal.get(Calendar.DAY_OF_WEEK), 582 ((cal.get(Calendar.HOUR_OF_DAY) * MINUTES_PER_HOUR 583 + cal.get(Calendar.MINUTE)) * SECONDS_PER_MINUTE 584 + cal.get(Calendar.SECOND)) * SECONDS_PER_MILLISECOND 585 + cal.get(Calendar.MILLISECOND))); 586 } 587 588 594 private static void printProperty(PrintStream out, String key) { 595 String value = getProperty(key); 596 if (value != null) { 597 out.print(key); 598 out.print(" = "); 599 out.print('"'); 600 out.print(value); 601 out.println('"'); 602 } 603 } 604 605 611 private static void doReportProxy(PrintStream out) { 612 printProperty(out, ProxySetup.HTTP_PROXY_HOST); 613 printProperty(out, ProxySetup.HTTP_PROXY_PORT); 614 printProperty(out, ProxySetup.HTTP_PROXY_USERNAME); 615 printProperty(out, ProxySetup.HTTP_PROXY_PASSWORD); 616 printProperty(out, ProxySetup.HTTP_NON_PROXY_HOSTS); 617 printProperty(out, ProxySetup.HTTPS_PROXY_HOST); 618 printProperty(out, ProxySetup.HTTPS_PROXY_PORT); 619 printProperty(out, ProxySetup.HTTPS_NON_PROXY_HOSTS); 620 printProperty(out, ProxySetup.FTP_PROXY_HOST); 621 printProperty(out, ProxySetup.FTP_PROXY_PORT); 622 printProperty(out, ProxySetup.FTP_NON_PROXY_HOSTS); 623 printProperty(out, ProxySetup.SOCKS_PROXY_HOST); 624 printProperty(out, ProxySetup.SOCKS_PROXY_PORT); 625 printProperty(out, ProxySetup.SOCKS_PROXY_USERNAME); 626 printProperty(out, ProxySetup.SOCKS_PROXY_PASSWORD); 627 628 if (JavaEnvUtils.getJavaVersionNumber() < 15) { 629 return; 630 } 631 printProperty(out, ProxySetup.USE_SYSTEM_PROXIES); 632 final String proxyDiagClassname 633 = "org.apache.tools.ant.util.java15.ProxyDiagnostics"; 634 try { 635 Class proxyDiagClass = Class.forName(proxyDiagClassname); 636 Object instance = proxyDiagClass.newInstance(); 637 out.println("Java1.5+ proxy settings:"); 638 out.println(instance.toString()); 639 } catch (ClassNotFoundException e) { 640 } catch (IllegalAccessException e) { 642 } catch (InstantiationException e) { 644 } catch (NoClassDefFoundError e) { 646 } 648 } 649 650 } 651 | Popular Tags |