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

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