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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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 "The Jakarta Project", "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.Task;
  57. import org.apache.tools.ant.AntClassLoader;
  58. import org.apache.tools.ant.BuildException;
  59. import org.apache.tools.ant.Project;
  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 <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  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(project);
  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(project);
  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 f) {
  174. this.file = FileUtils.newFileUtils()
  175. .removeLeadingPath(getProject().getBaseDir(), f);
  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. */
  191. public void setType(String type) {
  192. log("DEPRECATED - The setType(String) method has been deprecated."
  193. + " Use setType(Available.FileDir) instead.");
  194. this.type = new FileDir();
  195. this.type.setValue(type);
  196. }
  197. /**
  198. * Set what type of file is required - either directory or file.
  199. *
  200. * @param type an instance of the FileDir enumeratedAttribute indicating
  201. * whether the file required is to be a directory or a plain
  202. * file.
  203. */
  204. public void setType(FileDir type) {
  205. this.type = type;
  206. }
  207. /**
  208. * Set whether the search for classes should ignore the runtime classes and
  209. * just use the given classpath.
  210. *
  211. * @param ignore true if system classes are to be ignored.
  212. */
  213. public void setIgnoresystemclasses(boolean ignore) {
  214. this.ignoreSystemclasses = ignore;
  215. }
  216. /**
  217. * Entry point when operating as a task.
  218. *
  219. * @exception BuildException if the task is not configured correctly.
  220. */
  221. public void execute() throws BuildException {
  222. if (property == null) {
  223. throw new BuildException("property attribute is required",
  224. location);
  225. }
  226. isTask = true;
  227. try {
  228. if (eval()) {
  229. if (null != getProject().getProperty(property)) {
  230. log("DEPRECATED - <available> used to override an existing"
  231. + " property."
  232. + StringUtils.LINE_SEP
  233. + " Build file should not reuse the same property"
  234. + " name for different values.");
  235. }
  236. getProject().setProperty(property, value);
  237. }
  238. } finally {
  239. isTask = false;
  240. }
  241. }
  242. /**
  243. * Evaluate the availability of a resource.
  244. *
  245. * @return boolean is the resource is available.
  246. * @exception if the condition is not configured correctly
  247. */
  248. public boolean eval() throws BuildException {
  249. if (classname == null && file == null && resource == null) {
  250. throw new BuildException("At least one of (classname|file|"
  251. + "resource) is required", location);
  252. }
  253. if (type != null) {
  254. if (file == null) {
  255. throw new BuildException("The type attribute is only valid "
  256. + "when specifying the file "
  257. + "attribute.", location);
  258. }
  259. }
  260. if (classpath != null) {
  261. classpath.setProject(project);
  262. this.loader = new AntClassLoader(project, classpath);
  263. }
  264. String appendix = "";
  265. if (isTask) {
  266. appendix = " to set property " + property;
  267. } else {
  268. setTaskName("available");
  269. }
  270. if ((classname != null) && !checkClass(classname)) {
  271. log("Unable to load class " + classname + appendix,
  272. Project.MSG_VERBOSE);
  273. return false;
  274. }
  275. if ((file != null) && !checkFile()) {
  276. if (type != null) {
  277. log("Unable to find " + type + " " + file + appendix,
  278. Project.MSG_VERBOSE);
  279. } else {
  280. log("Unable to find " + file + appendix, Project.MSG_VERBOSE);
  281. }
  282. return false;
  283. }
  284. if ((resource != null) && !checkResource(resource)) {
  285. log("Unable to load resource " + resource + appendix,
  286. Project.MSG_VERBOSE);
  287. return false;
  288. }
  289. if (loader != null) {
  290. loader.cleanup();
  291. loader = null;
  292. }
  293. if (!isTask) {
  294. setTaskName(null);
  295. }
  296. return true;
  297. }
  298. /**
  299. * Search for file/directory either either relative to project's
  300. * basedir or in the path given as filepath.
  301. *
  302. * <p>filepath can be a list of directory and/or file names (gen'd
  303. * via <fileset>)</p>
  304. *
  305. * <p>look for:</p><ul>
  306. * <li>full-pathname specified == path in list</li>
  307. * <li>full-pathname specified == parent dir of path in list</li>
  308. * <li>simple name specified == path in list</li>
  309. * <li>simple name specified == path in list + name</li>
  310. * <li>simple name specified == parent dir + name</li>
  311. * <li>simple name specified == parent of parent dir + name</li>
  312. * </ul>
  313. */
  314. private boolean checkFile() {
  315. if (filepath == null) {
  316. return checkFile(project.resolveFile(file), file);
  317. } else {
  318. String[] paths = filepath.list();
  319. for (int i = 0; i < paths.length; ++i) {
  320. log("Searching " + paths[i], Project.MSG_DEBUG);
  321. File path = new File(paths[i]);
  322. // ** full-pathname specified == path in list
  323. // ** simple name specified == path in list
  324. if (path.exists() && file.equals(paths[i])) {
  325. if (type == null) {
  326. log("Found: " + path, Project.MSG_VERBOSE);
  327. return true;
  328. } else if (type.isDir()
  329. && path.isDirectory()) {
  330. log("Found directory: " + path, Project.MSG_VERBOSE);
  331. return true;
  332. } else if (type.isFile()
  333. && path.isFile()) {
  334. log("Found file: " + path, Project.MSG_VERBOSE);
  335. return true;
  336. }
  337. // not the requested type
  338. return false;
  339. }
  340. FileUtils fileUtils = FileUtils.newFileUtils();
  341. File parent = fileUtils.getParentFile(path);
  342. // ** full-pathname specified == parent dir of path in list
  343. if (parent != null && parent.exists()
  344. && file.equals(parent.getAbsolutePath())) {
  345. if (type == null) {
  346. log("Found: " + parent, Project.MSG_VERBOSE);
  347. return true;
  348. } else if (type.isDir()) {
  349. log("Found directory: " + parent, Project.MSG_VERBOSE);
  350. return true;
  351. }
  352. // not the requested type
  353. return false;
  354. }
  355. // ** simple name specified == path in list + name
  356. if (path.exists() && path.isDirectory()) {
  357. if (checkFile(new File(path, file),
  358. file + " in " + path)) {
  359. return true;
  360. }
  361. }
  362. // ** simple name specified == parent dir + name
  363. if (parent != null && parent.exists()) {
  364. if (checkFile(new File(parent, file),
  365. file + " in " + parent)) {
  366. return true;
  367. }
  368. }
  369. // ** simple name specified == parent of parent dir + name
  370. if (parent != null) {
  371. File grandParent = fileUtils.getParentFile(parent);
  372. if (grandParent != null && grandParent.exists()) {
  373. if (checkFile(new File(grandParent, file),
  374. file + " in " + grandParent)) {
  375. return true;
  376. }
  377. }
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. /**
  384. * Check if a given file exists and matches the required type.
  385. */
  386. private boolean checkFile(File f, String text) {
  387. if (type != null) {
  388. if (type.isDir()) {
  389. if (f.isDirectory()) {
  390. log("Found directory: " + text, Project.MSG_VERBOSE);
  391. }
  392. return f.isDirectory();
  393. } else if (type.isFile()) {
  394. if (f.isFile()) {
  395. log("Found file: " + text, Project.MSG_VERBOSE);
  396. }
  397. return f.isFile();
  398. }
  399. }
  400. if (f.exists()) {
  401. log("Found: " + text, Project.MSG_VERBOSE);
  402. }
  403. return f.exists();
  404. }
  405. /**
  406. * Check if a given resource can be loaded.
  407. */
  408. private boolean checkResource(String resource) {
  409. if (loader != null) {
  410. return (loader.getResourceAsStream(resource) != null);
  411. } else {
  412. ClassLoader cL = this.getClass().getClassLoader();
  413. if (cL != null) {
  414. return (cL.getResourceAsStream(resource) != null);
  415. } else {
  416. return
  417. (ClassLoader.getSystemResourceAsStream(resource) != null);
  418. }
  419. }
  420. }
  421. /**
  422. * Check if a given class can be loaded.
  423. */
  424. private boolean checkClass(String classname) {
  425. try {
  426. Class requiredClass = null;
  427. if (ignoreSystemclasses) {
  428. loader = new AntClassLoader(null, getProject(), classpath,
  429. false);
  430. if (loader != null) {
  431. try {
  432. requiredClass = loader.findClass(classname);
  433. } catch (SecurityException se) {
  434. // class found but restricted name; this is
  435. // actually the case we're looking for in JDK 1.3+,
  436. // so catch the exception and return
  437. return true;
  438. }
  439. } else {
  440. return false;
  441. }
  442. } else if (loader != null) {
  443. requiredClass = loader.loadClass(classname);
  444. } else {
  445. ClassLoader l = this.getClass().getClassLoader();
  446. // Can return null to represent the bootstrap class loader.
  447. // see API docs of Class.getClassLoader.
  448. if (l != null) {
  449. requiredClass = l.loadClass(classname);
  450. } else {
  451. requiredClass = Class.forName(classname);
  452. }
  453. }
  454. AntClassLoader.initializeClass(requiredClass);
  455. return true;
  456. } catch (ClassNotFoundException e) {
  457. log("class \"" + classname + "\" was not found",
  458. Project.MSG_DEBUG);
  459. return false;
  460. } catch (NoClassDefFoundError e) {
  461. log("Could not load dependent class \"" + e.getMessage()
  462. + "\" for class \"" + classname + "\"",
  463. Project.MSG_DEBUG);
  464. return false;
  465. }
  466. }
  467. /**
  468. * EnumeratedAttribute covering the file types to be checked for, either
  469. * file or dir.
  470. */
  471. public static class FileDir extends EnumeratedAttribute {
  472. private static final String[] values = {"file", "dir"};
  473. /**
  474. * @see EnumeratedAttribute#getValues
  475. */
  476. public String[] getValues() {
  477. return values;
  478. }
  479. /**
  480. * Indicate if the value specifies a directory.
  481. *
  482. * @return true if the value specifies a directory.
  483. */
  484. public boolean isDir() {
  485. return "dir".equalsIgnoreCase(getValue());
  486. }
  487. /**
  488. * Indicate if the value specifies a file.
  489. *
  490. * @return true if the value specifies a file.
  491. */
  492. public boolean isFile() {
  493. return "file".equalsIgnoreCase(getValue());
  494. }
  495. }
  496. }