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.

Available.java 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.File;
  56. import org.apache.tools.ant.AntClassLoader;
  57. import org.apache.tools.ant.BuildException;
  58. import org.apache.tools.ant.Project;
  59. import org.apache.tools.ant.Task;
  60. import org.apache.tools.ant.taskdefs.condition.Condition;
  61. import org.apache.tools.ant.types.EnumeratedAttribute;
  62. import org.apache.tools.ant.types.Path;
  63. import org.apache.tools.ant.types.Reference;
  64. import org.apache.tools.ant.util.FileUtils;
  65. import org.apache.tools.ant.util.StringUtils;
  66. /**
  67. * Will set the given property if the requested resource is available at
  68. * runtime. This task may also be used as a condition by the condition task.
  69. *
  70. * @author Stefano Mazzocchi
  71. * <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  72. * @author Magesh Umasankar
  73. *
  74. * @since Ant 1.1
  75. *
  76. * @ant.task category="control"
  77. */
  78. public class Available extends Task implements Condition {
  79. private String property;
  80. private String classname;
  81. private String file;
  82. private Path filepath;
  83. private String resource;
  84. private FileDir type;
  85. private Path classpath;
  86. private AntClassLoader loader;
  87. private String value = "true";
  88. private boolean isTask = false;
  89. private boolean ignoreSystemclasses = false;
  90. /**
  91. * Set the classpath to be used when searching for classes and resources.
  92. *
  93. * @param classpath an Ant Path object containing the search path.
  94. */
  95. public void setClasspath(Path classpath) {
  96. createClasspath().append(classpath);
  97. }
  98. /**
  99. * Classpath to be used when searching for classes and resources.
  100. *
  101. * @return an empty Path instance to be configured by Ant.
  102. */
  103. public Path createClasspath() {
  104. if (this.classpath == null) {
  105. this.classpath = new Path(getProject());
  106. }
  107. return this.classpath.createPath();
  108. }
  109. /**
  110. * Set the classpath by reference.
  111. *
  112. * @param r a Reference to a Path instance to be used as the classpath
  113. * value.
  114. */
  115. public void setClasspathRef(Reference r) {
  116. createClasspath().setRefid(r);
  117. }
  118. /**
  119. * Set the path to use when looking for a file.
  120. *
  121. * @param filepath a Path instance containing the search path for files.
  122. */
  123. public void setFilepath(Path filepath) {
  124. createFilepath().append(filepath);
  125. }
  126. /**
  127. * Path to search for file resources.
  128. *
  129. * @return a new Path instance which Ant will configure with a file search
  130. * path.
  131. */
  132. public Path createFilepath() {
  133. if (this.filepath == null) {
  134. this.filepath = new Path(getProject());
  135. }
  136. return this.filepath.createPath();
  137. }
  138. /**
  139. * Set the name of the property which will be set if the particular resource
  140. * is available.
  141. *
  142. * @param property the name of the property to set.
  143. */
  144. public void setProperty(String property) {
  145. this.property = property;
  146. }
  147. /**
  148. * Set the value to be given to the property if the desired resource is
  149. * available.
  150. *
  151. * @param value the value to be given.
  152. */
  153. public void setValue(String value) {
  154. this.value = value;
  155. }
  156. /**
  157. * Set a classname of a class which must be available to set the given
  158. * property.
  159. *
  160. * @param classname the name of the class required.
  161. */
  162. public void setClassname(String classname) {
  163. if (!"".equals(classname)) {
  164. this.classname = classname;
  165. }
  166. }
  167. /**
  168. * Set the file which must be present in the file system to set the given
  169. * property.
  170. *
  171. * @param file the name of the file which is required.
  172. */
  173. public void setFile(File file) {
  174. this.file = FileUtils.newFileUtils()
  175. .removeLeadingPath(getProject().getBaseDir(), file);
  176. }
  177. /**
  178. * Set the name of a Java resource which is required to set the property.
  179. *
  180. * @param resource the name of a resource which is required to be available.
  181. */
  182. public void setResource(String resource) {
  183. this.resource = resource;
  184. }
  185. /**
  186. * @deprecated setType(String) is deprecated and is replaced with
  187. * setType(Available.FileDir) to make Ant's Introspection
  188. * mechanism do the work and also to encapsulate operations on
  189. * the type in its own class.
  190. * @param type the type of resource
  191. */
  192. public void setType(String type) {
  193. log("DEPRECATED - The setType(String) method has been deprecated."
  194. + " Use setType(Available.FileDir) instead.");
  195. this.type = new FileDir();
  196. this.type.setValue(type);
  197. }
  198. /**
  199. * Set what type of file is required - either directory or file.
  200. *
  201. * @param type an instance of the FileDir enumeratedAttribute indicating
  202. * whether the file required is to be a directory or a plain
  203. * file.
  204. */
  205. public void setType(FileDir type) {
  206. this.type = type;
  207. }
  208. /**
  209. * Set whether the search for classes should ignore the runtime classes and
  210. * just use the given classpath.
  211. *
  212. * @param ignore true if system classes are to be ignored.
  213. */
  214. public void setIgnoresystemclasses(boolean ignore) {
  215. this.ignoreSystemclasses = ignore;
  216. }
  217. /**
  218. * Entry point when operating as a task.
  219. *
  220. * @exception BuildException if the task is not configured correctly.
  221. */
  222. public void execute() throws BuildException {
  223. if (property == null) {
  224. throw new BuildException("property attribute is required",
  225. getLocation());
  226. }
  227. isTask = true;
  228. try {
  229. if (eval()) {
  230. String oldvalue = getProject().getProperty(property);
  231. if (null != oldvalue && !oldvalue.equals(value)) {
  232. log("DEPRECATED - <available> used to override an existing"
  233. + " property."
  234. + StringUtils.LINE_SEP
  235. + " Build file should not reuse the same property"
  236. + " name for different values.");
  237. }
  238. getProject().setProperty(property, value);
  239. }
  240. } finally {
  241. isTask = false;
  242. }
  243. }
  244. /**
  245. * Evaluate the availability of a resource.
  246. *
  247. * @return boolean is the resource is available.
  248. * @exception BuildException if the condition is not configured correctly
  249. */
  250. public boolean eval() throws BuildException {
  251. if (classname == null && file == null && resource == null) {
  252. throw new BuildException("At least one of (classname|file|"
  253. + "resource) is required", getLocation());
  254. }
  255. if (type != null) {
  256. if (file == null) {
  257. throw new BuildException("The type attribute is only valid "
  258. + "when specifying the file "
  259. + "attribute.", getLocation());
  260. }
  261. }
  262. if (classpath != null) {
  263. classpath.setProject(getProject());
  264. this.loader = getProject().createClassLoader(classpath);
  265. }
  266. String appendix = "";
  267. if (isTask) {
  268. appendix = " to set property " + property;
  269. } else {
  270. setTaskName("available");
  271. }
  272. if ((classname != null) && !checkClass(classname)) {
  273. log("Unable to load class " + classname + appendix,
  274. Project.MSG_VERBOSE);
  275. return false;
  276. }
  277. if ((file != null) && !checkFile()) {
  278. if (type != null) {
  279. log("Unable to find " + type + " " + file + appendix,
  280. Project.MSG_VERBOSE);
  281. } else {
  282. log("Unable to find " + file + appendix, Project.MSG_VERBOSE);
  283. }
  284. return false;
  285. }
  286. if ((resource != null) && !checkResource(resource)) {
  287. log("Unable to load resource " + resource + appendix,
  288. Project.MSG_VERBOSE);
  289. return false;
  290. }
  291. if (loader != null) {
  292. loader.cleanup();
  293. loader = null;
  294. }
  295. if (!isTask) {
  296. setTaskName(null);
  297. }
  298. return true;
  299. }
  300. /**
  301. * Search for file/directory either either relative to project's
  302. * basedir or in the path given as filepath.
  303. *
  304. * <p>filepath can be a list of directory and/or file names (gen'd
  305. * via <fileset>)</p>
  306. *
  307. * <p>look for:</p><ul>
  308. * <li>full-pathname specified == path in list</li>
  309. * <li>full-pathname specified == parent dir of path in list</li>
  310. * <li>simple name specified == path in list</li>
  311. * <li>simple name specified == path in list + name</li>
  312. * <li>simple name specified == parent dir + name</li>
  313. * <li>simple name specified == parent of parent dir + name</li>
  314. * </ul>
  315. */
  316. private boolean checkFile() {
  317. if (filepath == null) {
  318. return checkFile(getProject().resolveFile(file), file);
  319. } else {
  320. String[] paths = filepath.list();
  321. for (int i = 0; i < paths.length; ++i) {
  322. log("Searching " + paths[i], Project.MSG_DEBUG);
  323. File path = new File(paths[i]);
  324. // ** full-pathname specified == path in list
  325. // ** simple name specified == path in list
  326. if (path.exists() && file.equals(paths[i])) {
  327. if (type == null) {
  328. log("Found: " + path, Project.MSG_VERBOSE);
  329. return true;
  330. } else if (type.isDir()
  331. && path.isDirectory()) {
  332. log("Found directory: " + path, Project.MSG_VERBOSE);
  333. return true;
  334. } else if (type.isFile()
  335. && path.isFile()) {
  336. log("Found file: " + path, Project.MSG_VERBOSE);
  337. return true;
  338. }
  339. // not the requested type
  340. return false;
  341. }
  342. FileUtils fileUtils = FileUtils.newFileUtils();
  343. File parent = fileUtils.getParentFile(path);
  344. // ** full-pathname specified == parent dir of path in list
  345. if (parent != null && parent.exists()
  346. && file.equals(parent.getAbsolutePath())) {
  347. if (type == null) {
  348. log("Found: " + parent, Project.MSG_VERBOSE);
  349. return true;
  350. } else if (type.isDir()) {
  351. log("Found directory: " + parent, Project.MSG_VERBOSE);
  352. return true;
  353. }
  354. // not the requested type
  355. return false;
  356. }
  357. // ** simple name specified == path in list + name
  358. if (path.exists() && path.isDirectory()) {
  359. if (checkFile(new File(path, file),
  360. file + " in " + path)) {
  361. return true;
  362. }
  363. }
  364. // ** simple name specified == parent dir + name
  365. if (parent != null && parent.exists()) {
  366. if (checkFile(new File(parent, file),
  367. file + " in " + parent)) {
  368. return true;
  369. }
  370. }
  371. // ** simple name specified == parent of parent dir + name
  372. if (parent != null) {
  373. File grandParent = fileUtils.getParentFile(parent);
  374. if (grandParent != null && grandParent.exists()) {
  375. if (checkFile(new File(grandParent, file),
  376. file + " in " + grandParent)) {
  377. return true;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. return false;
  384. }
  385. /**
  386. * Check if a given file exists and matches the required type.
  387. */
  388. private boolean checkFile(File f, String text) {
  389. if (type != null) {
  390. if (type.isDir()) {
  391. if (f.isDirectory()) {
  392. log("Found directory: " + text, Project.MSG_VERBOSE);
  393. }
  394. return f.isDirectory();
  395. } else if (type.isFile()) {
  396. if (f.isFile()) {
  397. log("Found file: " + text, Project.MSG_VERBOSE);
  398. }
  399. return f.isFile();
  400. }
  401. }
  402. if (f.exists()) {
  403. log("Found: " + text, Project.MSG_VERBOSE);
  404. }
  405. return f.exists();
  406. }
  407. /**
  408. * Check if a given resource can be loaded.
  409. */
  410. private boolean checkResource(String resource) {
  411. if (loader != null) {
  412. return (loader.getResourceAsStream(resource) != null);
  413. } else {
  414. ClassLoader cL = this.getClass().getClassLoader();
  415. if (cL != null) {
  416. return (cL.getResourceAsStream(resource) != null);
  417. } else {
  418. return
  419. (ClassLoader.getSystemResourceAsStream(resource) != null);
  420. }
  421. }
  422. }
  423. /**
  424. * Check if a given class can be loaded.
  425. */
  426. private boolean checkClass(String classname) {
  427. try {
  428. Class requiredClass = null;
  429. if (ignoreSystemclasses) {
  430. loader = getProject().createClassLoader(classpath);
  431. loader.setParentFirst(false);
  432. loader.addJavaLibraries();
  433. if (loader != null) {
  434. try {
  435. requiredClass = loader.findClass(classname);
  436. } catch (SecurityException se) {
  437. // class found but restricted name; this is
  438. // actually the case we're looking for in JDK 1.3+,
  439. // so catch the exception and return
  440. return true;
  441. }
  442. } else {
  443. return false;
  444. }
  445. } else if (loader != null) {
  446. requiredClass = loader.loadClass(classname);
  447. } else {
  448. ClassLoader l = this.getClass().getClassLoader();
  449. // Can return null to represent the bootstrap class loader.
  450. // see API docs of Class.getClassLoader.
  451. if (l != null) {
  452. requiredClass = Class.forName(classname, true, l);
  453. } else {
  454. requiredClass = Class.forName(classname);
  455. }
  456. }
  457. return true;
  458. } catch (ClassNotFoundException e) {
  459. log("class \"" + classname + "\" was not found",
  460. Project.MSG_DEBUG);
  461. return false;
  462. } catch (NoClassDefFoundError e) {
  463. log("Could not load dependent class \"" + e.getMessage()
  464. + "\" for class \"" + classname + "\"",
  465. Project.MSG_DEBUG);
  466. return false;
  467. }
  468. }
  469. /**
  470. * EnumeratedAttribute covering the file types to be checked for, either
  471. * file or dir.
  472. */
  473. public static class FileDir extends EnumeratedAttribute {
  474. private static final String[] VALUES = {"file", "dir"};
  475. /**
  476. * @see EnumeratedAttribute#getValues
  477. */
  478. public String[] getValues() {
  479. return VALUES;
  480. }
  481. /**
  482. * Indicate if the value specifies a directory.
  483. *
  484. * @return true if the value specifies a directory.
  485. */
  486. public boolean isDir() {
  487. return "dir".equalsIgnoreCase(getValue());
  488. }
  489. /**
  490. * Indicate if the value specifies a file.
  491. *
  492. * @return true if the value specifies a file.
  493. */
  494. public boolean isFile() {
  495. return "file".equalsIgnoreCase(getValue());
  496. }
  497. }
  498. }