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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import org.apache.tools.ant.AntClassLoader;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.Task;
  23. import org.apache.tools.ant.taskdefs.condition.Condition;
  24. import org.apache.tools.ant.types.EnumeratedAttribute;
  25. import org.apache.tools.ant.types.Path;
  26. import org.apache.tools.ant.types.Reference;
  27. import org.apache.tools.ant.util.FileUtils;
  28. import org.apache.tools.ant.util.StringUtils;
  29. /**
  30. * Will set the given property if the requested resource is available at
  31. * runtime. This task may also be used as a condition by the condition task.
  32. *
  33. * @since Ant 1.1
  34. *
  35. * @ant.task category="control"
  36. */
  37. public class Available extends Task implements Condition {
  38. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  39. private String property;
  40. private String classname;
  41. private String file;
  42. private Path filepath;
  43. private String resource;
  44. private FileDir type;
  45. private Path classpath;
  46. private AntClassLoader loader;
  47. private String value = "true";
  48. private boolean isTask = false;
  49. private boolean ignoreSystemclasses = false;
  50. /**
  51. * Set the classpath to be used when searching for classes and resources.
  52. *
  53. * @param classpath an Ant Path object containing the search path.
  54. */
  55. public void setClasspath(Path classpath) {
  56. createClasspath().append(classpath);
  57. }
  58. /**
  59. * Classpath to be used when searching for classes and resources.
  60. *
  61. * @return an empty Path instance to be configured by Ant.
  62. */
  63. public Path createClasspath() {
  64. if (this.classpath == null) {
  65. this.classpath = new Path(getProject());
  66. }
  67. return this.classpath.createPath();
  68. }
  69. /**
  70. * Set the classpath by reference.
  71. *
  72. * @param r a Reference to a Path instance to be used as the classpath
  73. * value.
  74. */
  75. public void setClasspathRef(Reference r) {
  76. createClasspath().setRefid(r);
  77. }
  78. /**
  79. * Set the path to use when looking for a file.
  80. *
  81. * @param filepath a Path instance containing the search path for files.
  82. */
  83. public void setFilepath(Path filepath) {
  84. createFilepath().append(filepath);
  85. }
  86. /**
  87. * Path to search for file resources.
  88. *
  89. * @return a new Path instance which Ant will configure with a file search
  90. * path.
  91. */
  92. public Path createFilepath() {
  93. if (this.filepath == null) {
  94. this.filepath = new Path(getProject());
  95. }
  96. return this.filepath.createPath();
  97. }
  98. /**
  99. * Set the name of the property which will be set if the particular resource
  100. * is available.
  101. *
  102. * @param property the name of the property to set.
  103. */
  104. public void setProperty(String property) {
  105. this.property = property;
  106. }
  107. /**
  108. * Set the value to be given to the property if the desired resource is
  109. * available.
  110. *
  111. * @param value the value to be given.
  112. */
  113. public void setValue(String value) {
  114. this.value = value;
  115. }
  116. /**
  117. * Set a classname of a class which must be available to set the given
  118. * property.
  119. *
  120. * @param classname the name of the class required.
  121. */
  122. public void setClassname(String classname) {
  123. if (!"".equals(classname)) {
  124. this.classname = classname;
  125. }
  126. }
  127. /**
  128. * Set the file which must be present in the file system to set the given
  129. * property.
  130. *
  131. * @param file the name of the file which is required.
  132. */
  133. public void setFile(File file) {
  134. this.file = FILE_UTILS.removeLeadingPath(getProject().getBaseDir(), file);
  135. }
  136. /**
  137. * Set the name of a Java resource which is required to set the property.
  138. *
  139. * @param resource the name of a resource which is required to be available.
  140. */
  141. public void setResource(String resource) {
  142. this.resource = resource;
  143. }
  144. /**
  145. * @deprecated setType(String) is deprecated and is replaced with
  146. * setType(Available.FileDir) to make Ant's Introspection
  147. * mechanism do the work and also to encapsulate operations on
  148. * the type in its own class.
  149. * @param type the type of resource
  150. */
  151. public void setType(String type) {
  152. log("DEPRECATED - The setType(String) method has been deprecated."
  153. + " Use setType(Available.FileDir) instead.");
  154. this.type = new FileDir();
  155. this.type.setValue(type);
  156. }
  157. /**
  158. * Set what type of file is required - either directory or file.
  159. *
  160. * @param type an instance of the FileDir enumeratedAttribute indicating
  161. * whether the file required is to be a directory or a plain
  162. * file.
  163. */
  164. public void setType(FileDir type) {
  165. this.type = type;
  166. }
  167. /**
  168. * Set whether the search for classes should ignore the runtime classes and
  169. * just use the given classpath.
  170. *
  171. * @param ignore true if system classes are to be ignored.
  172. */
  173. public void setIgnoresystemclasses(boolean ignore) {
  174. this.ignoreSystemclasses = ignore;
  175. }
  176. /**
  177. * Entry point when operating as a task.
  178. *
  179. * @exception BuildException if the task is not configured correctly.
  180. */
  181. public void execute() throws BuildException {
  182. if (property == null) {
  183. throw new BuildException("property attribute is required",
  184. getLocation());
  185. }
  186. isTask = true;
  187. try {
  188. if (eval()) {
  189. String oldvalue = getProject().getProperty(property);
  190. if (null != oldvalue && !oldvalue.equals(value)) {
  191. log("DEPRECATED - <available> used to override an existing"
  192. + " property."
  193. + StringUtils.LINE_SEP
  194. + " Build file should not reuse the same property"
  195. + " name for different values.");
  196. }
  197. // NB: this makes use of Project#setProperty rather than Project#setNewProperty
  198. // due to backwards compatiblity reasons
  199. getProject().setProperty(property, value);
  200. }
  201. } finally {
  202. isTask = false;
  203. }
  204. }
  205. /**
  206. * Evaluate the availability of a resource.
  207. *
  208. * @return boolean is the resource is available.
  209. * @exception BuildException if the condition is not configured correctly
  210. */
  211. public boolean eval() throws BuildException {
  212. if (classname == null && file == null && resource == null) {
  213. throw new BuildException("At least one of (classname|file|"
  214. + "resource) is required", getLocation());
  215. }
  216. if (type != null) {
  217. if (file == null) {
  218. throw new BuildException("The type attribute is only valid "
  219. + "when specifying the file "
  220. + "attribute.", getLocation());
  221. }
  222. }
  223. if (classpath != null) {
  224. classpath.setProject(getProject());
  225. this.loader = getProject().createClassLoader(classpath);
  226. }
  227. String appendix = "";
  228. if (isTask) {
  229. appendix = " to set property " + property;
  230. } else {
  231. setTaskName("available");
  232. }
  233. if ((classname != null) && !checkClass(classname)) {
  234. log("Unable to load class " + classname + appendix,
  235. Project.MSG_VERBOSE);
  236. return false;
  237. }
  238. if ((file != null) && !checkFile()) {
  239. if (type != null) {
  240. log("Unable to find " + type + " " + file + appendix,
  241. Project.MSG_VERBOSE);
  242. } else {
  243. log("Unable to find " + file + appendix, Project.MSG_VERBOSE);
  244. }
  245. return false;
  246. }
  247. if ((resource != null) && !checkResource(resource)) {
  248. log("Unable to load resource " + resource + appendix,
  249. Project.MSG_VERBOSE);
  250. return false;
  251. }
  252. if (loader != null) {
  253. loader.cleanup();
  254. loader = null;
  255. }
  256. if (!isTask) {
  257. setTaskName(null);
  258. }
  259. return true;
  260. }
  261. /**
  262. * Search for file/directory either relative to project's
  263. * basedir or in the path given as filepath.
  264. *
  265. * <p>filepath can be a list of directory and/or file names (gen'd
  266. * via <fileset>)</p>
  267. *
  268. * <p>look for:</p><ul>
  269. * <li>full-pathname specified == path in list</li>
  270. * <li>full-pathname specified == parent dir of path in list</li>
  271. * <li>simple name specified == path in list</li>
  272. * <li>simple name specified == path in list + name</li>
  273. * <li>simple name specified == parent dir + name</li>
  274. * <li>simple name specified == parent of parent dir + name</li>
  275. * </ul>
  276. */
  277. private boolean checkFile() {
  278. if (filepath == null) {
  279. return checkFile(getProject().resolveFile(file), file);
  280. } else {
  281. String[] paths = filepath.list();
  282. for (int i = 0; i < paths.length; ++i) {
  283. log("Searching " + paths[i], Project.MSG_DEBUG);
  284. File path = new File(paths[i]);
  285. // ** full-pathname specified == path in list
  286. // ** simple name specified == path in list
  287. if (path.exists() && file.equals(paths[i])) {
  288. if (type == null) {
  289. log("Found: " + path, Project.MSG_VERBOSE);
  290. return true;
  291. } else if (type.isDir()
  292. && path.isDirectory()) {
  293. log("Found directory: " + path, Project.MSG_VERBOSE);
  294. return true;
  295. } else if (type.isFile()
  296. && path.isFile()) {
  297. log("Found file: " + path, Project.MSG_VERBOSE);
  298. return true;
  299. }
  300. // not the requested type
  301. return false;
  302. }
  303. File parent = path.getParentFile();
  304. // ** full-pathname specified == parent dir of path in list
  305. if (parent != null && parent.exists()
  306. && file.equals(parent.getAbsolutePath())) {
  307. if (type == null) {
  308. log("Found: " + parent, Project.MSG_VERBOSE);
  309. return true;
  310. } else if (type.isDir()) {
  311. log("Found directory: " + parent, Project.MSG_VERBOSE);
  312. return true;
  313. }
  314. // not the requested type
  315. return false;
  316. }
  317. // ** simple name specified == path in list + name
  318. if (path.exists() && path.isDirectory()) {
  319. if (checkFile(new File(path, file),
  320. file + " in " + path)) {
  321. return true;
  322. }
  323. }
  324. // ** simple name specified == parent dir + name
  325. if (parent != null && parent.exists()) {
  326. if (checkFile(new File(parent, file),
  327. file + " in " + parent)) {
  328. return true;
  329. }
  330. }
  331. // ** simple name specified == parent of parent dir + name
  332. if (parent != null) {
  333. File grandParent = parent.getParentFile();
  334. if (grandParent != null && grandParent.exists()) {
  335. if (checkFile(new File(grandParent, file),
  336. file + " in " + grandParent)) {
  337. return true;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. return false;
  344. }
  345. /**
  346. * Check if a given file exists and matches the required type.
  347. */
  348. private boolean checkFile(File f, String text) {
  349. if (type != null) {
  350. if (type.isDir()) {
  351. if (f.isDirectory()) {
  352. log("Found directory: " + text, Project.MSG_VERBOSE);
  353. }
  354. return f.isDirectory();
  355. } else if (type.isFile()) {
  356. if (f.isFile()) {
  357. log("Found file: " + text, Project.MSG_VERBOSE);
  358. }
  359. return f.isFile();
  360. }
  361. }
  362. if (f.exists()) {
  363. log("Found: " + text, Project.MSG_VERBOSE);
  364. }
  365. return f.exists();
  366. }
  367. /**
  368. * Check if a given resource can be loaded.
  369. */
  370. private boolean checkResource(String resource) {
  371. if (loader != null) {
  372. return (loader.getResourceAsStream(resource) != null);
  373. } else {
  374. ClassLoader cL = this.getClass().getClassLoader();
  375. if (cL != null) {
  376. return (cL.getResourceAsStream(resource) != null);
  377. } else {
  378. return
  379. (ClassLoader.getSystemResourceAsStream(resource) != null);
  380. }
  381. }
  382. }
  383. /**
  384. * Check if a given class can be loaded.
  385. */
  386. private boolean checkClass(String classname) {
  387. try {
  388. Class requiredClass = null;
  389. if (ignoreSystemclasses) {
  390. loader = getProject().createClassLoader(classpath);
  391. loader.setParentFirst(false);
  392. loader.addJavaLibraries();
  393. if (loader != null) {
  394. try {
  395. requiredClass = loader.findClass(classname);
  396. } catch (SecurityException se) {
  397. // class found but restricted name; this is
  398. // actually the case we're looking for in JDK 1.3+,
  399. // so catch the exception and return
  400. return true;
  401. }
  402. } else {
  403. return false;
  404. }
  405. } else if (loader != null) {
  406. requiredClass = loader.loadClass(classname);
  407. } else {
  408. ClassLoader l = this.getClass().getClassLoader();
  409. // Can return null to represent the bootstrap class loader.
  410. // see API docs of Class.getClassLoader.
  411. if (l != null) {
  412. requiredClass = Class.forName(classname, true, l);
  413. } else {
  414. requiredClass = Class.forName(classname);
  415. }
  416. }
  417. return true;
  418. } catch (ClassNotFoundException e) {
  419. log("class \"" + classname + "\" was not found",
  420. Project.MSG_DEBUG);
  421. return false;
  422. } catch (NoClassDefFoundError e) {
  423. log("Could not load dependent class \"" + e.getMessage()
  424. + "\" for class \"" + classname + "\"",
  425. Project.MSG_DEBUG);
  426. return false;
  427. }
  428. }
  429. /**
  430. * EnumeratedAttribute covering the file types to be checked for, either
  431. * file or dir.
  432. */
  433. public static class FileDir extends EnumeratedAttribute {
  434. private static final String[] VALUES = {"file", "dir"};
  435. /**
  436. * @see EnumeratedAttribute#getValues
  437. */
  438. public String[] getValues() {
  439. return VALUES;
  440. }
  441. /**
  442. * Indicate if the value specifies a directory.
  443. *
  444. * @return true if the value specifies a directory.
  445. */
  446. public boolean isDir() {
  447. return "dir".equalsIgnoreCase(getValue());
  448. }
  449. /**
  450. * Indicate if the value specifies a file.
  451. *
  452. * @return true if the value specifies a file.
  453. */
  454. public boolean isFile() {
  455. return "file".equalsIgnoreCase(getValue());
  456. }
  457. }
  458. }