source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/WhichResource.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

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