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.

WhichResource.java 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 org.apache.tools.ant.types.Path;
  20. import org.apache.tools.ant.types.Reference;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.AntClassLoader;
  25. import java.net.URL;
  26. /**
  27. * Find a class or resource on the supplied classpath, or the
  28. * system classpath if none is supplied. The named property is set if
  29. * the item can be found. For example
  30. * <pre>
  31. * &lt;whichresource resource="/log4j.properties"
  32. * property="log4j.url" &gt;
  33. * </pre>
  34. * @since Ant 1.6
  35. * @ant.attribute.group name="oneof" description="Exactly one of these two"
  36. */
  37. public class WhichResource extends Task {
  38. /**
  39. * our classpath
  40. */
  41. private Path classpath;
  42. /**
  43. * class to look for
  44. */
  45. private String classname;
  46. /**
  47. * resource to look for
  48. */
  49. private String resource;
  50. /**
  51. * property to set
  52. */
  53. private String property;
  54. /**
  55. * Set the classpath to be used for this compilation.
  56. * @param cp the classpath to be used.
  57. */
  58. public void setClasspath(Path cp) {
  59. if (classpath == null) {
  60. classpath = cp;
  61. } else {
  62. classpath.append(cp);
  63. }
  64. }
  65. /**
  66. * Adds a path to the classpath.
  67. * @return a classpath to be configured.
  68. */
  69. public Path createClasspath() {
  70. if (classpath == null) {
  71. classpath = new Path(getProject());
  72. }
  73. return classpath.createPath();
  74. }
  75. /**
  76. * Set the classpath to use by reference.
  77. *
  78. * @param r a reference to an existing classpath.
  79. * @since Ant 1.7.1
  80. */
  81. public void setClasspathRef(Reference r) {
  82. createClasspath().setRefid(r);
  83. }
  84. /**
  85. * validate
  86. */
  87. private void validate() {
  88. int setcount = 0;
  89. if (classname != null) {
  90. setcount++;
  91. }
  92. if (resource != null) {
  93. setcount++;
  94. }
  95. if (setcount == 0) {
  96. throw new BuildException(
  97. "One of classname or resource must be specified");
  98. }
  99. if (setcount > 1) {
  100. throw new BuildException(
  101. "Only one of classname or resource can be specified");
  102. }
  103. if (property == null) {
  104. throw new BuildException("No property defined");
  105. }
  106. }
  107. /**
  108. * execute it
  109. * @throws BuildException on error
  110. */
  111. public void execute() throws BuildException {
  112. validate();
  113. if (classpath != null) {
  114. getProject().log("using user supplied classpath: " + classpath,
  115. Project.MSG_DEBUG);
  116. classpath = classpath.concatSystemClasspath("ignore");
  117. } else {
  118. classpath = new Path(getProject());
  119. classpath = classpath.concatSystemClasspath("only");
  120. getProject().log("using system classpath: " + classpath, Project.MSG_DEBUG);
  121. }
  122. AntClassLoader loader;
  123. loader = new AntClassLoader(getProject().getCoreLoader(),
  124. getProject(),
  125. classpath, false);
  126. String loc = null;
  127. if (classname != null) {
  128. //convert a class name into a resource
  129. resource = classname.replace('.', '/') + ".class";
  130. }
  131. if (resource == null) {
  132. throw new BuildException("One of class or resource is required");
  133. }
  134. if (resource.startsWith("/")) {
  135. resource = resource.substring(1);
  136. }
  137. log("Searching for " + resource, Project.MSG_VERBOSE);
  138. URL url;
  139. url = loader.getResource(resource);
  140. if (url != null) {
  141. //set the property
  142. loc = url.toExternalForm();
  143. getProject().setNewProperty(property, loc);
  144. }
  145. }
  146. /**
  147. * name the resource to look for
  148. * @param resource the name of the resource to look for.
  149. * @ant.attribute group="oneof"
  150. */
  151. public void setResource(String resource) {
  152. this.resource = resource;
  153. }
  154. /**
  155. * name the class to look for
  156. * @param classname the name of the class to look for.
  157. * @ant.attribute group="oneof"
  158. */
  159. public void setClass(String classname) {
  160. this.classname = classname;
  161. }
  162. /**
  163. * the property to fill with the URL of the resource or class
  164. * @param property the property to be set.
  165. * @ant.attribute group="required"
  166. */
  167. public void setProperty(String property) {
  168. this.property = property;
  169. }
  170. }