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