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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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. for (int i = 0; i < listeners.size(); i++) {
  1805. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1806. listener.buildStarted(event);
  1807. }
  1808. }
  1809. /**
  1810. * Sends a "build finished" event to the build listeners for this project.
  1811. * @param exception an exception indicating a reason for a build
  1812. * failure. May be <code>null</code>, indicating
  1813. * a successful build.
  1814. */
  1815. public void fireBuildFinished(Throwable exception) {
  1816. BuildEvent event = new BuildEvent(this);
  1817. event.setException(exception);
  1818. Vector listeners = getBuildListeners();
  1819. for (int i = 0; i < listeners.size(); i++) {
  1820. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1821. listener.buildFinished(event);
  1822. }
  1823. }
  1824. /**
  1825. * Sends a "target started" event to the build listeners for this project.
  1826. *
  1827. * @param target The target which is starting to build.
  1828. * Must not be <code>null</code>.
  1829. */
  1830. protected void fireTargetStarted(Target target) {
  1831. BuildEvent event = new BuildEvent(target);
  1832. Vector listeners = getBuildListeners();
  1833. for (int i = 0; i < listeners.size(); i++) {
  1834. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1835. listener.targetStarted(event);
  1836. }
  1837. }
  1838. /**
  1839. * Sends a "target finished" event to the build listeners for this
  1840. * project.
  1841. *
  1842. * @param target The target which has finished building.
  1843. * Must not be <code>null</code>.
  1844. * @param exception an exception indicating a reason for a build
  1845. * failure. May be <code>null</code>, indicating
  1846. * a successful build.
  1847. */
  1848. protected void fireTargetFinished(Target target, Throwable exception) {
  1849. BuildEvent event = new BuildEvent(target);
  1850. event.setException(exception);
  1851. Vector listeners = getBuildListeners();
  1852. for (int i = 0; i < listeners.size(); i++) {
  1853. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1854. listener.targetFinished(event);
  1855. }
  1856. }
  1857. /**
  1858. * Sends a "task started" event to the build listeners for this project.
  1859. *
  1860. * @param task The target which is starting to execute.
  1861. * Must not be <code>null</code>.
  1862. */
  1863. protected void fireTaskStarted(Task task) {
  1864. // register this as the current task on the current thread.
  1865. registerThreadTask(Thread.currentThread(), task);
  1866. BuildEvent event = new BuildEvent(task);
  1867. Vector listeners = getBuildListeners();
  1868. for (int i = 0; i < listeners.size(); i++) {
  1869. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1870. listener.taskStarted(event);
  1871. }
  1872. }
  1873. /**
  1874. * Sends a "task finished" event to the build listeners for this
  1875. * project.
  1876. *
  1877. * @param task The task which has finished executing.
  1878. * Must not be <code>null</code>.
  1879. * @param exception an exception indicating a reason for a build
  1880. * failure. May be <code>null</code>, indicating
  1881. * a successful build.
  1882. */
  1883. protected void fireTaskFinished(Task task, Throwable exception) {
  1884. registerThreadTask(Thread.currentThread(), null);
  1885. System.out.flush();
  1886. System.err.flush();
  1887. BuildEvent event = new BuildEvent(task);
  1888. event.setException(exception);
  1889. Vector listeners = getBuildListeners();
  1890. for (int i = 0; i < listeners.size(); i++) {
  1891. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1892. listener.taskFinished(event);
  1893. }
  1894. }
  1895. /**
  1896. * Sends a "message logged" event to the build listeners for this project.
  1897. *
  1898. * @param event The event to send. This should be built up with the
  1899. * appropriate task/target/project by the caller, so that
  1900. * this method can set the message and priority, then send
  1901. * the event. Must not be <code>null</code>.
  1902. * @param message The message to send. Should not be <code>null</code>.
  1903. * @param priority The priority of the message.
  1904. */
  1905. private void fireMessageLoggedEvent(BuildEvent event, String message,
  1906. int priority) {
  1907. event.setMessage(message, priority);
  1908. Vector listeners = getBuildListeners();
  1909. synchronized (this) {
  1910. if (loggingMessage) {
  1911. throw new BuildException("Listener attempted to access "
  1912. + (priority == MSG_ERR ? "System.err" : "System.out")
  1913. + " - infinite loop terminated");
  1914. }
  1915. loggingMessage = true;
  1916. for (int i = 0; i < listeners.size(); i++) {
  1917. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1918. listener.messageLogged(event);
  1919. }
  1920. loggingMessage = false;
  1921. }
  1922. }
  1923. /**
  1924. * Sends a "message logged" project level event to the build listeners for
  1925. * this project.
  1926. *
  1927. * @param project The project generating the event.
  1928. * Should not be <code>null</code>.
  1929. * @param message The message to send. Should not be <code>null</code>.
  1930. * @param priority The priority of the message.
  1931. */
  1932. protected void fireMessageLogged(Project project, String message,
  1933. int priority) {
  1934. BuildEvent event = new BuildEvent(project);
  1935. fireMessageLoggedEvent(event, message, priority);
  1936. }
  1937. /**
  1938. * Sends a "message logged" target level event to the build listeners for
  1939. * this project.
  1940. *
  1941. * @param target The target generating the event.
  1942. * Must not be <code>null</code>.
  1943. * @param message The message to send. Should not be <code>null</code>.
  1944. * @param priority The priority of the message.
  1945. */
  1946. protected void fireMessageLogged(Target target, String message,
  1947. int priority) {
  1948. BuildEvent event = new BuildEvent(target);
  1949. fireMessageLoggedEvent(event, message, priority);
  1950. }
  1951. /**
  1952. * Sends a "message logged" task level event to the build listeners for
  1953. * this project.
  1954. *
  1955. * @param task The task generating the event.
  1956. * Must not be <code>null</code>.
  1957. * @param message The message to send. Should not be <code>null</code>.
  1958. * @param priority The priority of the message.
  1959. */
  1960. protected void fireMessageLogged(Task task, String message, int priority) {
  1961. BuildEvent event = new BuildEvent(task);
  1962. fireMessageLoggedEvent(event, message, priority);
  1963. }
  1964. /**
  1965. * Register a task as the current task for a thread.
  1966. * If the task is null, the thread's entry is removed.
  1967. *
  1968. * @param thread the thread on which the task is registered.
  1969. * @param task the task to be registered.
  1970. * @since Ant 1.5
  1971. */
  1972. public synchronized void registerThreadTask(Thread thread, Task task) {
  1973. if (task != null) {
  1974. threadTasks.put(thread, task);
  1975. threadGroupTasks.put(thread.getThreadGroup(), task);
  1976. } else {
  1977. threadTasks.remove(thread);
  1978. threadGroupTasks.remove(thread.getThreadGroup());
  1979. }
  1980. }
  1981. /**
  1982. * Get the current task assopciated with a thread, if any
  1983. *
  1984. * @param thread the thread for which the task is required.
  1985. * @return the task which is currently registered for the given thread or
  1986. * null if no task is registered.
  1987. */
  1988. public Task getThreadTask(Thread thread) {
  1989. Task task = (Task) threadTasks.get(thread);
  1990. if (task == null) {
  1991. ThreadGroup group = thread.getThreadGroup();
  1992. while (task == null && group != null) {
  1993. task = (Task) threadGroupTasks.get(group);
  1994. group = group.getParent();
  1995. }
  1996. }
  1997. return task;
  1998. }
  1999. // Should move to a separate public class - and have API to add
  2000. // listeners, etc.
  2001. private static class AntRefTable extends Hashtable {
  2002. private Project project;
  2003. public AntRefTable(Project project) {
  2004. super();
  2005. this.project = project;
  2006. }
  2007. /** Returns the unmodified original object.
  2008. * This method should be called internally to
  2009. * get the 'real' object.
  2010. * The normal get method will do the replacement
  2011. * of UnknownElement ( this is similar with the JDNI
  2012. * refs behavior )
  2013. */
  2014. public Object getReal(Object key) {
  2015. return super.get(key);
  2016. }
  2017. /** Get method for the reference table.
  2018. * It can be used to hook dynamic references and to modify
  2019. * some references on the fly - for example for delayed
  2020. * evaluation.
  2021. *
  2022. * It is important to make sure that the processing that is
  2023. * done inside is not calling get indirectly.
  2024. *
  2025. * @param key
  2026. * @return
  2027. */
  2028. public Object get(Object key) {
  2029. //System.out.println("AntRefTable.get " + key);
  2030. Object o = super.get(key);
  2031. if (o instanceof UnknownElement) {
  2032. // Make sure that
  2033. ((UnknownElement) o).maybeConfigure();
  2034. o = ((UnknownElement) o).getTask();
  2035. }
  2036. return o;
  2037. }
  2038. }
  2039. private static class AntTaskTable extends LazyHashtable {
  2040. private Project project;
  2041. private Properties props;
  2042. private boolean tasks = false;
  2043. public AntTaskTable(Project p, boolean tasks) {
  2044. this.project = p;
  2045. this.tasks = tasks;
  2046. }
  2047. public void addDefinitions(Properties props) {
  2048. this.props = props;
  2049. }
  2050. protected void initAll() {
  2051. if (initAllDone) {
  2052. return;
  2053. }
  2054. project.log("InitAll", Project.MSG_DEBUG);
  2055. if (props == null) {
  2056. return;
  2057. }
  2058. Enumeration enum = props.propertyNames();
  2059. while (enum.hasMoreElements()) {
  2060. String key = (String) enum.nextElement();
  2061. Class taskClass = getTask(key);
  2062. if (taskClass != null) {
  2063. // This will call a get() and a put()
  2064. if (tasks) {
  2065. project.addTaskDefinition(key, taskClass);
  2066. } else {
  2067. project.addDataTypeDefinition(key, taskClass);
  2068. }
  2069. }
  2070. }
  2071. initAllDone = true;
  2072. }
  2073. protected Class getTask(String key) {
  2074. if (props == null) {
  2075. return null; // for tasks loaded before init()
  2076. }
  2077. String value = props.getProperty(key);
  2078. if (value == null) {
  2079. //project.log( "No class name for " + key, Project.MSG_VERBOSE);
  2080. return null;
  2081. }
  2082. try {
  2083. Class taskClass = null;
  2084. if (project.getCoreLoader() != null &&
  2085. !("only".equals(project.getProperty("build.sysclasspath")))) {
  2086. try {
  2087. taskClass = project.getCoreLoader().loadClass(value);
  2088. if (taskClass != null) {
  2089. return taskClass;
  2090. }
  2091. } catch (Exception ex) {
  2092. // ignore
  2093. }
  2094. }
  2095. taskClass = Class.forName(value);
  2096. return taskClass;
  2097. } catch (NoClassDefFoundError ncdfe) {
  2098. project.log("Could not load a dependent class ("
  2099. + ncdfe.getMessage() + ") for task "
  2100. + key, Project.MSG_DEBUG);
  2101. } catch (ClassNotFoundException cnfe) {
  2102. project.log("Could not load class (" + value
  2103. + ") for task " + key, Project.MSG_DEBUG);
  2104. }
  2105. return null;
  2106. }
  2107. // Hashtable implementation
  2108. public Object get(Object key) {
  2109. Object orig = super.get(key);
  2110. if (orig != null) {
  2111. return orig;
  2112. }
  2113. if (!(key instanceof String)) {
  2114. return null;
  2115. }
  2116. project.log("Get task " + key, Project.MSG_DEBUG);
  2117. Object taskClass = getTask((String) key);
  2118. if (taskClass != null) {
  2119. super.put(key, taskClass);
  2120. }
  2121. return taskClass;
  2122. }
  2123. public boolean containsKey(Object key) {
  2124. return get(key) != null;
  2125. }
  2126. }
  2127. /**
  2128. * Set a reference to this Project on the parameterized object.
  2129. * Need to set the project before other set/add elements
  2130. * are called
  2131. * @param obj the object to invoke setProject(this) on
  2132. */
  2133. public final void setProjectReference( final Object obj ) {
  2134. if ( obj instanceof ProjectComponent ) {
  2135. ( (ProjectComponent) obj ).setProject( this );
  2136. return;
  2137. }
  2138. try {
  2139. Method method =
  2140. obj.getClass().getMethod(
  2141. "setProject", new Class[] {Project.class} );
  2142. if ( method != null ) {
  2143. method.invoke( obj, new Object[] { this } );
  2144. }
  2145. } catch (Throwable e) {
  2146. // ignore this if the object does not have
  2147. // a set project method or the method
  2148. // is private/protected.
  2149. }
  2150. }
  2151. }