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.

Project.java 85 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 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 "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.IOException;
  57. import java.io.EOFException;
  58. import java.io.InputStream;
  59. import java.lang.reflect.Method;
  60. import java.lang.reflect.Modifier;
  61. import java.util.Enumeration;
  62. import java.util.Hashtable;
  63. import java.util.Properties;
  64. import java.util.Stack;
  65. import java.util.Vector;
  66. import org.apache.tools.ant.input.DefaultInputHandler;
  67. import org.apache.tools.ant.input.InputHandler;
  68. import org.apache.tools.ant.types.FilterSet;
  69. import org.apache.tools.ant.types.FilterSetCollection;
  70. import org.apache.tools.ant.types.Description;
  71. import org.apache.tools.ant.types.Path;
  72. import org.apache.tools.ant.util.FileUtils;
  73. import org.apache.tools.ant.util.JavaEnvUtils;
  74. import org.apache.tools.ant.util.WeakishReference;
  75. import org.apache.tools.ant.util.LazyHashtable;
  76. /**
  77. * Central representation of an Ant project. This class defines an
  78. * Ant project with all of its targets, tasks and various other
  79. * properties. It also provides the mechanism to kick off a build using
  80. * a particular target name.
  81. * <p>
  82. * This class also encapsulates methods which allow files to be referred
  83. * to using abstract path names which are translated to native system
  84. * file paths at runtime.
  85. *
  86. * @author duncan@x180.com
  87. *
  88. * @version $Revision$
  89. */
  90. public class Project {
  91. /** Message priority of "error". */
  92. public static final int MSG_ERR = 0;
  93. /** Message priority of "warning". */
  94. public static final int MSG_WARN = 1;
  95. /** Message priority of "information". */
  96. public static final int MSG_INFO = 2;
  97. /** Message priority of "verbose". */
  98. public static final int MSG_VERBOSE = 3;
  99. /** Message priority of "debug". */
  100. public static final int MSG_DEBUG = 4;
  101. /**
  102. * Constant for the "visiting" state, used when
  103. * traversing a DFS of target dependencies.
  104. */
  105. private static final String VISITING = "VISITING";
  106. /**
  107. * Constant for the "visited" state, used when
  108. * traversing a DFS of target dependencies.
  109. */
  110. private static final String VISITED = "VISITED";
  111. /**
  112. * The class name of the Ant class loader to use for
  113. * JDK 1.2 and above
  114. */
  115. private static final String ANTCLASSLOADER_JDK12
  116. = "org.apache.tools.ant.loader.AntClassLoader2";
  117. /**
  118. * Version constant for Java 1.0
  119. *
  120. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  121. */
  122. public static final String JAVA_1_0 = JavaEnvUtils.JAVA_1_0;
  123. /**
  124. * Version constant for Java 1.1
  125. *
  126. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  127. */
  128. public static final String JAVA_1_1 = JavaEnvUtils.JAVA_1_1;
  129. /**
  130. * Version constant for Java 1.2
  131. *
  132. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  133. */
  134. public static final String JAVA_1_2 = JavaEnvUtils.JAVA_1_2;
  135. /**
  136. * Version constant for Java 1.3
  137. *
  138. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  139. */
  140. public static final String JAVA_1_3 = JavaEnvUtils.JAVA_1_3;
  141. /**
  142. * Version constant for Java 1.4
  143. *
  144. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  145. */
  146. public static final String JAVA_1_4 = JavaEnvUtils.JAVA_1_4;
  147. /** Default filter start token. */
  148. public static final String TOKEN_START = FilterSet.DEFAULT_TOKEN_START;
  149. /** Default filter end token. */
  150. public static final String TOKEN_END = FilterSet.DEFAULT_TOKEN_END;
  151. /** Name of this project. */
  152. private String name;
  153. /** Description for this project (if any). */
  154. private String description;
  155. /** Map of references within the project (paths etc) (String to Object). */
  156. private Hashtable references = new AntRefTable(this);
  157. /** Name of the project's default target. */
  158. private String defaultTarget;
  159. /** Map from data type names to implementing classes (String to Class). */
  160. private Hashtable dataClassDefinitions = new AntTaskTable(this, false);
  161. /** Map from task names to implementing classes (String to Class). */
  162. private Hashtable taskClassDefinitions = new AntTaskTable(this, true);
  163. /**
  164. * Map from task names to vectors of created tasks
  165. * (String to Vector of Task). This is used to invalidate tasks if
  166. * the task definition changes.
  167. */
  168. private Hashtable createdTasks = new Hashtable();
  169. /** Map from target names to targets (String to Target). */
  170. private Hashtable targets = new Hashtable();
  171. /** Set of global filters. */
  172. private FilterSet globalFilterSet = new FilterSet();
  173. /**
  174. * Wrapper around globalFilterSet. This collection only ever
  175. * contains one FilterSet, but the wrapper is needed in order to
  176. * make it easier to use the FileUtils interface.
  177. */
  178. private FilterSetCollection globalFilters
  179. = new FilterSetCollection(globalFilterSet);
  180. /** Project base directory. */
  181. private File baseDir;
  182. /** List of listeners to notify of build events. */
  183. private Vector listeners = new Vector();
  184. /**
  185. * The Ant core classloader - may be <code>null</code> if using
  186. * parent classloader.
  187. */
  188. private ClassLoader coreLoader = null;
  189. /** Records the latest task to be executed on a thread (Thread to Task). */
  190. private Hashtable threadTasks = new Hashtable();
  191. /** Records the latest task to be executed on a thread Group. */
  192. private Hashtable threadGroupTasks = new Hashtable();
  193. /**
  194. * Called to handle any input requests.
  195. */
  196. private InputHandler inputHandler = null;
  197. /**
  198. * The default input stream used to read any input
  199. */
  200. private InputStream defaultInputStream = null;
  201. /**
  202. * Sets the input handler
  203. *
  204. * @param handler the InputHandler instance to use for gathering input.
  205. */
  206. public void setInputHandler(InputHandler handler) {
  207. inputHandler = handler;
  208. }
  209. /**
  210. * Set the default System input stream. Normally this stream is set to
  211. * System.in. This inputStream is used when no task inptu redirection is
  212. * being performed.
  213. *
  214. * @param defaultInputStream the default input stream to use when input
  215. * is reuested.
  216. * @since Ant 1.6
  217. */
  218. public void setDefaultInputStream(InputStream defaultInputStream) {
  219. this.defaultInputStream = defaultInputStream;
  220. }
  221. /**
  222. * Get this project's input stream
  223. *
  224. * @return the InputStream instance in use by this Porject instance to
  225. * read input
  226. */
  227. public InputStream getDefaultInputStream() {
  228. return defaultInputStream;
  229. }
  230. /**
  231. * Retrieves the current input handler.
  232. *
  233. * @return the InputHandler instance currently in place for the project
  234. * instance.
  235. */
  236. public InputHandler getInputHandler() {
  237. return inputHandler;
  238. }
  239. /** Instance of a utility class to use for file operations. */
  240. private FileUtils fileUtils;
  241. /**
  242. * Flag which catches Listeners which try to use System.out or System.err
  243. */
  244. private boolean loggingMessage = false;
  245. /**
  246. * Creates a new Ant project.
  247. */
  248. public Project() {
  249. fileUtils = FileUtils.newFileUtils();
  250. inputHandler = new DefaultInputHandler();
  251. }
  252. /**
  253. * Initialises the project.
  254. *
  255. * This involves setting the default task definitions and loading the
  256. * system properties.
  257. *
  258. * @exception BuildException if the default task list cannot be loaded
  259. */
  260. public void init() throws BuildException {
  261. setJavaVersionProperty();
  262. String defs = "/org/apache/tools/ant/taskdefs/defaults.properties";
  263. try {
  264. Properties props = new Properties();
  265. InputStream in = this.getClass().getResourceAsStream(defs);
  266. if (in == null) {
  267. throw new BuildException("Can't load default task list");
  268. }
  269. props.load(in);
  270. in.close();
  271. ((AntTaskTable) taskClassDefinitions).addDefinitions(props);
  272. } catch (IOException ioe) {
  273. throw new BuildException("Can't load default task list");
  274. }
  275. String dataDefs = "/org/apache/tools/ant/types/defaults.properties";
  276. try {
  277. Properties props = new Properties();
  278. InputStream in = this.getClass().getResourceAsStream(dataDefs);
  279. if (in == null) {
  280. throw new BuildException("Can't load default datatype list");
  281. }
  282. props.load(in);
  283. in.close();
  284. ((AntTaskTable) dataClassDefinitions).addDefinitions(props);
  285. } catch (IOException ioe) {
  286. throw new BuildException("Can't load default datatype list");
  287. }
  288. setSystemProperties();
  289. }
  290. /**
  291. * Factory method to create a class loader for loading classes
  292. *
  293. * @return an appropriate classloader
  294. */
  295. private AntClassLoader createClassLoader() {
  296. AntClassLoader loader = null;
  297. if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
  298. try {
  299. // 1.2+ - create advanced helper dynamically
  300. Class loaderClass
  301. = Class.forName(ANTCLASSLOADER_JDK12);
  302. loader = (AntClassLoader) loaderClass.newInstance();
  303. } catch (Exception e) {
  304. log("Unable to create Class Loader: "
  305. + e.getMessage(), Project.MSG_DEBUG);
  306. }
  307. }
  308. if (loader == null) {
  309. loader = new AntClassLoader();
  310. }
  311. loader.setProject(this);
  312. return loader;
  313. }
  314. /**
  315. * Factory method to create a class loader for loading classes from
  316. * a given path
  317. *
  318. * @param path the path from whcih clases are to be loaded.
  319. *
  320. * @return an appropriate classloader
  321. */
  322. public AntClassLoader createClassLoader(Path path) {
  323. AntClassLoader loader = createClassLoader();
  324. loader.setClassPath(path);
  325. return loader;
  326. }
  327. /**
  328. * Sets the core classloader for the project. If a <code>null</code>
  329. * classloader is specified, the parent classloader should be used.
  330. *
  331. * @param coreLoader The classloader to use for the project.
  332. * May be <code>null</code>.
  333. */
  334. public void setCoreLoader(ClassLoader coreLoader) {
  335. this.coreLoader = coreLoader;
  336. }
  337. /**
  338. * Returns the core classloader to use for this project.
  339. * This may be <code>null</code>, indicating that
  340. * the parent classloader should be used.
  341. *
  342. * @return the core classloader to use for this project.
  343. *
  344. */
  345. public ClassLoader getCoreLoader() {
  346. return coreLoader;
  347. }
  348. /**
  349. * Adds a build listener to the list. This listener will
  350. * be notified of build events for this project.
  351. *
  352. * @param listener The listener to add to the list.
  353. * Must not be <code>null</code>.
  354. */
  355. public void addBuildListener(BuildListener listener) {
  356. listeners.addElement(listener);
  357. }
  358. /**
  359. * Removes a build listener from the list. This listener
  360. * will no longer be notified of build events for this project.
  361. *
  362. * @param listener The listener to remove from the list.
  363. * Should not be <code>null</code>.
  364. */
  365. public void removeBuildListener(BuildListener listener) {
  366. listeners.removeElement(listener);
  367. }
  368. /**
  369. * Returns a list of build listeners for the project.
  370. *
  371. * @return a list of build listeners for the project
  372. */
  373. public Vector getBuildListeners() {
  374. return (Vector) listeners.clone();
  375. }
  376. /**
  377. * Writes a message to the log with the default log level
  378. * of MSG_INFO
  379. * @param message The text to log. Should not be <code>null</code>.
  380. */
  381. public void log(String message) {
  382. log(message, MSG_INFO);
  383. }
  384. /**
  385. * Writes a project level message to the log with the given log level.
  386. * @param message The text to log. Should not be <code>null</code>.
  387. * @param msgLevel The priority level to log at.
  388. */
  389. public void log(String message, int msgLevel) {
  390. fireMessageLogged(this, message, msgLevel);
  391. }
  392. /**
  393. * Writes a task level message to the log with the given log level.
  394. * @param task The task to use in the log. Must not be <code>null</code>.
  395. * @param message The text to log. Should not be <code>null</code>.
  396. * @param msgLevel The priority level to log at.
  397. */
  398. public void log(Task task, String message, int msgLevel) {
  399. fireMessageLogged(task, message, msgLevel);
  400. }
  401. /**
  402. * Writes a target level message to the log with the given log level.
  403. * @param target The target to use in the log.
  404. * Must not be <code>null</code>.
  405. * @param message The text to log. Should not be <code>null</code>.
  406. * @param msgLevel The priority level to log at.
  407. */
  408. public void log(Target target, String message, int msgLevel) {
  409. fireMessageLogged(target, message, msgLevel);
  410. }
  411. /**
  412. * Returns the set of global filters.
  413. *
  414. * @return the set of global filters
  415. */
  416. public FilterSet getGlobalFilterSet() {
  417. return globalFilterSet;
  418. }
  419. /**
  420. * Sets a property. Any existing property of the same name
  421. * is overwritten, unless it is a user property.
  422. * @param name The name of property to set.
  423. * Must not be <code>null</code>.
  424. * @param value The new value of the property.
  425. * Must not be <code>null</code>.
  426. */
  427. public synchronized void setProperty(String name, String value) {
  428. PropertyHelper.getPropertyHelper(this).
  429. setProperty(null, name, value, true);
  430. }
  431. /**
  432. * Sets a property if no value currently exists. If the property
  433. * exists already, a message is logged and the method returns with
  434. * no other effect.
  435. *
  436. * @param name The name of property to set.
  437. * Must not be <code>null</code>.
  438. * @param value The new value of the property.
  439. * Must not be <code>null</code>.
  440. * @since 1.5
  441. */
  442. public synchronized void setNewProperty(String name, String value) {
  443. PropertyHelper.getPropertyHelper(this).setNewProperty(null, name,
  444. value);
  445. }
  446. /**
  447. * Sets a user property, which cannot be overwritten by
  448. * set/unset property calls. Any previous value is overwritten.
  449. * @param name The name of property to set.
  450. * Must not be <code>null</code>.
  451. * @param value The new value of the property.
  452. * Must not be <code>null</code>.
  453. * @see #setProperty(String,String)
  454. */
  455. public synchronized void setUserProperty(String name, String value) {
  456. PropertyHelper.getPropertyHelper(this).setUserProperty(null, name,
  457. value);
  458. }
  459. /**
  460. * Sets a user property, which cannot be overwritten by set/unset
  461. * property calls. Any previous value is overwritten. Also marks
  462. * these properties as properties that have not come from the
  463. * command line.
  464. *
  465. * @param name The name of property to set.
  466. * Must not be <code>null</code>.
  467. * @param value The new value of the property.
  468. * Must not be <code>null</code>.
  469. * @see #setProperty(String,String)
  470. */
  471. public synchronized void setInheritedProperty(String name, String value) {
  472. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  473. ph.setInheritedProperty(null, name, value);
  474. }
  475. /**
  476. * Sets a property unless it is already defined as a user property
  477. * (in which case the method returns silently).
  478. *
  479. * @param name The name of the property.
  480. * Must not be <code>null</code>.
  481. * @param value The property value. Must not be <code>null</code>.
  482. */
  483. private void setPropertyInternal(String name, String value) {
  484. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  485. ph.setProperty(null, name, value, false);
  486. }
  487. /**
  488. * Returns the value of a property, if it is set.
  489. *
  490. * @param name The name of the property.
  491. * May be <code>null</code>, in which case
  492. * the return value is also <code>null</code>.
  493. * @return the property value, or <code>null</code> for no match
  494. * or if a <code>null</code> name is provided.
  495. */
  496. public String getProperty(String name) {
  497. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  498. return (String) ph.getProperty(null, name);
  499. }
  500. /**
  501. * Replaces ${} style constructions in the given value with the
  502. * string value of the corresponding data types.
  503. *
  504. * @param value The string to be scanned for property references.
  505. * May be <code>null</code>.
  506. *
  507. * @return the given string with embedded property names replaced
  508. * by values, or <code>null</code> if the given string is
  509. * <code>null</code>.
  510. *
  511. * @exception BuildException if the given value has an unclosed
  512. * property name, e.g. <code>${xxx</code>
  513. */
  514. public String replaceProperties(String value)
  515. throws BuildException {
  516. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  517. return ph.replaceProperties(null, value, null);
  518. }
  519. /**
  520. * Returns the value of a user property, if it is set.
  521. *
  522. * @param name The name of the property.
  523. * May be <code>null</code>, in which case
  524. * the return value is also <code>null</code>.
  525. * @return the property value, or <code>null</code> for no match
  526. * or if a <code>null</code> name is provided.
  527. */
  528. public String getUserProperty(String name) {
  529. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  530. return (String) ph.getUserProperty(null, name);
  531. }
  532. /**
  533. * Returns a copy of the properties table.
  534. * @return a hashtable containing all properties
  535. * (including user properties).
  536. */
  537. public Hashtable getProperties() {
  538. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  539. return ph.getProperties();
  540. }
  541. /**
  542. * Returns a copy of the user property hashtable
  543. * @return a hashtable containing just the user properties
  544. */
  545. public Hashtable getUserProperties() {
  546. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  547. return ph.getUserProperties();
  548. }
  549. /**
  550. * Copies all user properties that have been set on the command
  551. * line or a GUI tool from this instance to the Project instance
  552. * given as the argument.
  553. *
  554. * <p>To copy all "user" properties, you will also have to call
  555. * {@link #copyInheritedProperties copyInheritedProperties}.</p>
  556. *
  557. * @param other the project to copy the properties to. Must not be null.
  558. *
  559. * @since Ant 1.5
  560. */
  561. public void copyUserProperties(Project other) {
  562. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  563. ph.copyUserProperties(other);
  564. }
  565. /**
  566. * Copies all user properties that have not been set on the
  567. * command line or a GUI tool from this instance to the Project
  568. * instance given as the argument.
  569. *
  570. * <p>To copy all "user" properties, you will also have to call
  571. * {@link #copyUserProperties copyUserProperties}.</p>
  572. *
  573. * @param other the project to copy the properties to. Must not be null.
  574. *
  575. * @since Ant 1.5
  576. */
  577. public void copyInheritedProperties(Project other) {
  578. PropertyHelper ph = PropertyHelper.getPropertyHelper(this);
  579. ph.copyInheritedProperties(other);
  580. }
  581. /**
  582. * Sets the default target of the project.
  583. *
  584. * @param defaultTarget The name of the default target for this project.
  585. * May be <code>null</code>, indicating that there is
  586. * no default target.
  587. *
  588. * @deprecated use setDefault
  589. * @see #setDefault(String)
  590. */
  591. public void setDefaultTarget(String defaultTarget) {
  592. this.defaultTarget = defaultTarget;
  593. }
  594. /**
  595. * Returns the name of the default target of the project.
  596. * @return name of the default target or
  597. * <code>null</code> if no default has been set.
  598. */
  599. public String getDefaultTarget() {
  600. return defaultTarget;
  601. }
  602. /**
  603. * Sets the default target of the project.
  604. *
  605. * @param defaultTarget The name of the default target for this project.
  606. * May be <code>null</code>, indicating that there is
  607. * no default target.
  608. */
  609. public void setDefault(String defaultTarget) {
  610. this.defaultTarget = defaultTarget;
  611. }
  612. /**
  613. * Sets the name of the project, also setting the user
  614. * property <code>ant.project.name</code>.
  615. *
  616. * @param name The name of the project.
  617. * Must not be <code>null</code>.
  618. */
  619. public void setName(String name) {
  620. setUserProperty("ant.project.name", name);
  621. this.name = name;
  622. }
  623. /**
  624. * Returns the project name, if one has been set.
  625. *
  626. * @return the project name, or <code>null</code> if it hasn't been set.
  627. */
  628. public String getName() {
  629. return name;
  630. }
  631. /**
  632. * Sets the project description.
  633. *
  634. * @param description The description of the project.
  635. * May be <code>null</code>.
  636. */
  637. public void setDescription(String description) {
  638. this.description = description;
  639. }
  640. /**
  641. * Returns the project description, if one has been set.
  642. *
  643. * @return the project description, or <code>null</code> if it hasn't
  644. * been set.
  645. */
  646. public String getDescription() {
  647. if (description == null) {
  648. description = Description.getDescription(this);
  649. }
  650. return description;
  651. }
  652. /**
  653. * Adds a filter to the set of global filters.
  654. *
  655. * @param token The token to filter.
  656. * Must not be <code>null</code>.
  657. * @param value The replacement value.
  658. * Must not be <code>null</code>.
  659. * @deprecated Use getGlobalFilterSet().addFilter(token,value)
  660. *
  661. * @see #getGlobalFilterSet()
  662. * @see FilterSet#addFilter(String,String)
  663. */
  664. public void addFilter(String token, String value) {
  665. if (token == null) {
  666. return;
  667. }
  668. globalFilterSet.addFilter(new FilterSet.Filter(token, value));
  669. }
  670. /**
  671. * Returns a hashtable of global filters, mapping tokens to values.
  672. *
  673. * @return a hashtable of global filters, mapping tokens to values
  674. * (String to String).
  675. *
  676. * @deprecated Use getGlobalFilterSet().getFilterHash()
  677. *
  678. * @see #getGlobalFilterSet()
  679. * @see FilterSet#getFilterHash()
  680. */
  681. public Hashtable getFilters() {
  682. // we need to build the hashtable dynamically
  683. return globalFilterSet.getFilterHash();
  684. }
  685. /**
  686. * Sets the base directory for the project, checking that
  687. * the given filename exists and is a directory.
  688. *
  689. * @param baseD The project base directory.
  690. * Must not be <code>null</code>.
  691. *
  692. * @exception BuildException if the directory if invalid
  693. */
  694. public void setBasedir(String baseD) throws BuildException {
  695. setBaseDir(new File(baseD));
  696. }
  697. /**
  698. * Sets the base directory for the project, checking that
  699. * the given file exists and is a directory.
  700. *
  701. * @param baseDir The project base directory.
  702. * Must not be <code>null</code>.
  703. * @exception BuildException if the specified file doesn't exist or
  704. * isn't a directory
  705. */
  706. public void setBaseDir(File baseDir) throws BuildException {
  707. baseDir = fileUtils.normalize(baseDir.getAbsolutePath());
  708. if (!baseDir.exists()) {
  709. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  710. + " does not exist");
  711. }
  712. if (!baseDir.isDirectory()) {
  713. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  714. + " is not a directory");
  715. }
  716. this.baseDir = baseDir;
  717. setPropertyInternal("basedir", this.baseDir.getPath());
  718. String msg = "Project base dir set to: " + this.baseDir;
  719. log(msg, MSG_VERBOSE);
  720. }
  721. /**
  722. * Returns the base directory of the project as a file object.
  723. *
  724. * @return the project base directory, or <code>null</code> if the
  725. * base directory has not been successfully set to a valid value.
  726. */
  727. public File getBaseDir() {
  728. if (baseDir == null) {
  729. try {
  730. setBasedir(".");
  731. } catch (BuildException ex) {
  732. ex.printStackTrace();
  733. }
  734. }
  735. return baseDir;
  736. }
  737. /**
  738. * Returns the version of Java this class is running under.
  739. * @return the version of Java as a String, e.g. "1.1"
  740. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  741. * @deprecated use org.apache.tools.ant.util.JavaEnvUtils instead
  742. */
  743. public static String getJavaVersion() {
  744. return JavaEnvUtils.getJavaVersion();
  745. }
  746. /**
  747. * Sets the <code>ant.java.version</code> property and tests for
  748. * unsupported JVM versions. If the version is supported,
  749. * verbose log messages are generated to record the Java version
  750. * and operating system name.
  751. *
  752. * @exception BuildException if this Java version is not supported
  753. *
  754. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  755. */
  756. public void setJavaVersionProperty() throws BuildException {
  757. String javaVersion = JavaEnvUtils.getJavaVersion();
  758. setPropertyInternal("ant.java.version", javaVersion);
  759. // sanity check
  760. if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_0)) {
  761. throw new BuildException("Ant cannot work on Java 1.0");
  762. }
  763. log("Detected Java version: " + javaVersion + " in: "
  764. + System.getProperty("java.home"), MSG_VERBOSE);
  765. log("Detected OS: " + System.getProperty("os.name"), MSG_VERBOSE);
  766. }
  767. /**
  768. * Adds all system properties which aren't already defined as
  769. * user properties to the project properties.
  770. */
  771. public void setSystemProperties() {
  772. Properties systemP = System.getProperties();
  773. Enumeration e = systemP.keys();
  774. while (e.hasMoreElements()) {
  775. Object name = e.nextElement();
  776. String value = systemP.get(name).toString();
  777. this.setPropertyInternal(name.toString(), value);
  778. }
  779. }
  780. /**
  781. * Adds a new task definition to the project.
  782. * Attempting to override an existing definition with an
  783. * equivalent one (i.e. with the same classname) results in
  784. * a verbose log message. Attempting to override an existing definition
  785. * with a different one results in a warning log message and
  786. * invalidates any tasks which have already been created with the
  787. * old definition.
  788. *
  789. * @param taskName The name of the task to add.
  790. * Must not be <code>null</code>.
  791. * @param taskClass The full name of the class implementing the task.
  792. * Must not be <code>null</code>.
  793. *
  794. * @exception BuildException if the class is unsuitable for being an Ant
  795. * task. An error level message is logged before
  796. * this exception is thrown.
  797. *
  798. * @see #checkTaskClass(Class)
  799. */
  800. public void addTaskDefinition(String taskName, Class taskClass)
  801. throws BuildException {
  802. Class old = (Class) taskClassDefinitions.get(taskName);
  803. if (null != old) {
  804. if (old.equals(taskClass)) {
  805. log("Ignoring override for task " + taskName
  806. + ", it is already defined by the same class.",
  807. MSG_VERBOSE);
  808. return;
  809. } else {
  810. int logLevel = MSG_WARN;
  811. if (old.getName().equals(taskClass.getName())) {
  812. ClassLoader oldLoader = old.getClassLoader();
  813. ClassLoader newLoader = taskClass.getClassLoader();
  814. // system classloader on older JDKs can be null
  815. if (oldLoader != null
  816. && newLoader != null
  817. && oldLoader instanceof AntClassLoader
  818. && newLoader instanceof AntClassLoader
  819. && ((AntClassLoader) oldLoader).getClasspath()
  820. .equals(((AntClassLoader) newLoader).getClasspath())) {
  821. // same classname loaded from the same
  822. // classpath components
  823. logLevel = MSG_VERBOSE;
  824. }
  825. }
  826. log("Trying to override old definition of task " + taskName,
  827. logLevel);
  828. invalidateCreatedTasks(taskName);
  829. }
  830. }
  831. String msg = " +User task: " + taskName + " " + taskClass.getName();
  832. log(msg, MSG_DEBUG);
  833. checkTaskClass(taskClass);
  834. taskClassDefinitions.put(taskName, taskClass);
  835. }
  836. /**
  837. * Checks whether or not a class is suitable for serving as Ant task.
  838. * Ant task implementation classes must be public, concrete, and have
  839. * a no-arg constructor.
  840. *
  841. * @param taskClass The class to be checked.
  842. * Must not be <code>null</code>.
  843. *
  844. * @exception BuildException if the class is unsuitable for being an Ant
  845. * task. An error level message is logged before
  846. * this exception is thrown.
  847. */
  848. public void checkTaskClass(final Class taskClass) throws BuildException {
  849. if (!Modifier.isPublic(taskClass.getModifiers())) {
  850. final String message = taskClass + " is not public";
  851. log(message, Project.MSG_ERR);
  852. throw new BuildException(message);
  853. }
  854. if (Modifier.isAbstract(taskClass.getModifiers())) {
  855. final String message = taskClass + " is abstract";
  856. log(message, Project.MSG_ERR);
  857. throw new BuildException(message);
  858. }
  859. try {
  860. taskClass.getConstructor(null);
  861. // don't have to check for public, since
  862. // getConstructor finds public constructors only.
  863. } catch (NoSuchMethodException e) {
  864. final String message = "No public no-arg constructor in "
  865. + taskClass;
  866. log(message, Project.MSG_ERR);
  867. throw new BuildException(message);
  868. }
  869. if (!Task.class.isAssignableFrom(taskClass)) {
  870. TaskAdapter.checkTaskClass(taskClass, this);
  871. }
  872. }
  873. /**
  874. * Returns the current task definition hashtable. The returned hashtable is
  875. * "live" and so should not be modified.
  876. *
  877. * @return a map of from task name to implementing class
  878. * (String to Class).
  879. */
  880. public Hashtable getTaskDefinitions() {
  881. return taskClassDefinitions;
  882. }
  883. /**
  884. * Adds a new datatype definition.
  885. * Attempting to override an existing definition with an
  886. * equivalent one (i.e. with the same classname) results in
  887. * a verbose log message. Attempting to override an existing definition
  888. * with a different one results in a warning log message, but the
  889. * definition is changed.
  890. *
  891. * @param typeName The name of the datatype.
  892. * Must not be <code>null</code>.
  893. * @param typeClass The full name of the class implementing the datatype.
  894. * Must not be <code>null</code>.
  895. */
  896. public void addDataTypeDefinition(String typeName, Class typeClass) {
  897. synchronized (dataClassDefinitions) {
  898. Class old = (Class) dataClassDefinitions.get(typeName);
  899. if (null != old) {
  900. if (old.equals(typeClass)) {
  901. log("Ignoring override for datatype " + typeName
  902. + ", it is already defined by the same class.",
  903. MSG_VERBOSE);
  904. return;
  905. } else {
  906. log("Trying to override old definition of datatype "
  907. + typeName, MSG_WARN);
  908. }
  909. }
  910. dataClassDefinitions.put(typeName, typeClass);
  911. }
  912. String msg = " +User datatype: " + typeName + " "
  913. + typeClass.getName();
  914. log(msg, MSG_DEBUG);
  915. }
  916. /**
  917. * Returns the current datatype definition hashtable. The returned
  918. * hashtable is "live" and so should not be modified.
  919. *
  920. * @return a map of from datatype name to implementing class
  921. * (String to Class).
  922. */
  923. public Hashtable getDataTypeDefinitions() {
  924. return dataClassDefinitions;
  925. }
  926. /**
  927. * Adds a <em>new</em> target to the project.
  928. *
  929. * @param target The target to be added to the project.
  930. * Must not be <code>null</code>.
  931. *
  932. * @exception BuildException if the target already exists in the project
  933. *
  934. * @see Project#addOrReplaceTarget
  935. */
  936. public void addTarget(Target target) throws BuildException {
  937. String name = target.getName();
  938. if (targets.get(name) != null) {
  939. throw new BuildException("Duplicate target: `" + name + "'");
  940. }
  941. addOrReplaceTarget(name, target);
  942. }
  943. /**
  944. * Adds a <em>new</em> target to the project.
  945. *
  946. * @param targetName The name to use for the target.
  947. * Must not be <code>null</code>.
  948. * @param target The target to be added to the project.
  949. * Must not be <code>null</code>.
  950. *
  951. * @exception BuildException if the target already exists in the project
  952. *
  953. * @see Project#addOrReplaceTarget
  954. */
  955. public void addTarget(String targetName, Target target)
  956. throws BuildException {
  957. if (targets.get(targetName) != null) {
  958. throw new BuildException("Duplicate target: `" + targetName + "'");
  959. }
  960. addOrReplaceTarget(targetName, target);
  961. }
  962. /**
  963. * Adds a target to the project, or replaces one with the same
  964. * name.
  965. *
  966. * @param target The target to be added or replaced in the project.
  967. * Must not be <code>null</code>.
  968. */
  969. public void addOrReplaceTarget(Target target) {
  970. addOrReplaceTarget(target.getName(), target);
  971. }
  972. /**
  973. * Adds a target to the project, or replaces one with the same
  974. * name.
  975. *
  976. * @param targetName The name to use for the target.
  977. * Must not be <code>null</code>.
  978. * @param target The target to be added or replaced in the project.
  979. * Must not be <code>null</code>.
  980. */
  981. public void addOrReplaceTarget(String targetName, Target target) {
  982. String msg = " +Target: " + targetName;
  983. log(msg, MSG_DEBUG);
  984. target.setProject(this);
  985. targets.put(targetName, target);
  986. }
  987. /**
  988. * Returns the hashtable of targets. The returned hashtable
  989. * is "live" and so should not be modified.
  990. * @return a map from name to target (String to Target).
  991. */
  992. public Hashtable getTargets() {
  993. return targets;
  994. }
  995. /**
  996. * Creates a new instance of a task, adding it to a list of
  997. * created tasks for later invalidation. This causes all tasks
  998. * to be remembered until the containing project is removed
  999. * @param taskType The name of the task to create an instance of.
  1000. * Must not be <code>null</code>.
  1001. *
  1002. * @return an instance of the specified task, or <code>null</code> if
  1003. * the task name is not recognised.
  1004. *
  1005. * @exception BuildException if the task name is recognised but task
  1006. * creation fails.
  1007. */
  1008. public Task createTask(String taskType) throws BuildException {
  1009. Task task = createNewTask(taskType);
  1010. if (task != null) {
  1011. addCreatedTask(taskType, task);
  1012. }
  1013. return task;
  1014. }
  1015. /**
  1016. * Creates a new instance of a task. This task is not
  1017. * cached in the createdTasks list.
  1018. * @since ant1.6
  1019. * @param taskType The name of the task to create an instance of.
  1020. * Must not be <code>null</code>.
  1021. *
  1022. * @return an instance of the specified task, or <code>null</code> if
  1023. * the task name is not recognised.
  1024. *
  1025. * @exception BuildException if the task name is recognised but task
  1026. * creation fails.
  1027. */
  1028. private Task createNewTask(String taskType) throws BuildException {
  1029. Class c = (Class) taskClassDefinitions.get(taskType);
  1030. if (c == null) {
  1031. return null;
  1032. }
  1033. try {
  1034. Object o = c.newInstance();
  1035. setProjectReference( o );
  1036. Task task = null;
  1037. if (o instanceof Task) {
  1038. task = (Task) o;
  1039. } else {
  1040. // "Generic" Bean - use the setter pattern
  1041. // and an Adapter
  1042. TaskAdapter taskA = new TaskAdapter();
  1043. taskA.setProxy(o);
  1044. task = taskA;
  1045. }
  1046. task.setProject(this);
  1047. task.setTaskType(taskType);
  1048. // set default value, can be changed by the user
  1049. task.setTaskName(taskType);
  1050. String msg = " +Task: " + taskType;
  1051. log (msg, MSG_DEBUG);
  1052. return task;
  1053. } catch (Throwable t) {
  1054. String msg = "Could not create task of type: "
  1055. + taskType + " due to " + t;
  1056. throw new BuildException(msg, t);
  1057. }
  1058. }
  1059. /**
  1060. * Keeps a record of all tasks that have been created so that they
  1061. * can be invalidated if a new task definition overrides the current one.
  1062. *
  1063. * @param type The name of the type of task which has been created.
  1064. * Must not be <code>null</code>.
  1065. *
  1066. * @param task The freshly created task instance.
  1067. * Must not be <code>null</code>.
  1068. */
  1069. private void addCreatedTask(String type, Task task) {
  1070. synchronized (createdTasks) {
  1071. Vector v = (Vector) createdTasks.get(type);
  1072. if (v == null) {
  1073. v = new Vector();
  1074. createdTasks.put(type, v);
  1075. }
  1076. v.addElement(WeakishReference.createReference(task));
  1077. }
  1078. }
  1079. /**
  1080. * Mark tasks as invalid which no longer are of the correct type
  1081. * for a given taskname.
  1082. *
  1083. * @param type The name of the type of task to invalidate.
  1084. * Must not be <code>null</code>.
  1085. */
  1086. private void invalidateCreatedTasks(String type) {
  1087. synchronized (createdTasks) {
  1088. Vector v = (Vector) createdTasks.get(type);
  1089. if (v != null) {
  1090. Enumeration enum = v.elements();
  1091. while (enum.hasMoreElements()) {
  1092. WeakishReference ref =
  1093. (WeakishReference) enum.nextElement();
  1094. Task t = (Task) ref.get();
  1095. //being a weak ref, it may be null by this point
  1096. if (t != null) {
  1097. t.markInvalid();
  1098. }
  1099. }
  1100. v.removeAllElements();
  1101. createdTasks.remove(type);
  1102. }
  1103. }
  1104. }
  1105. /**
  1106. * Creates a new instance of a data type.
  1107. *
  1108. * @param typeName The name of the data type to create an instance of.
  1109. * Must not be <code>null</code>.
  1110. *
  1111. * @return an instance of the specified data type, or <code>null</code> if
  1112. * the data type name is not recognised.
  1113. *
  1114. * @exception BuildException if the data type name is recognised but
  1115. * instance creation fails.
  1116. */
  1117. public Object createDataType(String typeName) throws BuildException {
  1118. Class c = (Class) dataClassDefinitions.get(typeName);
  1119. if (c == null) {
  1120. return null;
  1121. }
  1122. try {
  1123. java.lang.reflect.Constructor ctor = null;
  1124. boolean noArg = false;
  1125. // DataType can have a "no arg" constructor or take a single
  1126. // Project argument.
  1127. try {
  1128. ctor = c.getConstructor(new Class[0]);
  1129. noArg = true;
  1130. } catch (NoSuchMethodException nse) {
  1131. ctor = c.getConstructor(new Class[] {Project.class});
  1132. noArg = false;
  1133. }
  1134. Object o = null;
  1135. if (noArg) {
  1136. o = ctor.newInstance(new Object[0]);
  1137. } else {
  1138. o = ctor.newInstance(new Object[] {this});
  1139. }
  1140. setProjectReference( o );
  1141. String msg = " +DataType: " + typeName;
  1142. log (msg, MSG_DEBUG);
  1143. return o;
  1144. } catch (java.lang.reflect.InvocationTargetException ite) {
  1145. Throwable t = ite.getTargetException();
  1146. String msg = "Could not create datatype of type: "
  1147. + typeName + " due to " + t;
  1148. throw new BuildException(msg, t);
  1149. } catch (Throwable t) {
  1150. String msg = "Could not create datatype of type: "
  1151. + typeName + " due to " + t;
  1152. throw new BuildException(msg, t);
  1153. }
  1154. }
  1155. /**
  1156. * Execute the specified sequence of targets, and the targets
  1157. * they depend on.
  1158. *
  1159. * @param targetNames A vector of target name strings to execute.
  1160. * Must not be <code>null</code>.
  1161. *
  1162. * @exception BuildException if the build failed
  1163. */
  1164. public void executeTargets(Vector targetNames) throws BuildException {
  1165. for (int i = 0; i < targetNames.size(); i++) {
  1166. executeTarget((String) targetNames.elementAt(i));
  1167. }
  1168. }
  1169. /**
  1170. * Demultiplexes output so that each task receives the appropriate
  1171. * messages. If the current thread is not currently executing a task,
  1172. * the message is logged directly.
  1173. *
  1174. * @param line Message to handle. Should not be <code>null</code>.
  1175. * @param isError Whether the text represents an error (<code>true</code>)
  1176. * or information (<code>false</code>).
  1177. */
  1178. public void demuxOutput(String line, boolean isError) {
  1179. Task task = getThreadTask(Thread.currentThread());
  1180. if (task == null) {
  1181. fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO);
  1182. } else {
  1183. if (isError) {
  1184. task.handleErrorOutput(line);
  1185. } else {
  1186. task.handleOutput(line);
  1187. }
  1188. }
  1189. }
  1190. /**
  1191. * Read data from the default input stream. If no default has been
  1192. * specified, System.in is used.
  1193. *
  1194. * @param buffer the buffer into which data is to be read.
  1195. * @param offset the offset into the buffer at which data is stored.
  1196. * @param length the amount of data to read
  1197. *
  1198. * @return the number of bytes read
  1199. *
  1200. * @exception IOException if the data cannot be read
  1201. * @since Ant 1.6
  1202. */
  1203. public int defaultInput(byte[] buffer, int offset, int length)
  1204. throws IOException {
  1205. if (defaultInputStream != null) {
  1206. return defaultInputStream.read(buffer, offset, length);
  1207. } else {
  1208. throw new EOFException("No input provided for project");
  1209. }
  1210. }
  1211. /**
  1212. * Demux an input request to the correct task.
  1213. *
  1214. * @param buffer the buffer into which data is to be read.
  1215. * @param offset the offset into the buffer at which data is stored.
  1216. * @param length the amount of data to read
  1217. *
  1218. * @return the number of bytes read
  1219. *
  1220. * @exception IOException if the data cannot be read
  1221. * @since Ant 1.6
  1222. */
  1223. public int demuxInput(byte[] buffer, int offset, int length)
  1224. throws IOException {
  1225. Task task = getThreadTask(Thread.currentThread());
  1226. if (task == null) {
  1227. return defaultInput(buffer, offset, length);
  1228. } else {
  1229. return task.handleInput(buffer, offset, length);
  1230. }
  1231. }
  1232. /**
  1233. * Demultiplexes flush operation so that each task receives the appropriate
  1234. * messages. If the current thread is not currently executing a task,
  1235. * the message is logged directly.
  1236. *
  1237. * @since Ant 1.5.2
  1238. *
  1239. * @param line Message to handle. Should not be <code>null</code>.
  1240. * @param isError Whether the text represents an error (<code>true</code>)
  1241. * or information (<code>false</code>).
  1242. */
  1243. public void demuxFlush(String line, boolean isError) {
  1244. Task task = getThreadTask(Thread.currentThread());
  1245. if (task == null) {
  1246. fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO);
  1247. } else {
  1248. if (isError) {
  1249. task.handleErrorFlush(line);
  1250. } else {
  1251. task.handleFlush(line);
  1252. }
  1253. }
  1254. }
  1255. /**
  1256. * Executes the specified target and any targets it depends on.
  1257. *
  1258. * @param targetName The name of the target to execute.
  1259. * Must not be <code>null</code>.
  1260. *
  1261. * @exception BuildException if the build failed
  1262. */
  1263. public void executeTarget(String targetName) throws BuildException {
  1264. // sanity check ourselves, if we've been asked to build nothing
  1265. // then we should complain
  1266. if (targetName == null) {
  1267. String msg = "No target specified";
  1268. throw new BuildException(msg);
  1269. }
  1270. // Sort the dependency tree, and run everything from the
  1271. // beginning until we hit our targetName.
  1272. // Sorting checks if all the targets (and dependencies)
  1273. // exist, and if there is any cycle in the dependency
  1274. // graph.
  1275. Vector sortedTargets = topoSort(targetName, targets);
  1276. int curidx = 0;
  1277. Target curtarget;
  1278. do {
  1279. curtarget = (Target) sortedTargets.elementAt(curidx++);
  1280. curtarget.performTasks();
  1281. } while (!curtarget.getName().equals(targetName));
  1282. }
  1283. /**
  1284. * Returns the canonical form of a filename.
  1285. * <p>
  1286. * If the specified file name is relative it is resolved
  1287. * with respect to the given root directory.
  1288. *
  1289. * @param fileName The name of the file to resolve.
  1290. * Must not be <code>null</code>.
  1291. *
  1292. * @param rootDir The directory to resolve relative file names with
  1293. * respect to. May be <code>null</code>, in which case
  1294. * the current directory is used.
  1295. *
  1296. * @return the resolved File.
  1297. *
  1298. * @deprecated
  1299. */
  1300. public File resolveFile(String fileName, File rootDir) {
  1301. return fileUtils.resolveFile(rootDir, fileName);
  1302. }
  1303. /**
  1304. * Returns the canonical form of a filename.
  1305. * <p>
  1306. * If the specified file name is relative it is resolved
  1307. * with respect to the project's base directory.
  1308. *
  1309. * @param fileName The name of the file to resolve.
  1310. * Must not be <code>null</code>.
  1311. *
  1312. * @return the resolved File.
  1313. *
  1314. */
  1315. public File resolveFile(String fileName) {
  1316. return fileUtils.resolveFile(baseDir, fileName);
  1317. }
  1318. /**
  1319. * Translates a path into its native (platform specific) format.
  1320. * <p>
  1321. * This method uses PathTokenizer to separate the input path
  1322. * into its components. This handles DOS style paths in a relatively
  1323. * sensible way. The file separators are then converted to their platform
  1324. * specific versions.
  1325. *
  1326. * @param toProcess The path to be translated.
  1327. * May be <code>null</code>.
  1328. *
  1329. * @return the native version of the specified path or
  1330. * an empty string if the path is <code>null</code> or empty.
  1331. *
  1332. * @see PathTokenizer
  1333. */
  1334. public static String translatePath(String toProcess) {
  1335. if (toProcess == null || toProcess.length() == 0) {
  1336. return "";
  1337. }
  1338. StringBuffer path = new StringBuffer(toProcess.length() + 50);
  1339. PathTokenizer tokenizer = new PathTokenizer(toProcess);
  1340. while (tokenizer.hasMoreTokens()) {
  1341. String pathComponent = tokenizer.nextToken();
  1342. pathComponent = pathComponent.replace('/', File.separatorChar);
  1343. pathComponent = pathComponent.replace('\\', File.separatorChar);
  1344. if (path.length() != 0) {
  1345. path.append(File.pathSeparatorChar);
  1346. }
  1347. path.append(pathComponent);
  1348. }
  1349. return path.toString();
  1350. }
  1351. /**
  1352. * Convenience method to copy a file from a source to a destination.
  1353. * No filtering is performed.
  1354. *
  1355. * @param sourceFile Name of file to copy from.
  1356. * Must not be <code>null</code>.
  1357. * @param destFile Name of file to copy to.
  1358. * Must not be <code>null</code>.
  1359. *
  1360. * @exception IOException if the copying fails
  1361. *
  1362. * @deprecated
  1363. */
  1364. public void copyFile(String sourceFile, String destFile)
  1365. throws IOException {
  1366. fileUtils.copyFile(sourceFile, destFile);
  1367. }
  1368. /**
  1369. * Convenience method to copy a file from a source to a destination
  1370. * specifying if token filtering should be used.
  1371. *
  1372. * @param sourceFile Name of file to copy from.
  1373. * Must not be <code>null</code>.
  1374. * @param destFile Name of file to copy to.
  1375. * Must not be <code>null</code>.
  1376. * @param filtering Whether or not token filtering should be used during
  1377. * the copy.
  1378. *
  1379. * @exception IOException if the copying fails
  1380. *
  1381. * @deprecated
  1382. */
  1383. public void copyFile(String sourceFile, String destFile, boolean filtering)
  1384. throws IOException {
  1385. fileUtils.copyFile(sourceFile, destFile,
  1386. filtering ? globalFilters : null);
  1387. }
  1388. /**
  1389. * Convenience method to copy a file from a source to a
  1390. * destination specifying if token filtering should be used and if
  1391. * source files may overwrite newer destination files.
  1392. *
  1393. * @param sourceFile Name of file to copy from.
  1394. * Must not be <code>null</code>.
  1395. * @param destFile Name of file to copy to.
  1396. * Must not be <code>null</code>.
  1397. * @param filtering Whether or not token filtering should be used during
  1398. * the copy.
  1399. * @param overwrite Whether or not the destination file should be
  1400. * overwritten if it already exists.
  1401. *
  1402. * @exception IOException if the copying fails
  1403. *
  1404. * @deprecated
  1405. */
  1406. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1407. boolean overwrite) throws IOException {
  1408. fileUtils.copyFile(sourceFile, destFile,
  1409. filtering ? globalFilters : null, overwrite);
  1410. }
  1411. /**
  1412. * Convenience method to copy a file from a source to a
  1413. * destination specifying if token filtering should be used, if
  1414. * source files may overwrite newer destination files, and if the
  1415. * last modified time of the resulting file should be set to
  1416. * that of the source file.
  1417. *
  1418. * @param sourceFile Name of file to copy from.
  1419. * Must not be <code>null</code>.
  1420. * @param destFile Name of file to copy to.
  1421. * Must not be <code>null</code>.
  1422. * @param filtering Whether or not token filtering should be used during
  1423. * the copy.
  1424. * @param overwrite Whether or not the destination file should be
  1425. * overwritten if it already exists.
  1426. * @param preserveLastModified Whether or not the last modified time of
  1427. * the resulting file should be set to that
  1428. * of the source file.
  1429. *
  1430. * @exception IOException if the copying fails
  1431. *
  1432. * @deprecated
  1433. */
  1434. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1435. boolean overwrite, boolean preserveLastModified)
  1436. throws IOException {
  1437. fileUtils.copyFile(sourceFile, destFile,
  1438. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1439. }
  1440. /**
  1441. * Convenience method to copy a file from a source to a destination.
  1442. * No filtering is performed.
  1443. *
  1444. * @param sourceFile File to copy from.
  1445. * Must not be <code>null</code>.
  1446. * @param destFile File to copy to.
  1447. * Must not be <code>null</code>.
  1448. *
  1449. * @exception IOException if the copying fails
  1450. *
  1451. * @deprecated
  1452. */
  1453. public void copyFile(File sourceFile, File destFile) throws IOException {
  1454. fileUtils.copyFile(sourceFile, destFile);
  1455. }
  1456. /**
  1457. * Convenience method to copy a file from a source to a destination
  1458. * specifying if token filtering should be used.
  1459. *
  1460. * @param sourceFile File to copy from.
  1461. * Must not be <code>null</code>.
  1462. * @param destFile File to copy to.
  1463. * Must not be <code>null</code>.
  1464. * @param filtering Whether or not token filtering should be used during
  1465. * the copy.
  1466. *
  1467. * @exception IOException if the copying fails
  1468. *
  1469. * @deprecated
  1470. */
  1471. public void copyFile(File sourceFile, File destFile, boolean filtering)
  1472. throws IOException {
  1473. fileUtils.copyFile(sourceFile, destFile,
  1474. filtering ? globalFilters : null);
  1475. }
  1476. /**
  1477. * Convenience method to copy a file from a source to a
  1478. * destination specifying if token filtering should be used and if
  1479. * source files may overwrite newer destination files.
  1480. *
  1481. * @param sourceFile File to copy from.
  1482. * Must not be <code>null</code>.
  1483. * @param destFile File to copy to.
  1484. * Must not be <code>null</code>.
  1485. * @param filtering Whether or not token filtering should be used during
  1486. * the copy.
  1487. * @param overwrite Whether or not the destination file should be
  1488. * overwritten if it already exists.
  1489. *
  1490. * @exception IOException if the file cannot be copied.
  1491. *
  1492. * @deprecated
  1493. */
  1494. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1495. boolean overwrite) throws IOException {
  1496. fileUtils.copyFile(sourceFile, destFile,
  1497. filtering ? globalFilters : null, overwrite);
  1498. }
  1499. /**
  1500. * Convenience method to copy a file from a source to a
  1501. * destination specifying if token filtering should be used, if
  1502. * source files may overwrite newer destination files, and if the
  1503. * last modified time of the resulting file should be set to
  1504. * that of the source file.
  1505. *
  1506. * @param sourceFile File to copy from.
  1507. * Must not be <code>null</code>.
  1508. * @param destFile File to copy to.
  1509. * Must not be <code>null</code>.
  1510. * @param filtering Whether or not token filtering should be used during
  1511. * the copy.
  1512. * @param overwrite Whether or not the destination file should be
  1513. * overwritten if it already exists.
  1514. * @param preserveLastModified Whether or not the last modified time of
  1515. * the resulting file should be set to that
  1516. * of the source file.
  1517. *
  1518. * @exception IOException if the file cannot be copied.
  1519. *
  1520. * @deprecated
  1521. */
  1522. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1523. boolean overwrite, boolean preserveLastModified)
  1524. throws IOException {
  1525. fileUtils.copyFile(sourceFile, destFile,
  1526. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1527. }
  1528. /**
  1529. * Calls File.setLastModified(long time) on Java above 1.1, and logs
  1530. * a warning on Java 1.1.
  1531. *
  1532. * @param file The file to set the last modified time on.
  1533. * Must not be <code>null</code>.
  1534. *
  1535. * @param time the required modification time.
  1536. *
  1537. * @deprecated
  1538. *
  1539. * @exception BuildException if the last modified time cannot be set
  1540. * despite running on a platform with a version
  1541. * above 1.1.
  1542. */
  1543. public void setFileLastModified(File file, long time)
  1544. throws BuildException {
  1545. if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
  1546. log("Cannot change the modification time of " + file
  1547. + " in JDK 1.1", Project.MSG_WARN);
  1548. return;
  1549. }
  1550. fileUtils.setFileLastModified(file, time);
  1551. log("Setting modification time for " + file, MSG_VERBOSE);
  1552. }
  1553. /**
  1554. * Returns the boolean equivalent of a string, which is considered
  1555. * <code>true</code> if either <code>"on"</code>, <code>"true"</code>,
  1556. * or <code>"yes"</code> is found, ignoring case.
  1557. *
  1558. * @param s The string to convert to a boolean value.
  1559. * Must not be <code>null</code>.
  1560. *
  1561. * @return <code>true</code> if the given string is <code>"on"</code>,
  1562. * <code>"true"</code> or <code>"yes"</code>, or
  1563. * <code>false</code> otherwise.
  1564. */
  1565. public static boolean toBoolean(String s) {
  1566. return (s.equalsIgnoreCase("on") ||
  1567. s.equalsIgnoreCase("true") ||
  1568. s.equalsIgnoreCase("yes"));
  1569. }
  1570. /**
  1571. * Topologically sorts a set of targets.
  1572. *
  1573. * @param root The name of the root target. The sort is created in such
  1574. * a way that the sequence of Targets up to the root
  1575. * target is the minimum possible such sequence.
  1576. * Must not be <code>null</code>.
  1577. * @param targets A map of names to targets (String to Target).
  1578. * Must not be <code>null</code>.
  1579. * @return a vector of strings with the names of the targets in
  1580. * sorted order.
  1581. * @exception BuildException if there is a cyclic dependency among the
  1582. * targets, or if a named target does not exist.
  1583. */
  1584. public final Vector topoSort(String root, Hashtable targets)
  1585. throws BuildException {
  1586. Vector ret = new Vector();
  1587. Hashtable state = new Hashtable();
  1588. Stack visiting = new Stack();
  1589. // We first run a DFS based sort using the root as the starting node.
  1590. // This creates the minimum sequence of Targets to the root node.
  1591. // We then do a sort on any remaining unVISITED targets.
  1592. // This is unnecessary for doing our build, but it catches
  1593. // circular dependencies or missing Targets on the entire
  1594. // dependency tree, not just on the Targets that depend on the
  1595. // build Target.
  1596. tsort(root, targets, state, visiting, ret);
  1597. log("Build sequence for target `" + root + "' is " + ret, MSG_VERBOSE);
  1598. for (Enumeration en = targets.keys(); en.hasMoreElements();) {
  1599. String curTarget = (String) en.nextElement();
  1600. String st = (String) state.get(curTarget);
  1601. if (st == null) {
  1602. tsort(curTarget, targets, state, visiting, ret);
  1603. } else if (st == VISITING) {
  1604. throw new RuntimeException("Unexpected node in visiting state: "
  1605. + curTarget);
  1606. }
  1607. }
  1608. log("Complete build sequence is " + ret, MSG_VERBOSE);
  1609. return ret;
  1610. }
  1611. /**
  1612. * Performs a single step in a recursive depth-first-search traversal of
  1613. * the target dependency tree.
  1614. * <p>
  1615. * The current target is first set to the "visiting" state, and pushed
  1616. * onto the "visiting" stack.
  1617. * <p>
  1618. * An exception is then thrown if any child of the current node is in the
  1619. * visiting state, as that implies a circular dependency. The exception
  1620. * contains details of the cycle, using elements of the "visiting" stack.
  1621. * <p>
  1622. * If any child has not already been "visited", this method is called
  1623. * recursively on it.
  1624. * <p>
  1625. * The current target is then added to the ordered list of targets. Note
  1626. * that this is performed after the children have been visited in order
  1627. * to get the correct order. The current target is set to the "visited"
  1628. * state.
  1629. * <p>
  1630. * By the time this method returns, the ordered list contains the sequence
  1631. * of targets up to and including the current target.
  1632. *
  1633. * @param root The current target to inspect.
  1634. * Must not be <code>null</code>.
  1635. * @param targets A mapping from names to targets (String to Target).
  1636. * Must not be <code>null</code>.
  1637. * @param state A mapping from target names to states
  1638. * (String to String).
  1639. * The states in question are "VISITING" and "VISITED".
  1640. * Must not be <code>null</code>.
  1641. * @param visiting A stack of targets which are currently being visited.
  1642. * Must not be <code>null</code>.
  1643. * @param ret The list to add target names to. This will end up
  1644. * containing the complete list of depenencies in
  1645. * dependency order.
  1646. * Must not be <code>null</code>.
  1647. *
  1648. * @exception BuildException if a non-existent target is specified or if
  1649. * a circular dependency is detected.
  1650. */
  1651. private final void tsort(String root, Hashtable targets,
  1652. Hashtable state, Stack visiting,
  1653. Vector ret)
  1654. throws BuildException {
  1655. state.put(root, VISITING);
  1656. visiting.push(root);
  1657. Target target = (Target) targets.get(root);
  1658. // Make sure we exist
  1659. if (target == null) {
  1660. StringBuffer sb = new StringBuffer("Target `");
  1661. sb.append(root);
  1662. sb.append("' does not exist in this project. ");
  1663. visiting.pop();
  1664. if (!visiting.empty()) {
  1665. String parent = (String) visiting.peek();
  1666. sb.append("It is used from target `");
  1667. sb.append(parent);
  1668. sb.append("'.");
  1669. }
  1670. throw new BuildException(new String(sb));
  1671. }
  1672. for (Enumeration en = target.getDependencies(); en.hasMoreElements();) {
  1673. String cur = (String) en.nextElement();
  1674. String m = (String) state.get(cur);
  1675. if (m == null) {
  1676. // Not been visited
  1677. tsort(cur, targets, state, visiting, ret);
  1678. } else if (m == VISITING) {
  1679. // Currently visiting this node, so have a cycle
  1680. throw makeCircularException(cur, visiting);
  1681. }
  1682. }
  1683. String p = (String) visiting.pop();
  1684. if (root != p) {
  1685. throw new RuntimeException("Unexpected internal error: expected to "
  1686. + "pop " + root + " but got " + p);
  1687. }
  1688. state.put(root, VISITED);
  1689. ret.addElement(target);
  1690. }
  1691. /**
  1692. * Builds an appropriate exception detailing a specified circular
  1693. * dependency.
  1694. *
  1695. * @param end The dependency to stop at. Must not be <code>null</code>.
  1696. * @param stk A stack of dependencies. Must not be <code>null</code>.
  1697. *
  1698. * @return a BuildException detailing the specified circular dependency.
  1699. */
  1700. private static BuildException makeCircularException(String end, Stack stk) {
  1701. StringBuffer sb = new StringBuffer("Circular dependency: ");
  1702. sb.append(end);
  1703. String c;
  1704. do {
  1705. c = (String) stk.pop();
  1706. sb.append(" <- ");
  1707. sb.append(c);
  1708. } while (!c.equals(end));
  1709. return new BuildException(new String(sb));
  1710. }
  1711. /**
  1712. * Adds a reference to the project.
  1713. *
  1714. * @param name The name of the reference. Must not be <code>null</code>.
  1715. * @param value The value of the reference. Must not be <code>null</code>.
  1716. */
  1717. public void addReference(String name, Object value) {
  1718. synchronized (references) {
  1719. Object old = ((AntRefTable) references).getReal(name);
  1720. if (old == value) {
  1721. // no warning, this is not changing anything
  1722. return;
  1723. }
  1724. if (old != null && !(old instanceof UnknownElement)) {
  1725. log("Overriding previous definition of reference to " + name,
  1726. MSG_WARN);
  1727. }
  1728. String valueAsString = "";
  1729. try {
  1730. valueAsString = value.toString();
  1731. } catch (Throwable t) {
  1732. log("Caught exception (" + t.getClass().getName() + ")"
  1733. + " while expanding " + name + ": " + t.getMessage(),
  1734. MSG_WARN);
  1735. }
  1736. log("Adding reference: " + name + " -> " + valueAsString,
  1737. MSG_DEBUG);
  1738. references.put(name, value);
  1739. }
  1740. }
  1741. /**
  1742. * Returns a map of the references in the project (String to Object).
  1743. * The returned hashtable is "live" and so must not be modified.
  1744. *
  1745. * @return a map of the references in the project (String to Object).
  1746. */
  1747. public Hashtable getReferences() {
  1748. return references;
  1749. }
  1750. /**
  1751. * Looks up a reference by its key (ID).
  1752. *
  1753. * @param key The key for the desired reference.
  1754. * Must not be <code>null</code>.
  1755. *
  1756. * @return the reference with the specified ID, or <code>null</code> if
  1757. * there is no such reference in the project.
  1758. */
  1759. public Object getReference(String key) {
  1760. return references.get(key);
  1761. }
  1762. /**
  1763. * Returns a description of the type of the given element, with
  1764. * special handling for instances of tasks and data types.
  1765. * <p>
  1766. * This is useful for logging purposes.
  1767. *
  1768. * @param element The element to describe.
  1769. * Must not be <code>null</code>.
  1770. *
  1771. * @return a description of the element type
  1772. *
  1773. * @since 1.95, Ant 1.5
  1774. */
  1775. public String getElementName(Object element) {
  1776. Hashtable elements = taskClassDefinitions;
  1777. Class elementClass = element.getClass();
  1778. String typeName = "task";
  1779. if (!elements.contains(elementClass)) {
  1780. elements = dataClassDefinitions;
  1781. typeName = "data type";
  1782. if (!elements.contains(elementClass)) {
  1783. elements = null;
  1784. }
  1785. }
  1786. if (elements != null) {
  1787. Enumeration e = elements.keys();
  1788. while (e.hasMoreElements()) {
  1789. String name = (String) e.nextElement();
  1790. Class clazz = (Class) elements.get(name);
  1791. if (elementClass.equals(clazz)) {
  1792. return "The <" + name + "> " + typeName;
  1793. }
  1794. }
  1795. }
  1796. return "Class " + elementClass.getName();
  1797. }
  1798. /**
  1799. * Sends a "build started" event to the build listeners for this project.
  1800. */
  1801. public void fireBuildStarted() {
  1802. BuildEvent event = new BuildEvent(this);
  1803. Vector listeners = getBuildListeners();
  1804. int size = listeners.size();
  1805. for (int i = 0; i < size; i++) {
  1806. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1807. listener.buildStarted(event);
  1808. }
  1809. }
  1810. /**
  1811. * Sends a "build finished" event to the build listeners for this project.
  1812. * @param exception an exception indicating a reason for a build
  1813. * failure. May be <code>null</code>, indicating
  1814. * a successful build.
  1815. */
  1816. public void fireBuildFinished(Throwable exception) {
  1817. BuildEvent event = new BuildEvent(this);
  1818. event.setException(exception);
  1819. Vector listeners = getBuildListeners();
  1820. int size = listeners.size();
  1821. for (int i = 0; i < size; i++) {
  1822. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1823. listener.buildFinished(event);
  1824. }
  1825. }
  1826. /**
  1827. * Sends a "target started" event to the build listeners for this project.
  1828. *
  1829. * @param target The target which is starting to build.
  1830. * Must not be <code>null</code>.
  1831. */
  1832. protected void fireTargetStarted(Target target) {
  1833. BuildEvent event = new BuildEvent(target);
  1834. Vector listeners = getBuildListeners();
  1835. int size = listeners.size();
  1836. for (int i = 0; i < size; i++) {
  1837. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1838. listener.targetStarted(event);
  1839. }
  1840. }
  1841. /**
  1842. * Sends a "target finished" event to the build listeners for this
  1843. * project.
  1844. *
  1845. * @param target The target which has finished building.
  1846. * Must not be <code>null</code>.
  1847. * @param exception an exception indicating a reason for a build
  1848. * failure. May be <code>null</code>, indicating
  1849. * a successful build.
  1850. */
  1851. protected void fireTargetFinished(Target target, Throwable exception) {
  1852. BuildEvent event = new BuildEvent(target);
  1853. event.setException(exception);
  1854. Vector listeners = getBuildListeners();
  1855. int size = listeners.size();
  1856. for (int i = 0; i < size; i++) {
  1857. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1858. listener.targetFinished(event);
  1859. }
  1860. }
  1861. /**
  1862. * Sends a "task started" event to the build listeners for this project.
  1863. *
  1864. * @param task The target which is starting to execute.
  1865. * Must not be <code>null</code>.
  1866. */
  1867. protected void fireTaskStarted(Task task) {
  1868. // register this as the current task on the current thread.
  1869. registerThreadTask(Thread.currentThread(), task);
  1870. BuildEvent event = new BuildEvent(task);
  1871. Vector listeners = getBuildListeners();
  1872. int size = listeners.size();
  1873. for (int i = 0; i < size; i++) {
  1874. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1875. listener.taskStarted(event);
  1876. }
  1877. }
  1878. /**
  1879. * Sends a "task finished" event to the build listeners for this
  1880. * project.
  1881. *
  1882. * @param task The task which has finished executing.
  1883. * Must not be <code>null</code>.
  1884. * @param exception an exception indicating a reason for a build
  1885. * failure. May be <code>null</code>, indicating
  1886. * a successful build.
  1887. */
  1888. protected void fireTaskFinished(Task task, Throwable exception) {
  1889. registerThreadTask(Thread.currentThread(), null);
  1890. System.out.flush();
  1891. System.err.flush();
  1892. BuildEvent event = new BuildEvent(task);
  1893. event.setException(exception);
  1894. Vector listeners = getBuildListeners();
  1895. int size = listeners.size();
  1896. for (int i = 0; i < size; i++) {
  1897. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1898. listener.taskFinished(event);
  1899. }
  1900. }
  1901. /**
  1902. * Sends a "message logged" event to the build listeners for this project.
  1903. *
  1904. * @param event The event to send. This should be built up with the
  1905. * appropriate task/target/project by the caller, so that
  1906. * this method can set the message and priority, then send
  1907. * the event. Must not be <code>null</code>.
  1908. * @param message The message to send. Should not be <code>null</code>.
  1909. * @param priority The priority of the message.
  1910. */
  1911. private void fireMessageLoggedEvent(BuildEvent event, String message,
  1912. int priority) {
  1913. event.setMessage(message, priority);
  1914. Vector listeners = getBuildListeners();
  1915. synchronized (this) {
  1916. if (loggingMessage) {
  1917. throw new BuildException("Listener attempted to access "
  1918. + (priority == MSG_ERR ? "System.err" : "System.out")
  1919. + " - infinite loop terminated");
  1920. }
  1921. loggingMessage = true;
  1922. int size = listeners.size();
  1923. for (int i = 0; i < size; i++) {
  1924. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1925. listener.messageLogged(event);
  1926. }
  1927. loggingMessage = false;
  1928. }
  1929. }
  1930. /**
  1931. * Sends a "message logged" project level event to the build listeners for
  1932. * this project.
  1933. *
  1934. * @param project The project generating the event.
  1935. * Should not be <code>null</code>.
  1936. * @param message The message to send. Should not be <code>null</code>.
  1937. * @param priority The priority of the message.
  1938. */
  1939. protected void fireMessageLogged(Project project, String message,
  1940. int priority) {
  1941. BuildEvent event = new BuildEvent(project);
  1942. fireMessageLoggedEvent(event, message, priority);
  1943. }
  1944. /**
  1945. * Sends a "message logged" target level event to the build listeners for
  1946. * this project.
  1947. *
  1948. * @param target The target generating the event.
  1949. * Must not be <code>null</code>.
  1950. * @param message The message to send. Should not be <code>null</code>.
  1951. * @param priority The priority of the message.
  1952. */
  1953. protected void fireMessageLogged(Target target, String message,
  1954. int priority) {
  1955. BuildEvent event = new BuildEvent(target);
  1956. fireMessageLoggedEvent(event, message, priority);
  1957. }
  1958. /**
  1959. * Sends a "message logged" task level event to the build listeners for
  1960. * this project.
  1961. *
  1962. * @param task The task generating the event.
  1963. * Must not be <code>null</code>.
  1964. * @param message The message to send. Should not be <code>null</code>.
  1965. * @param priority The priority of the message.
  1966. */
  1967. protected void fireMessageLogged(Task task, String message, int priority) {
  1968. BuildEvent event = new BuildEvent(task);
  1969. fireMessageLoggedEvent(event, message, priority);
  1970. }
  1971. /**
  1972. * Register a task as the current task for a thread.
  1973. * If the task is null, the thread's entry is removed.
  1974. *
  1975. * @param thread the thread on which the task is registered.
  1976. * @param task the task to be registered.
  1977. * @since Ant 1.5
  1978. */
  1979. public synchronized void registerThreadTask(Thread thread, Task task) {
  1980. if (task != null) {
  1981. threadTasks.put(thread, task);
  1982. threadGroupTasks.put(thread.getThreadGroup(), task);
  1983. } else {
  1984. threadTasks.remove(thread);
  1985. threadGroupTasks.remove(thread.getThreadGroup());
  1986. }
  1987. }
  1988. /**
  1989. * Get the current task assopciated with a thread, if any
  1990. *
  1991. * @param thread the thread for which the task is required.
  1992. * @return the task which is currently registered for the given thread or
  1993. * null if no task is registered.
  1994. */
  1995. public Task getThreadTask(Thread thread) {
  1996. Task task = (Task) threadTasks.get(thread);
  1997. if (task == null) {
  1998. ThreadGroup group = thread.getThreadGroup();
  1999. while (task == null && group != null) {
  2000. task = (Task) threadGroupTasks.get(group);
  2001. group = group.getParent();
  2002. }
  2003. }
  2004. return task;
  2005. }
  2006. // Should move to a separate public class - and have API to add
  2007. // listeners, etc.
  2008. private static class AntRefTable extends Hashtable {
  2009. private Project project;
  2010. public AntRefTable(Project project) {
  2011. super();
  2012. this.project = project;
  2013. }
  2014. /** Returns the unmodified original object.
  2015. * This method should be called internally to
  2016. * get the 'real' object.
  2017. * The normal get method will do the replacement
  2018. * of UnknownElement ( this is similar with the JDNI
  2019. * refs behavior )
  2020. */
  2021. public Object getReal(Object key) {
  2022. return super.get(key);
  2023. }
  2024. /** Get method for the reference table.
  2025. * It can be used to hook dynamic references and to modify
  2026. * some references on the fly - for example for delayed
  2027. * evaluation.
  2028. *
  2029. * It is important to make sure that the processing that is
  2030. * done inside is not calling get indirectly.
  2031. *
  2032. * @param key
  2033. * @return
  2034. */
  2035. public Object get(Object key) {
  2036. //System.out.println("AntRefTable.get " + key);
  2037. Object o = super.get(key);
  2038. if (o instanceof UnknownElement) {
  2039. // Make sure that
  2040. ((UnknownElement) o).maybeConfigure();
  2041. o = ((UnknownElement) o).getTask();
  2042. }
  2043. return o;
  2044. }
  2045. }
  2046. private static class AntTaskTable extends LazyHashtable {
  2047. private Project project;
  2048. private Properties props;
  2049. private boolean tasks = false;
  2050. public AntTaskTable(Project p, boolean tasks) {
  2051. this.project = p;
  2052. this.tasks = tasks;
  2053. }
  2054. public void addDefinitions(Properties props) {
  2055. this.props = props;
  2056. }
  2057. protected void initAll() {
  2058. if (initAllDone) {
  2059. return;
  2060. }
  2061. project.log("InitAll", Project.MSG_DEBUG);
  2062. if (props == null) {
  2063. return;
  2064. }
  2065. Enumeration enum = props.propertyNames();
  2066. while (enum.hasMoreElements()) {
  2067. String key = (String) enum.nextElement();
  2068. Class taskClass = getTask(key);
  2069. if (taskClass != null) {
  2070. // This will call a get() and a put()
  2071. if (tasks) {
  2072. project.addTaskDefinition(key, taskClass);
  2073. } else {
  2074. project.addDataTypeDefinition(key, taskClass);
  2075. }
  2076. }
  2077. }
  2078. initAllDone = true;
  2079. }
  2080. protected Class getTask(String key) {
  2081. if (props == null) {
  2082. return null; // for tasks loaded before init()
  2083. }
  2084. String value = props.getProperty(key);
  2085. if (value == null) {
  2086. //project.log( "No class name for " + key, Project.MSG_VERBOSE);
  2087. return null;
  2088. }
  2089. try {
  2090. Class taskClass = null;
  2091. if (project.getCoreLoader() != null &&
  2092. !("only".equals(project.getProperty("build.sysclasspath")))) {
  2093. try {
  2094. taskClass = project.getCoreLoader().loadClass(value);
  2095. if (taskClass != null) {
  2096. return taskClass;
  2097. }
  2098. } catch (Exception ex) {
  2099. // ignore
  2100. }
  2101. }
  2102. taskClass = Class.forName(value);
  2103. return taskClass;
  2104. } catch (NoClassDefFoundError ncdfe) {
  2105. project.log("Could not load a dependent class ("
  2106. + ncdfe.getMessage() + ") for task "
  2107. + key, Project.MSG_DEBUG);
  2108. } catch (ClassNotFoundException cnfe) {
  2109. project.log("Could not load class (" + value
  2110. + ") for task " + key, Project.MSG_DEBUG);
  2111. }
  2112. return null;
  2113. }
  2114. // Hashtable implementation
  2115. public Object get(Object key) {
  2116. Object orig = super.get(key);
  2117. if (orig != null) {
  2118. return orig;
  2119. }
  2120. if (!(key instanceof String)) {
  2121. return null;
  2122. }
  2123. project.log("Get task " + key, Project.MSG_DEBUG);
  2124. Object taskClass = getTask((String) key);
  2125. if (taskClass != null) {
  2126. super.put(key, taskClass);
  2127. }
  2128. return taskClass;
  2129. }
  2130. public boolean containsKey(Object key) {
  2131. return get(key) != null;
  2132. }
  2133. }
  2134. /**
  2135. * Set a reference to this Project on the parameterized object.
  2136. * Need to set the project before other set/add elements
  2137. * are called
  2138. * @param obj the object to invoke setProject(this) on
  2139. */
  2140. public final void setProjectReference( final Object obj ) {
  2141. if ( obj instanceof ProjectComponent ) {
  2142. ( (ProjectComponent) obj ).setProject( this );
  2143. return;
  2144. }
  2145. try {
  2146. Method method =
  2147. obj.getClass().getMethod(
  2148. "setProject", new Class[] {Project.class} );
  2149. if ( method != null ) {
  2150. method.invoke( obj, new Object[] { this } );
  2151. }
  2152. } catch (Throwable e) {
  2153. // ignore this if the object does not have
  2154. // a set project method or the method
  2155. // is private/protected.
  2156. }
  2157. }
  2158. }