KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > WhichResource


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
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.types.Path;
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.AntClassLoader;
26
27 import java.net.URL JavaDoc;
28
29 /**
30  * Find a class or resource on the supplied classpath, or the
31  * system classpath if none is supplied. The named property is set if
32  * the item can be found. For example
33  * <pre>
34  * &lt;whichresource resource="/log4j.properties"
35  * property="log4j.url" &gt;
36  * </pre>
37  * @since Ant 1.6
38  * @ant.attribute.group name="oneof" description="Exactly one of these two"
39  */

40 public class WhichResource extends Task {
41     /**
42      * our classpath
43      */

44     private Path classpath;
45
46     /**
47      * class to look for
48      */

49     private String JavaDoc classname;
50
51     /**
52      * resource to look for
53      */

54     private String JavaDoc resource;
55
56     /**
57      * property to set
58      */

59     private String JavaDoc property;
60
61
62     /**
63      * Set the classpath to be used for this compilation.
64      * @param cp the classpath to be used.
65      */

66     public void setClasspath(Path cp) {
67         if (classpath == null) {
68             classpath = cp;
69         } else {
70             classpath.append(cp);
71         }
72     }
73
74     /**
75      * Adds a path to the classpath.
76      * @return a classpath to be configured.
77      */

78     public Path createClasspath() {
79         if (classpath == null) {
80             classpath = new Path(getProject());
81         }
82         return classpath.createPath();
83     }
84
85
86     /**
87      * validate
88      */

89     private void validate() {
90         int setcount = 0;
91         if (classname != null) {
92             setcount++;
93         }
94         if (resource != null) {
95             setcount++;
96         }
97
98
99         if (setcount == 0) {
100             throw new BuildException(
101                     "One of classname or resource must be specified");
102         }
103         if (setcount > 1) {
104             throw new BuildException(
105                     "Only one of classname or resource can be specified");
106         }
107         if (property == null) {
108             throw new BuildException("No property defined");
109         }
110     }
111
112     /**
113      * execute it
114      * @throws BuildException on error
115      */

116     public void execute() throws BuildException {
117         validate();
118         if (classpath != null) {
119             getProject().log("using user supplied classpath: " + classpath,
120                     Project.MSG_DEBUG);
121             classpath = classpath.concatSystemClasspath("ignore");
122         } else {
123             classpath = new Path(getProject());
124             classpath = classpath.concatSystemClasspath("only");
125             getProject().log("using system classpath: " + classpath, Project.MSG_DEBUG);
126         }
127         AntClassLoader loader;
128         loader = new AntClassLoader(getProject().getCoreLoader(),
129                     getProject(),
130                     classpath, false);
131         String JavaDoc loc = null;
132         if (classname != null) {
133             //convert a class name into a resource
134
resource = classname.replace('.', '/') + ".class";
135         }
136
137         if (resource == null) {
138             throw new BuildException("One of class or resource is required");
139         }
140
141         if (resource.startsWith("/")) {
142             resource = resource.substring(1);
143         }
144
145         log("Searching for " + resource, Project.MSG_VERBOSE);
146         URL JavaDoc url;
147         url = loader.getResource(resource);
148         if (url != null) {
149             //set the property
150
loc = url.toExternalForm();
151             getProject().setNewProperty(property, loc);
152         }
153     }
154
155     /**
156      * name the resource to look for
157      * @param resource the name of the resource to look for.
158      * @ant.attribute group="oneof"
159      */

160     public void setResource(String JavaDoc resource) {
161         this.resource = resource;
162     }
163
164     /**
165      * name the class to look for
166      * @param classname the name of the class to look for.
167      * @ant.attribute group="oneof"
168      */

169     public void setClass(String JavaDoc classname) {
170         this.classname = classname;
171     }
172
173     /**
174      * the property to fill with the URL of the resource or class
175      * @param property the property to be set.
176      * @ant.attribute group="required"
177      */

178     public void setProperty(String JavaDoc property) {
179         this.property = property;
180     }
181
182 }
183
Popular Tags