You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Diagnostics.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.io.File;
  56. import java.io.FilenameFilter;
  57. import java.io.PrintStream;
  58. import java.io.InputStream;
  59. import java.io.IOException;
  60. import java.util.Enumeration;
  61. import java.util.Properties;
  62. import java.lang.reflect.Method;
  63. import java.lang.reflect.InvocationTargetException;
  64. /**
  65. * A little diagnostic helper that output some information that may help
  66. * in support. It should quickly give correct information about the
  67. * jar existing in ant.home/lib and the jar versions...
  68. *
  69. * @since Ant 1.5
  70. * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
  71. */
  72. public final class Diagnostics {
  73. /** utility class */
  74. private Diagnostics(){
  75. }
  76. /**
  77. * Check if optional tasks are available. Not that it does not check
  78. * for implementation version. Use <tt>validateVersion()</tt> for this.
  79. * @return <tt>true</tt> if optional tasks are available.
  80. */
  81. public static boolean isOptionalAvailable() {
  82. try {
  83. Class.forName("org.apache.tools.ant.taskdefs.optional.Test");
  84. } catch (ClassNotFoundException e){
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * Check if core and optional implementation version do match.
  91. * @throws BuildException if the implementation version of optional tasks
  92. * does not match the core implementation version.
  93. */
  94. public static void validateVersion() throws BuildException {
  95. try {
  96. Class optional = Class.forName("org.apache.tools.ant.taskdefs.optional.Test");
  97. String coreVersion = getImplementationVersion(Main.class);
  98. String optionalVersion = getImplementationVersion(optional);
  99. if (coreVersion != null && !coreVersion.equals(optionalVersion) ){
  100. throw new BuildException(
  101. "Invalid implementation version between Ant core and Ant optional tasks.\n" +
  102. " core : " + coreVersion + "\n" +
  103. " optional: " + optionalVersion);
  104. }
  105. } catch (ClassNotFoundException e){
  106. }
  107. }
  108. /**
  109. * return the list of jar files existing in ANT_HOME/lib
  110. * and that must have been picked up by Ant script.
  111. * @return the list of jar files existing in ant.home/lib or
  112. * <tt>null</tt> if an error occurs.
  113. */
  114. public static File[] listLibraries() {
  115. String home = System.getProperty("ant.home");
  116. File libDir = new File(home, "lib");
  117. FilenameFilter filter = new FilenameFilter() {
  118. public boolean accept(File dir, String name) {
  119. return name.endsWith(".jar");
  120. }
  121. };
  122. // listFiles is JDK 1.2+ method...
  123. String[] filenames = libDir.list(filter);
  124. File[] files = new File[filenames.length];
  125. for (int i = 0; i < filenames.length; i++){
  126. files[i] = new File(libDir, filenames[i]);
  127. }
  128. return files;
  129. }
  130. /**
  131. * main entry point for command line
  132. * @param args command line arguments.
  133. */
  134. public static void main(String[] args){
  135. doReport(System.out);
  136. }
  137. /**
  138. * Helper method to get the implementation version.
  139. * @param clazz the class to get the information from.
  140. * @return null if there is no package or implementation version.
  141. * '?.?' for JDK 1.0 or 1.1.
  142. */
  143. private static String getImplementationVersion(Class clazz){
  144. try {
  145. // Package pkg = clazz.getPackage();
  146. Method method = Class.class.getMethod("getPackage", new Class[0]);
  147. Object pkg = method.invoke(clazz, null);
  148. if (pkg != null) {
  149. // pkg.getImplementationVersion();
  150. method = pkg.getClass().getMethod("getImplementationVersion", new Class[0]);
  151. Object version = method.invoke(pkg, null);
  152. return (String)version;
  153. }
  154. } catch (Exception e){
  155. // JDK < 1.2 should land here because the methods above don't exist.
  156. return "?.?";
  157. }
  158. return null;
  159. }
  160. /**
  161. * Print a report to the given stream.
  162. * @param out the stream to print the report to.
  163. */
  164. public static void doReport(PrintStream out){
  165. out.println("------- Ant diagnostics report -------");
  166. out.println(Main.getAntVersion());
  167. out.println();
  168. out.println("-------------------------------------------");
  169. out.println(" Implementation Version (JDK1.2+ only)");
  170. out.println("-------------------------------------------");
  171. out.println("core tasks : " + getImplementationVersion(Main.class));
  172. Class optional = null;
  173. try {
  174. optional = Class.forName(
  175. "org.apache.tools.ant.taskdefs.optional.Test");
  176. out.println("optional tasks : " + getImplementationVersion(optional));
  177. } catch (ClassNotFoundException e){
  178. out.println("optional tasks : not available");
  179. }
  180. out.println();
  181. out.println("-------------------------------------------");
  182. out.println(" ANT_HOME/lib jar listing");
  183. out.println("-------------------------------------------");
  184. doReportLibraries(out);
  185. out.println();
  186. out.println("-------------------------------------------");
  187. out.println(" Tasks availability");
  188. out.println("-------------------------------------------");
  189. doReportTasksAvailability(out);
  190. out.println();
  191. out.println("-------------------------------------------");
  192. out.println(" org.apache.env.Which diagnostics");
  193. out.println("-------------------------------------------");
  194. doReportWhich(out);
  195. out.println();
  196. out.println("-------------------------------------------");
  197. out.println(" System properties");
  198. out.println("-------------------------------------------");
  199. doReportSystemProperties(out);
  200. out.println();
  201. }
  202. /**
  203. * Report a listing of system properties existing in the current vm.
  204. * @param out the stream to print the properties to.
  205. */
  206. private static void doReportSystemProperties(PrintStream out){
  207. for( Enumeration keys = System.getProperties().keys();
  208. keys.hasMoreElements(); ){
  209. String key = (String)keys.nextElement();
  210. out.println(key + " : " + System.getProperty(key));
  211. }
  212. }
  213. /**
  214. * Report the content of ANT_HOME/lib directory
  215. * @param out the stream to print the content to
  216. */
  217. private static void doReportLibraries(PrintStream out){
  218. File[] libs = listLibraries();
  219. for (int i = 0; i < libs.length; i++){
  220. out.println(libs[i].getName()
  221. + " (" + libs[i].length() + " bytes)");
  222. }
  223. }
  224. /**
  225. * Call org.apache.env.Which if available
  226. * @param out the stream to print the content to.
  227. */
  228. private static void doReportWhich(PrintStream out){
  229. Throwable error = null;
  230. try {
  231. Class which = Class.forName("org.apache.env.Which");
  232. Method method = which.getMethod("main", new Class[]{ String[].class });
  233. method.invoke(null, new Object[]{new String[]{}});
  234. } catch (ClassNotFoundException e) {
  235. out.println("Not available.");
  236. out.println("Download it at http://xml.apache.org/commons/");
  237. } catch (InvocationTargetException e) {
  238. error = e.getTargetException() == null ? e : e.getTargetException();
  239. } catch (Exception e) {
  240. error = e;
  241. }
  242. // report error if something weird happens...this is diagnostic.
  243. if (error != null) {
  244. out.println("Error while running org.apache.env.Which");
  245. error.printStackTrace();
  246. }
  247. }
  248. /**
  249. * Create a report about non-available tasks that are defined in the
  250. * mapping but could not be found via lookup. It might generally happen
  251. * because Ant requires multiple libraries to compile and one of them
  252. * was missing when compiling Ant.
  253. * @param out the stream to print the tasks report to
  254. * @param is the stream defining the mapping task name/classname, can be
  255. * <tt>null</tt> for a missing stream (ie mapping).
  256. */
  257. private static void doReportTasksAvailability(PrintStream out){
  258. InputStream is = Main.class.getResourceAsStream("/org/apache/tools/ant/taskdefs/defaults.properties");
  259. if (is == null) {
  260. out.println("None available");
  261. } else {
  262. Properties props = new Properties();
  263. try {
  264. props.load(is);
  265. for (Enumeration keys = props.keys(); keys.hasMoreElements();){
  266. String key = (String)keys.nextElement();
  267. String classname = props.getProperty(key);
  268. try {
  269. Class.forName(classname);
  270. props.remove(key);
  271. } catch (ClassNotFoundException e){
  272. out.println(key + " : Not Available");
  273. }
  274. }
  275. if (props.size() == 0){
  276. out.println("All defined tasks are available");
  277. }
  278. } catch (IOException e){
  279. out.println(e.getMessage());
  280. }
  281. }
  282. }
  283. }