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.

TaskManager.java 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. package org.apache.ant;
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. import java.util.zip.*;
  9. /**
  10. * Manager of tasks and all things related to tasks. Tasks can be found in a
  11. * wide number of locations -- and most of these locations require class loading
  12. * help. As well, new nodes on the task search path may be added at any time.
  13. * When these are added, new tasks should be scanned for.
  14. *
  15. * @author James Duncan Davidson (duncan@apache.org)
  16. */
  17. class TaskManager {
  18. // -----------------------------------------------------------------
  19. // PRIVATE DATA MEMBERS
  20. // -----------------------------------------------------------------
  21. /**
  22. * Data structure where all the Class definition for all known tasks are
  23. * held.
  24. */
  25. private Hashtable taskClasses = new Hashtable();
  26. /**
  27. * Data structure that holds all the nodes where tasks are picked up from.
  28. */
  29. private Vector taskPathNodes = new Vector();
  30. // -----------------------------------------------------------------
  31. // CONSTRUCTORS
  32. // -----------------------------------------------------------------
  33. /**
  34. * Creates a new TaskManager.
  35. */
  36. TaskManager() {
  37. }
  38. // -----------------------------------------------------------------
  39. // PACKAGE METHODS
  40. // -----------------------------------------------------------------
  41. /**
  42. * Adds a node to the task path
  43. */
  44. void addTaskPathNode(File file) {
  45. taskPathNodes.addElement(file);
  46. processTaskPathNode(file);
  47. }
  48. /**
  49. *
  50. */
  51. AbstractTask getTaskInstance(String taskName) {
  52. Class clazz = (Class)taskClasses.get(taskName);
  53. try {
  54. return (AbstractTask)clazz.newInstance();
  55. } catch (Exception e) {
  56. System.out.println("Can't instantiate task: " + taskName);
  57. System.out.println(e);
  58. // XXX error out and stop
  59. }
  60. return null;
  61. }
  62. // -----------------------------------------------------------------
  63. // PRIVATE METHODS
  64. // -----------------------------------------------------------------
  65. /**
  66. * Returns an enum of the task names that are defined in a given
  67. * properties file.
  68. */
  69. private Enumeration getTaskNames(Properties props) {
  70. Vector v = new Vector();
  71. String s = props.getProperty("tasks");
  72. StringTokenizer tok = new StringTokenizer(s, ",", false);
  73. while (tok.hasMoreTokens()) {
  74. String taskName = tok.nextToken().trim();
  75. v.addElement(taskName);
  76. }
  77. return v.elements();
  78. }
  79. /**
  80. * Processes a directory to get class defintions from it
  81. */
  82. private void processDir(File dir) {
  83. System.out.println("Scanning " + dir + " for tasks");
  84. File file = new File(dir, "taskdef.properties");
  85. if (file.exists()) {
  86. try {
  87. InputStream in = new FileInputStream(file);
  88. Properties props = new Properties();
  89. props.load(in);
  90. in.close();
  91. Enumeration enum = getTaskNames(props);
  92. while (enum.hasMoreElements()) {
  93. String taskName = (String)enum.nextElement();
  94. String taskClass = props.getProperty("task." + taskName + ".class");
  95. URLClassLoader loader = new URLClassLoader(new URL[] {dir.toURL()});
  96. try {
  97. Class clazz = loader.loadClass(taskClass);
  98. System.out.println("Got task: " + taskName + " " + clazz);
  99. taskClasses.put(taskName, clazz);
  100. } catch (ClassNotFoundException cnfe) {
  101. System.out.println("Couldn't load task: " + taskName);
  102. System.out.println(cnfe);
  103. // XXX error out and stop....
  104. }
  105. }
  106. } catch (IOException ioe) {
  107. System.out.println("Could not work with dir: " + dir);
  108. System.out.println(ioe);
  109. // XXX error out and stop the build
  110. }
  111. }
  112. }
  113. /**
  114. * Processes a jar file to get class definitions from it
  115. */
  116. private void processJar(File file) {
  117. System.out.println("Scanning " + file + " for tasks");
  118. try {
  119. ZipFile zipFile = new ZipFile(file);
  120. ZipEntry zipEntry = zipFile.getEntry("taskdef.properties");
  121. if (zipEntry != null) {
  122. InputStream in = zipFile.getInputStream(zipEntry);
  123. Properties props = new Properties();
  124. props.load(in);
  125. in.close();
  126. Enumeration enum = getTaskNames(props);
  127. while (enum.hasMoreElements()) {
  128. String taskName = (String)enum.nextElement();
  129. String taskClass = props.getProperty("task." + taskName + ".class");
  130. URLClassLoader loader = new URLClassLoader(new URL[] {file.toURL()});
  131. try {
  132. Class clazz = loader.loadClass(taskClass);
  133. System.out.println("Got Task: " + taskName + " " + clazz);
  134. taskClasses.put(taskName, clazz);
  135. } catch (ClassNotFoundException cnfe) {
  136. System.out.println("Couldn't load task: " + taskName);
  137. System.out.println(cnfe);
  138. // XXX error out and stop....
  139. }
  140. }
  141. }
  142. // make sure to not leave resources hanging
  143. zipFile.close();
  144. } catch (IOException ioe) {
  145. System.out.println("Couldn't work with file: " + file);
  146. System.out.println(ioe);
  147. // XXX need to exception out of here properly to stop things
  148. }
  149. }
  150. /**
  151. * Processes a node of the task path searching for task definitions there
  152. * and adding them to the list of known tasks
  153. */
  154. private void processTaskPathNode(File file) {
  155. // task path nodes can be any of the following:
  156. // * jar file
  157. // * directory of jar files
  158. // * directory holding class files
  159. if(file.isDirectory()) {
  160. // first look for all jar files here
  161. // second look for a taskdefs.properties here to see if we should
  162. // treat the directory as a classpath
  163. String[] files = file.list();
  164. for (int i = 0; i < files.length; i++) {
  165. if (files[i].endsWith(".jar")) {
  166. processJar(new File(file, files[i]));
  167. } else if (files[i].equals("taskdef.properties")) {
  168. processDir(file);
  169. }
  170. }
  171. } else if (file.getName().endsWith(".jar")) {
  172. processJar(file);
  173. }
  174. }
  175. }