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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999-2001 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.Path;
  62. import org.apache.tools.ant.types.Reference;
  63. /**
  64. * Will set the given property if the requested resource is available at runtime.
  65. *
  66. * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  67. */
  68. public class Available extends Task implements Condition {
  69. private String property;
  70. private String classname;
  71. private String file;
  72. private Path filepath;
  73. private String resource;
  74. private String type;
  75. private Path classpath;
  76. private AntClassLoader loader;
  77. private String value = "true";
  78. public void setClasspath(Path classpath) {
  79. createClasspath().append(classpath);
  80. }
  81. public Path createClasspath() {
  82. if (this.classpath == null) {
  83. this.classpath = new Path(project);
  84. }
  85. return this.classpath.createPath();
  86. }
  87. public void setClasspathRef(Reference r) {
  88. createClasspath().setRefid(r);
  89. }
  90. public void setFilepath(Path filepath) {
  91. createFilepath().append(filepath);
  92. }
  93. public Path createFilepath() {
  94. if (this.filepath == null) {
  95. this.filepath = new Path(project);
  96. }
  97. return this.filepath.createPath();
  98. }
  99. public void setProperty(String property) {
  100. this.property = property;
  101. }
  102. public void setValue(String value) {
  103. this.value = value;
  104. }
  105. public void setClassname(String classname) {
  106. if (!"".equals(classname)) {
  107. this.classname = classname;
  108. }
  109. }
  110. public void setFile(String file) {
  111. this.file = file;
  112. }
  113. public void setResource(String resource) {
  114. this.resource = resource;
  115. }
  116. public void setType(String type) {
  117. this.type = type;
  118. }
  119. public void execute() throws BuildException {
  120. if (property == null) {
  121. throw new BuildException("property attribute is required", location);
  122. }
  123. if (eval()) {
  124. this.project.setProperty(property, value);
  125. }
  126. }
  127. public boolean eval() throws BuildException {
  128. if (classname == null && file == null && resource == null) {
  129. throw new BuildException("At least one of (classname|file|resource) is required", location);
  130. }
  131. if (type != null){
  132. if (file == null){
  133. throw new BuildException("The type attribute is only valid when specifying the file attribute.");
  134. }
  135. if (!type.equalsIgnoreCase("file") && !type.equalsIgnoreCase("dir")){
  136. throw new BuildException("Type must be one of either dir or file");
  137. }
  138. }
  139. if (classpath != null) {
  140. classpath.setProject(project);
  141. this.loader = new AntClassLoader(project, classpath);
  142. }
  143. if ((classname != null) && !checkClass(classname)) {
  144. log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE);
  145. return false;
  146. }
  147. if ((file != null) && !checkFile()) {
  148. if (type != null) {
  149. log("Unable to find " + type + " " + file + " to set property " + property, Project.MSG_VERBOSE);
  150. } else {
  151. log("Unable to find " + file + " to set property " + property, Project.MSG_VERBOSE);
  152. }
  153. return false;
  154. }
  155. if ((resource != null) && !checkResource(resource)) {
  156. log("Unable to load resource " + resource + " to set property " + property, Project.MSG_VERBOSE);
  157. return false;
  158. }
  159. if (loader != null) {
  160. loader.cleanup();
  161. }
  162. return true;
  163. }
  164. private boolean checkFile() {
  165. if (filepath == null) {
  166. return checkFile(file);
  167. } else {
  168. String[] paths = filepath.list();
  169. for(int i = 0; i < paths.length; ++i) {
  170. log("Searching " + paths[i], Project.MSG_DEBUG);
  171. /*
  172. ** filepath can be a list of directory and/or
  173. ** file names (gen'd via <fileset>)
  174. **
  175. ** look for:
  176. ** full-pathname specified == path in list
  177. ** full-pathname specified == parent dir of path in list
  178. ** simple name specified == path in list
  179. ** simple name specified == path in list + name
  180. ** simple name specified == parent dir + name
  181. ** simple name specified == parent of parent dir + name
  182. **
  183. */
  184. File path = new File(paths[i]);
  185. String dirname = path.getParent();
  186. if (type != null) {
  187. if (type.equalsIgnoreCase("dir")) {
  188. if (path.isFile()) {
  189. // full-pathname specified
  190. if (dirname.equals(path.toString())) {
  191. log("Found directory: " + path, Project.MSG_VERBOSE);
  192. return true;
  193. // simple name specified
  194. } else if(new File(dirname, file).isDirectory()) {
  195. log("Found directory: " + dirname + File.separator + file, Project.MSG_VERBOSE);
  196. return true;
  197. }
  198. // full-pathname specified
  199. } else if (path.toString().equals(new File(file).toString()) && path.isDirectory()) {
  200. log("Found directory: " + path, Project.MSG_VERBOSE);
  201. return true;
  202. // simple name specified
  203. } else if (new File(path, file).isDirectory()) {
  204. log("Found directory: " + path + File.separator + file, Project.MSG_VERBOSE);
  205. return true;
  206. }
  207. /* end check for type dir */
  208. } else {
  209. if (path.toString().equals(new File(file).toString()) && path.isFile()) {
  210. log("Found file: " + path, Project.MSG_VERBOSE);
  211. return true;
  212. } else if (new File(path, file).isFile()) {
  213. log("Found file: " + path + File.separator + file, Project.MSG_VERBOSE);
  214. return true;
  215. } else if (new File(dirname, file).isFile()) {
  216. log("Found file: " + dirname + File.separator + file, Project.MSG_VERBOSE);
  217. return true;
  218. }
  219. }
  220. /* end check for specified type */
  221. } else {
  222. if (path.toString().equals(new File(file).toString())) {
  223. log("Found: " + path, Project.MSG_VERBOSE);
  224. return true;
  225. } else if (new File(path, file).exists()) {
  226. log("Found: " + path + File.separator + file, Project.MSG_VERBOSE);
  227. return true;
  228. } else if (new File(dirname, file).exists()) {
  229. log("Found: " + dirname + File.separator + file, Project.MSG_VERBOSE);
  230. return true;
  231. } else {
  232. File dir = new File(dirname);
  233. dirname = dir.getParent();
  234. if (new File(dirname, file).exists()) {
  235. log("Found: " + dirname + File.separator + file, Project.MSG_VERBOSE);
  236. return true;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return false;
  243. }
  244. private boolean checkFile(String file) {
  245. File filename = new File(file);
  246. if (type != null) {
  247. if (type.equalsIgnoreCase("dir")) {
  248. if( filename.isDirectory()) {
  249. log("Found directory: " + file, Project.MSG_VERBOSE);
  250. }
  251. return filename.isDirectory();
  252. } else if (type.equalsIgnoreCase("file")) {
  253. if( filename.isFile()) {
  254. log("Found file: " + file, Project.MSG_VERBOSE);
  255. }
  256. return filename.isFile();
  257. }
  258. }
  259. if (filename.exists()) {
  260. log("Found: " + file, Project.MSG_VERBOSE);
  261. }
  262. return filename.exists();
  263. }
  264. private boolean checkResource(String resource) {
  265. if (loader != null) {
  266. return (loader.getResourceAsStream(resource) != null);
  267. } else {
  268. ClassLoader cL = this.getClass().getClassLoader();
  269. if (cL != null) {
  270. return (cL.getResourceAsStream(resource) != null);
  271. } else {
  272. return
  273. (ClassLoader.getSystemResourceAsStream(resource) != null);
  274. }
  275. }
  276. }
  277. private boolean checkClass(String classname) {
  278. try {
  279. if (loader != null) {
  280. loader.loadClass(classname);
  281. } else {
  282. ClassLoader l = this.getClass().getClassLoader();
  283. // Can return null to represent the bootstrap class loader.
  284. // see API docs of Class.getClassLoader.
  285. if (l != null) {
  286. l.loadClass(classname);
  287. } else {
  288. Class.forName(classname);
  289. }
  290. }
  291. return true;
  292. } catch (ClassNotFoundException e) {
  293. return false;
  294. } catch (NoClassDefFoundError e) {
  295. return false;
  296. }
  297. }
  298. }