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

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