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 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. public class TaskManager {
  18. // -----------------------------------------------------------------
  19. // PRIVATE DATA MEMBERS
  20. // -----------------------------------------------------------------
  21. /**
  22. * FrontEnd that this TaskManager can communicate through.
  23. */
  24. private FrontEnd frontEnd;
  25. /**
  26. * Data structure where all the Class definition for all known tasks are
  27. * held.
  28. */
  29. private Hashtable taskClasses = new Hashtable();
  30. /**
  31. * Data structure that holds all the nodes where tasks are picked up from.
  32. */
  33. private Vector taskPathNodes = new Vector();
  34. // -----------------------------------------------------------------
  35. // CONSTRUCTORS
  36. // -----------------------------------------------------------------
  37. /**
  38. * Creates a new TaskManager.
  39. */
  40. TaskManager(FrontEnd frontEnd) {
  41. System.out.println("CREATING TM");
  42. this.frontEnd = frontEnd;
  43. }
  44. // -----------------------------------------------------------------
  45. // PUBLIC METHODS
  46. // -----------------------------------------------------------------
  47. /**
  48. * Adds a node to the task path
  49. */
  50. public void addTaskPathNode(File file) {
  51. taskPathNodes.addElement(file);
  52. processTaskPathNode(file);
  53. }
  54. // -----------------------------------------------------------------
  55. // PACKAGE METHODS
  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. frontEnd.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. frontEnd.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. frontEnd.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. frontEnd.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. /**
  188. * Sets up the taskpath based on the currently running operating
  189. * system. In general, the ordering of the taskpath is: user directory,
  190. * system directory, and then installation. This allows users or
  191. * system admins to override or add tasks.
  192. */
  193. private void setUpTaskPath() {
  194. // 1st, add user's home dir.
  195. File f;
  196. String userHome = System.getProperty("user.home");
  197. // generic unix
  198. f = new File(userHome + ".ant", "tasks");
  199. if (f.exists() && f.isDirectory()) {
  200. addTaskPathNode(f);
  201. }
  202. // macos x
  203. f = new File(userHome + "/Library/Ant", "Tasks");
  204. if (f.exists() && f.isDirectory()) {
  205. addTaskPathNode(f);
  206. }
  207. // windows -- todo
  208. // 2nd, add system local dir.
  209. // generic unix
  210. f = new File("/usr/local/ant/tasks");
  211. if (f.exists() && f.isDirectory()) {
  212. addTaskPathNode(f);
  213. }
  214. // macos x
  215. f = new File("/Library/Ant/Tasks");
  216. if (f.exists() && f.isDirectory()) {
  217. addTaskPathNode(f);
  218. }
  219. // windows -- todo
  220. // 3rd, add installation local dir.
  221. //System.out.println("BASE: " + this.getClass().getResource("/"));
  222. // XXX ---- not really sure how the best way of getting this info is...
  223. // hafta think about it.
  224. }
  225. }