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

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