KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > JavaResource


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.types.resources;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.apache.tools.ant.util.FileUtils;
24 import org.apache.tools.ant.types.Path;
25 import org.apache.tools.ant.types.Resource;
26 import org.apache.tools.ant.types.Reference;
27
28 /**
29  * A Resource representation of something loadable via a Java classloader.
30  * @since Ant 1.7
31  */

32 public class JavaResource extends Resource {
33
34     private Path classpath;
35     private Reference loader;
36
37     /**
38      * Default constructor.
39      */

40     public JavaResource() {
41     }
42
43     /**
44      * Construct a new JavaResource using the specified name and
45      * classpath.
46      *
47      * @param name the resource name.
48      * @param path the classpath.
49      */

50     public JavaResource(String JavaDoc name, Path path) {
51         setName(name);
52         classpath = path;
53     }
54
55     /**
56      * Set the classpath to use when looking up a resource.
57      * @param classpath to add to any existing classpath
58      */

59     public void setClasspath(Path classpath) {
60         checkAttributesAllowed();
61         if (this.classpath == null) {
62             this.classpath = classpath;
63         } else {
64             this.classpath.append(classpath);
65         }
66     }
67
68     /**
69      * Add a classpath to use when looking up a resource.
70      * @return The classpath to be configured
71      */

72     public Path createClasspath() {
73         checkChildrenAllowed();
74         if (this.classpath == null) {
75             this.classpath = new Path(getProject());
76         }
77         return this.classpath.createPath();
78     }
79
80     /**
81      * Set the classpath to use when looking up a resource,
82      * given as reference to a <path> defined elsewhere
83      * @param r The reference value
84      */

85     public void setClasspathRef(Reference r) {
86         checkAttributesAllowed();
87         createClasspath().setRefid(r);
88     }
89
90     /**
91      * get the classpath used by this <code>LoadProperties</code>.
92      * @return The classpath
93      */

94     public Path getClasspath() {
95         return isReference()
96             ? ((JavaResource) getCheckedRef()).getClasspath() : classpath;
97     }
98
99     /**
100      * Use the reference to locate the loader. If the loader is not
101      * found, taskdef will use the specified classpath and register it
102      * with the specified name.
103      *
104      * This allow multiple taskdef/typedef to use the same class loader,
105      * so they can be used together. It eliminate the need to
106      * put them in the CLASSPATH.
107      *
108      * @param r the reference to locate the loader.
109      */

110     public void setLoaderRef(Reference r) {
111         checkAttributesAllowed();
112         loader = r;
113     }
114
115     /**
116      * Overrides the super version.
117      * @param r the Reference to set.
118      */

119     public void setRefid(Reference r) {
120         if (loader != null || classpath != null) {
121             throw tooManyAttributes();
122         }
123         super.setRefid(r);
124     }
125
126     /**
127      * Learn whether this file exists.
128      * @return true if this resource exists.
129      */

130     public boolean isExists() {
131         InputStream JavaDoc is = null;
132         try {
133             return isReference() ? ((Resource) getCheckedRef()).isExists()
134                 : (is = getInputStream()) != null;
135         } catch (IOException JavaDoc ex) {
136             return false;
137         } finally {
138             FileUtils.close(is);
139         }
140     }
141
142     /**
143      * Return an InputStream for reading the contents of this Resource.
144      * @return an InputStream object.
145      * @throws IOException if an error occurs.
146      */

147     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
148         if (isReference()) {
149             return ((Resource) getCheckedRef()).getInputStream();
150         }
151         ClassLoader JavaDoc cl = null;
152         if (loader != null) {
153             cl = (ClassLoader JavaDoc) loader.getReferencedObject();
154         }
155         if (cl == null) {
156             if (getClasspath() != null) {
157                 cl = getProject().createClassLoader(classpath);
158             } else {
159                 cl = JavaResource.class.getClassLoader();
160             }
161             if (loader != null && cl != null) {
162                 getProject().addReference(loader.getRefId(), cl);
163             }
164         }
165
166         return cl == null ? ClassLoader.getSystemResourceAsStream(getName())
167             : cl.getResourceAsStream(getName());
168     }
169
170     /**
171      * Compare this JavaResource to another Resource.
172      * @param another the other Resource against which to compare.
173      * @return a negative integer, zero, or a positive integer as this
174      * JavaResource is less than, equal to, or greater than the
175      * specified Resource.
176      */

177     public int compareTo(Object JavaDoc another) {
178         if (isReference()) {
179             return ((Comparable JavaDoc) getCheckedRef()).compareTo(another);
180         }
181         if (another.getClass().equals(getClass())) {
182             JavaResource otherjr = (JavaResource) another;
183             if (!getName().equals(otherjr.getName())) {
184                 return getName().compareTo(otherjr.getName());
185             }
186             if (loader != otherjr.loader) {
187                 if (loader == null) {
188                     return -1;
189                 }
190                 if (otherjr.loader == null) {
191                     return 1;
192                 }
193                 return loader.getRefId().compareTo(otherjr.loader.getRefId());
194             }
195             Path p = getClasspath();
196             Path op = otherjr.getClasspath();
197             if (p != op) {
198                 if (p == null) {
199                     return -1;
200                 }
201                 if (op == null) {
202                     return 1;
203                 }
204                 return p.toString().compareTo(op.toString());
205             }
206             return 0;
207         }
208         return super.compareTo(another);
209     }
210
211 }
212
Popular Tags