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

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