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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. /**
  66. * Will set the given property if the requested resource is available at runtime.
  67. *
  68. * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  69. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  70. *
  71. * @ant.task category="control"
  72. */
  73. public class Available extends Task implements Condition {
  74. private String property;
  75. private String classname;
  76. private String file;
  77. private Path filepath;
  78. private String resource;
  79. private FileDir type;
  80. private Path classpath;
  81. private AntClassLoader loader;
  82. private String value = "true";
  83. private boolean isTask = false;
  84. public void setClasspath(Path classpath) {
  85. createClasspath().append(classpath);
  86. }
  87. public Path createClasspath() {
  88. if (this.classpath == null) {
  89. this.classpath = new Path(project);
  90. }
  91. return this.classpath.createPath();
  92. }
  93. public void setClasspathRef(Reference r) {
  94. createClasspath().setRefid(r);
  95. }
  96. public void setFilepath(Path filepath) {
  97. createFilepath().append(filepath);
  98. }
  99. public Path createFilepath() {
  100. if (this.filepath == null) {
  101. this.filepath = new Path(project);
  102. }
  103. return this.filepath.createPath();
  104. }
  105. public void setProperty(String property) {
  106. this.property = property;
  107. }
  108. public void setValue(String value) {
  109. this.value = value;
  110. }
  111. public void setClassname(String classname) {
  112. if (!"".equals(classname)) {
  113. this.classname = classname;
  114. }
  115. }
  116. public void setFile(String file) {
  117. this.file = file;
  118. }
  119. public void setResource(String resource) {
  120. this.resource = resource;
  121. }
  122. /**
  123. * @deprecated setType(String) is deprecated and is replaced with
  124. * setType(Available.FileDir) to make Ant's Introspection
  125. * mechanism do the work and also to encapsulate operations on
  126. * the type in its own class.
  127. */
  128. public void setType(String type) {
  129. log("DEPRECATED - The setType(String) method has been deprecated."
  130. + " Use setType(Available.FileDir) instead.");
  131. this.type = new FileDir();
  132. this.type.setValue(type);
  133. }
  134. public void setType(FileDir type) {
  135. this.type = type;
  136. }
  137. public void execute() throws BuildException {
  138. if (property == null) {
  139. throw new BuildException("property attribute is required", location);
  140. }
  141. isTask = true;
  142. if (eval()) {
  143. String lSep = System.getProperty("line.separator");
  144. if (null != project.getProperty(property)) {
  145. log("DEPRECATED - <available> used to overide an existing property. "
  146. + lSep
  147. + " Build writer should not reuse the same property name for "
  148. + lSep + "different values.");
  149. }
  150. this.project.setProperty(property, value);
  151. }
  152. isTask = false;
  153. }
  154. public boolean eval() throws BuildException {
  155. if (classname == null && file == null && resource == null) {
  156. throw new BuildException("At least one of (classname|file|resource) is required", location);
  157. }
  158. if (type != null){
  159. if (file == null){
  160. throw new BuildException("The type attribute is only valid when specifying the file attribute.");
  161. }
  162. }
  163. if (classpath != null) {
  164. classpath.setProject(project);
  165. this.loader = new AntClassLoader(project, classpath);
  166. }
  167. String appendix = "";
  168. if (isTask) {
  169. appendix = " to set property " + property;
  170. } else {
  171. setTaskName("available");
  172. }
  173. if ((classname != null) && !checkClass(classname)) {
  174. log("Unable to load class " + classname + appendix, Project.MSG_VERBOSE);
  175. return false;
  176. }
  177. if ((file != null) && !checkFile()) {
  178. if (type != null) {
  179. log("Unable to find " + type + " " + file + appendix, Project.MSG_VERBOSE);
  180. } else {
  181. log("Unable to find " + file + appendix, Project.MSG_VERBOSE);
  182. }
  183. return false;
  184. }
  185. if ((resource != null) && !checkResource(resource)) {
  186. log("Unable to load resource " + resource + appendix, Project.MSG_VERBOSE);
  187. return false;
  188. }
  189. if (loader != null) {
  190. loader.cleanup();
  191. }
  192. if (!isTask) {
  193. setTaskName(null);
  194. }
  195. return true;
  196. }
  197. private boolean checkFile() {
  198. if (filepath == null) {
  199. return checkFile(project.resolveFile(file), file);
  200. } else {
  201. String[] paths = filepath.list();
  202. for(int i = 0; i < paths.length; ++i) {
  203. log("Searching " + paths[i], Project.MSG_DEBUG);
  204. /*
  205. ** filepath can be a list of directory and/or
  206. ** file names (gen'd via <fileset>)
  207. **
  208. ** look for:
  209. ** full-pathname specified == path in list
  210. ** full-pathname specified == parent dir of path in list
  211. ** simple name specified == path in list
  212. ** simple name specified == path in list + name
  213. ** simple name specified == parent dir + name
  214. ** simple name specified == parent of parent dir + name
  215. **
  216. */
  217. File path = new File(paths[i]);
  218. // ** full-pathname specified == path in list
  219. // ** simple name specified == path in list
  220. if (path.exists() && file.equals(paths[i])) {
  221. if (type == null) {
  222. log("Found: " + path, Project.MSG_VERBOSE);
  223. return true;
  224. } else if (type.isDir()
  225. && path.isDirectory()) {
  226. log("Found directory: " + path, Project.MSG_VERBOSE);
  227. return true;
  228. } else if (type.isFile()
  229. && path.isFile()) {
  230. log("Found file: " + path, Project.MSG_VERBOSE);
  231. return true;
  232. }
  233. // not the requested type
  234. return false;
  235. }
  236. FileUtils fileUtils = FileUtils.newFileUtils();
  237. File parent = fileUtils.getParentFile(path);
  238. // ** full-pathname specified == parent dir of path in list
  239. if (parent != null && parent.exists()
  240. && file.equals(parent.getAbsolutePath())) {
  241. if (type == null) {
  242. log("Found: " + parent, Project.MSG_VERBOSE);
  243. return true;
  244. } else if (type.isDir()) {
  245. log("Found directory: " + parent, Project.MSG_VERBOSE);
  246. return true;
  247. }
  248. // not the requested type
  249. return false;
  250. }
  251. // ** simple name specified == path in list + name
  252. if (path.exists() && path.isDirectory()) {
  253. if (checkFile(new File(path, file),
  254. file + " in " + path)) {
  255. return true;
  256. }
  257. }
  258. // ** simple name specified == parent dir + name
  259. if (parent != null && parent.exists()) {
  260. if (checkFile(new File(parent, file),
  261. file + " in " + parent)) {
  262. return true;
  263. }
  264. }
  265. // ** simple name specified == parent of parent dir + name
  266. if (parent != null) {
  267. File grandParent = fileUtils.getParentFile(parent);
  268. if (grandParent != null && grandParent.exists()) {
  269. if (checkFile(new File(grandParent, file),
  270. file + " in " + grandParent)) {
  271. return true;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return false;
  278. }
  279. private boolean checkFile(File f, String text) {
  280. if (type != null) {
  281. if (type.isDir()) {
  282. if( f.isDirectory()) {
  283. log("Found directory: " + text, Project.MSG_VERBOSE);
  284. }
  285. return f.isDirectory();
  286. } else if (type.isFile()) {
  287. if( f.isFile()) {
  288. log("Found file: " + text, Project.MSG_VERBOSE);
  289. }
  290. return f.isFile();
  291. }
  292. }
  293. if (f.exists()) {
  294. log("Found: " + text, Project.MSG_VERBOSE);
  295. }
  296. return f.exists();
  297. }
  298. private boolean checkResource(String resource) {
  299. if (loader != null) {
  300. return (loader.getResourceAsStream(resource) != null);
  301. } else {
  302. ClassLoader cL = this.getClass().getClassLoader();
  303. if (cL != null) {
  304. return (cL.getResourceAsStream(resource) != null);
  305. } else {
  306. return
  307. (ClassLoader.getSystemResourceAsStream(resource) != null);
  308. }
  309. }
  310. }
  311. private boolean checkClass(String classname) {
  312. try {
  313. if (loader != null) {
  314. loader.loadClass(classname);
  315. } else {
  316. ClassLoader l = this.getClass().getClassLoader();
  317. // Can return null to represent the bootstrap class loader.
  318. // see API docs of Class.getClassLoader.
  319. if (l != null) {
  320. l.loadClass(classname);
  321. } else {
  322. Class.forName(classname);
  323. }
  324. }
  325. return true;
  326. } catch (ClassNotFoundException e) {
  327. return false;
  328. } catch (NoClassDefFoundError e) {
  329. return false;
  330. }
  331. }
  332. public static class FileDir extends EnumeratedAttribute {
  333. private final static String[] values = {"file", "dir"};
  334. public String[] getValues() {
  335. return values;
  336. }
  337. public boolean isDir() {
  338. return "dir".equalsIgnoreCase(getValue());
  339. }
  340. public boolean isFile() {
  341. return "file".equalsIgnoreCase(getValue());
  342. }
  343. public String toString() {
  344. return getValue();
  345. }
  346. }
  347. }