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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * Reference to Ant that holds this TaskManager
  23. */
  24. //private Ant ant;
  25. /**
  26. * Project to which this task manger belongs.
  27. */
  28. private Project project;
  29. /**
  30. * Data structure where all the Class definition for all known tasks are
  31. * held.
  32. */
  33. private Hashtable taskClasses = new Hashtable();
  34. /**
  35. * Data structure that holds all the nodes where tasks are picked up from.
  36. */
  37. private Vector taskPathNodes = new Vector();
  38. // -----------------------------------------------------------------
  39. // CONSTRUCTORS
  40. // -----------------------------------------------------------------
  41. /**
  42. * Creates a new TaskManager.
  43. */
  44. TaskManager(Project project) {
  45. this.project = project;
  46. }
  47. // -----------------------------------------------------------------
  48. // PACKAGE METHODS
  49. // -----------------------------------------------------------------
  50. /**
  51. * Adds a node to the task path
  52. */
  53. void addTaskPathNode(File file) {
  54. taskPathNodes.addElement(file);
  55. processTaskPathNode(file);
  56. }
  57. /**
  58. *
  59. */
  60. AbstractTask getTaskInstance(String taskName) throws AntException {
  61. Class clazz = (Class)taskClasses.get(taskName);
  62. try {
  63. return (AbstractTask)clazz.newInstance();
  64. } catch (Exception e) {
  65. String msg = "Can't instantiate task: " + taskName;
  66. AntException ae = new AntException(msg, e);
  67. throw ae;
  68. }
  69. }
  70. // -----------------------------------------------------------------
  71. // PRIVATE METHODS
  72. // -----------------------------------------------------------------
  73. /**
  74. * Returns an enum of the task names that are defined in a given
  75. * properties file.
  76. */
  77. private Enumeration getTaskNames(Properties props) {
  78. Vector v = new Vector();
  79. String s = props.getProperty("tasks");
  80. StringTokenizer tok = new StringTokenizer(s, ",", false);
  81. while (tok.hasMoreTokens()) {
  82. String taskName = tok.nextToken().trim();
  83. v.addElement(taskName);
  84. }
  85. return v.elements();
  86. }
  87. /**
  88. * Processes a directory to get class defintions from it
  89. */
  90. private void processDir(File dir) {
  91. project.getFrontEnd().writeMessage("Scanning " + dir + " for tasks",
  92. FrontEnd.MSG_LEVEL_LOW);
  93. File file = new File(dir, "taskdef.properties");
  94. if (file.exists()) {
  95. try {
  96. InputStream in = new FileInputStream(file);
  97. Properties props = new Properties();
  98. props.load(in);
  99. in.close();
  100. Enumeration enum = getTaskNames(props);
  101. while (enum.hasMoreElements()) {
  102. String taskName = (String)enum.nextElement();
  103. String taskClass = props.getProperty("task." + taskName + ".class");
  104. URLClassLoader loader = new URLClassLoader(new URL[] {dir.toURL()});
  105. try {
  106. Class clazz = loader.loadClass(taskClass);
  107. project.getFrontEnd().writeMessage("Got Task: " + taskName +
  108. clazz, FrontEnd.MSG_LEVEL_LOW);
  109. taskClasses.put(taskName, clazz);
  110. } catch (ClassNotFoundException cnfe) {
  111. System.out.println("Couldn't load task: " + taskName);
  112. System.out.println(cnfe);
  113. // XXX error out and stop....
  114. }
  115. }
  116. } catch (IOException ioe) {
  117. System.out.println("Could not work with dir: " + dir);
  118. System.out.println(ioe);
  119. // XXX error out and stop the build
  120. }
  121. }
  122. }
  123. /**
  124. * Processes a jar file to get class definitions from it
  125. */
  126. private void processJar(File file) {
  127. project.getFrontEnd().writeMessage("Scanning " + file + " for tasks",
  128. FrontEnd.MSG_LEVEL_LOW);
  129. try {
  130. ZipFile zipFile = new ZipFile(file);
  131. ZipEntry zipEntry = zipFile.getEntry("taskdef.properties");
  132. if (zipEntry != null) {
  133. InputStream in = zipFile.getInputStream(zipEntry);
  134. Properties props = new Properties();
  135. props.load(in);
  136. in.close();
  137. Enumeration enum = getTaskNames(props);
  138. while (enum.hasMoreElements()) {
  139. String taskName = (String)enum.nextElement();
  140. String taskClass = props.getProperty("task." + taskName + ".class");
  141. URLClassLoader loader = new URLClassLoader(new URL[] {file.toURL()});
  142. try {
  143. Class clazz = loader.loadClass(taskClass);
  144. project.getFrontEnd().writeMessage("Got Task: " + taskName +
  145. clazz, FrontEnd.MSG_LEVEL_LOW);
  146. taskClasses.put(taskName, clazz);
  147. } catch (ClassNotFoundException cnfe) {
  148. System.out.println("Couldn't load task: " + taskName);
  149. System.out.println(cnfe);
  150. // XXX error out and stop....
  151. }
  152. }
  153. }
  154. // make sure to not leave resources hanging
  155. zipFile.close();
  156. } catch (IOException ioe) {
  157. System.out.println("Couldn't work with file: " + file);
  158. System.out.println(ioe);
  159. // XXX need to exception out of here properly to stop things
  160. }
  161. }
  162. /**
  163. * Processes a node of the task path searching for task definitions there
  164. * and adding them to the list of known tasks
  165. */
  166. private void processTaskPathNode(File file) {
  167. // task path nodes can be any of the following:
  168. // * jar file
  169. // * directory of jar files
  170. // * directory holding class files
  171. if(file.isDirectory()) {
  172. // first look for all jar files here
  173. // second look for a taskdefs.properties here to see if we should
  174. // treat the directory as a classpath
  175. String[] files = file.list();
  176. for (int i = 0; i < files.length; i++) {
  177. if (files[i].endsWith(".jar")) {
  178. processJar(new File(file, files[i]));
  179. } else if (files[i].equals("taskdef.properties")) {
  180. processDir(file);
  181. }
  182. }
  183. } else if (file.getName().endsWith(".jar")) {
  184. processJar(file);
  185. }
  186. }
  187. }