001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.types.Path;
022: import org.apache.tools.ant.Task;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.AntClassLoader;
026:
027: import java.net.URL;
028:
029: /**
030: * Find a class or resource on the supplied classpath, or the
031: * system classpath if none is supplied. The named property is set if
032: * the item can be found. For example
033: * <pre>
034: * <whichresource resource="/log4j.properties"
035: * property="log4j.url" >
036: * </pre>
037: * @since Ant 1.6
038: * @ant.attribute.group name="oneof" description="Exactly one of these two"
039: */
040: public class WhichResource extends Task {
041: /**
042: * our classpath
043: */
044: private Path classpath;
045:
046: /**
047: * class to look for
048: */
049: private String classname;
050:
051: /**
052: * resource to look for
053: */
054: private String resource;
055:
056: /**
057: * property to set
058: */
059: private String property;
060:
061: /**
062: * Set the classpath to be used for this compilation.
063: * @param cp the classpath to be used.
064: */
065: public void setClasspath(Path cp) {
066: if (classpath == null) {
067: classpath = cp;
068: } else {
069: classpath.append(cp);
070: }
071: }
072:
073: /**
074: * Adds a path to the classpath.
075: * @return a classpath to be configured.
076: */
077: public Path createClasspath() {
078: if (classpath == null) {
079: classpath = new Path(getProject());
080: }
081: return classpath.createPath();
082: }
083:
084: /**
085: * validate
086: */
087: private void validate() {
088: int setcount = 0;
089: if (classname != null) {
090: setcount++;
091: }
092: if (resource != null) {
093: setcount++;
094: }
095:
096: if (setcount == 0) {
097: throw new BuildException(
098: "One of classname or resource must be specified");
099: }
100: if (setcount > 1) {
101: throw new BuildException(
102: "Only one of classname or resource can be specified");
103: }
104: if (property == null) {
105: throw new BuildException("No property defined");
106: }
107: }
108:
109: /**
110: * execute it
111: * @throws BuildException on error
112: */
113: public void execute() throws BuildException {
114: validate();
115: if (classpath != null) {
116: getProject().log(
117: "using user supplied classpath: " + classpath,
118: Project.MSG_DEBUG);
119: classpath = classpath.concatSystemClasspath("ignore");
120: } else {
121: classpath = new Path(getProject());
122: classpath = classpath.concatSystemClasspath("only");
123: getProject().log("using system classpath: " + classpath,
124: Project.MSG_DEBUG);
125: }
126: AntClassLoader loader;
127: loader = new AntClassLoader(getProject().getCoreLoader(),
128: getProject(), classpath, false);
129: String loc = null;
130: if (classname != null) {
131: //convert a class name into a resource
132: resource = classname.replace('.', '/') + ".class";
133: }
134:
135: if (resource == null) {
136: throw new BuildException(
137: "One of class or resource is required");
138: }
139:
140: if (resource.startsWith("/")) {
141: resource = resource.substring(1);
142: }
143:
144: log("Searching for " + resource, Project.MSG_VERBOSE);
145: URL url;
146: url = loader.getResource(resource);
147: if (url != null) {
148: //set the property
149: loc = url.toExternalForm();
150: getProject().setNewProperty(property, loc);
151: }
152: }
153:
154: /**
155: * name the resource to look for
156: * @param resource the name of the resource to look for.
157: * @ant.attribute group="oneof"
158: */
159: public void setResource(String resource) {
160: this .resource = resource;
161: }
162:
163: /**
164: * name the class to look for
165: * @param classname the name of the class to look for.
166: * @ant.attribute group="oneof"
167: */
168: public void setClass(String classname) {
169: this .classname = classname;
170: }
171:
172: /**
173: * the property to fill with the URL of the resource or class
174: * @param property the property to be set.
175: * @ant.attribute group="required"
176: */
177: public void setProperty(String property) {
178: this.property = property;
179: }
180:
181: }
|