KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > ClassPathResource


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.core.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.springframework.util.Assert;
26 import org.springframework.util.ClassUtils;
27 import org.springframework.util.ObjectUtils;
28 import org.springframework.util.ResourceUtils;
29 import org.springframework.util.StringUtils;
30
31 /**
32  * {@link Resource} implementation for class path resources.
33  * Uses either a given ClassLoader or a given Class for loading resources.
34  *
35  * <p>Supports resolution as <code>java.io.File</code> if the class path
36  * resource resides in the file system, but not for resources in a JAR.
37  * Always supports resolution as URL.
38  *
39  * @author Juergen Hoeller
40  * @since 28.12.2003
41  * @see java.lang.ClassLoader#getResourceAsStream(String)
42  * @see java.lang.Class#getResourceAsStream(String)
43  */

44 public class ClassPathResource extends AbstractResource {
45
46     private final String JavaDoc path;
47
48     private ClassLoader JavaDoc classLoader;
49
50     private Class JavaDoc clazz;
51
52
53     /**
54      * Create a new ClassPathResource for ClassLoader usage.
55      * A leading slash will be removed, as the ClassLoader
56      * resource access methods will not accept it.
57      * <p>The thread context class loader will be used for
58      * loading the resource.
59      * @param path the absolute path within the class path
60      * @see java.lang.ClassLoader#getResourceAsStream(String)
61      * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
62      */

63     public ClassPathResource(String JavaDoc path) {
64         this(path, (ClassLoader JavaDoc) null);
65     }
66
67     /**
68      * Create a new ClassPathResource for ClassLoader usage.
69      * A leading slash will be removed, as the ClassLoader
70      * resource access methods will not accept it.
71      * @param path the absolute path within the classpath
72      * @param classLoader the class loader to load the resource with,
73      * or <code>null</code> for the thread context class loader
74      * @see java.lang.ClassLoader#getResourceAsStream(String)
75      */

76     public ClassPathResource(String JavaDoc path, ClassLoader JavaDoc classLoader) {
77         Assert.notNull(path, "Path must not be null");
78         if (path.startsWith("/")) {
79             path = path.substring(1);
80         }
81         this.path = StringUtils.cleanPath(path);
82         this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
83     }
84
85     /**
86      * Create a new ClassPathResource for Class usage.
87      * The path can be relative to the given class,
88      * or absolute within the classpath via a leading slash.
89      * @param path relative or absolute path within the class path
90      * @param clazz the class to load resources with
91      * @see java.lang.Class#getResourceAsStream
92      */

93     public ClassPathResource(String JavaDoc path, Class JavaDoc clazz) {
94         Assert.notNull(path, "Path must not be null");
95         this.path = StringUtils.cleanPath(path);
96         this.clazz = clazz;
97     }
98
99     /**
100      * Create a new ClassPathResource with optional ClassLoader and Class.
101      * Only for internal usage.
102      * @param path relative or absolute path within the classpath
103      * @param classLoader the class loader to load the resource with, if any
104      * @param clazz the class to load resources with, if any
105      */

106     protected ClassPathResource(String JavaDoc path, ClassLoader JavaDoc classLoader, Class JavaDoc clazz) {
107         this.path = path;
108         this.classLoader = classLoader;
109         this.clazz = clazz;
110     }
111
112
113     /**
114      * Return the path for this resource.
115      */

116     public final String JavaDoc getPath() {
117         return this.path;
118     }
119
120
121     /**
122      * This implementation opens an InputStream for the given class path resource.
123      * @see java.lang.ClassLoader#getResourceAsStream(String)
124      * @see java.lang.Class#getResourceAsStream(String)
125      */

126     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
127         InputStream JavaDoc is = null;
128         if (this.clazz != null) {
129             is = this.clazz.getResourceAsStream(this.path);
130         }
131         else {
132             is = this.classLoader.getResourceAsStream(this.path);
133         }
134         if (is == null) {
135             throw new FileNotFoundException JavaDoc(
136                     getDescription() + " cannot be opened because it does not exist");
137         }
138         return is;
139     }
140
141     /**
142      * This implementation returns a URL for the underlying class path resource.
143      * @see java.lang.ClassLoader#getResource(String)
144      * @see java.lang.Class#getResource(String)
145      */

146     public URL JavaDoc getURL() throws IOException JavaDoc {
147         URL JavaDoc url = null;
148         if (this.clazz != null) {
149             url = this.clazz.getResource(this.path);
150         }
151         else {
152             url = this.classLoader.getResource(this.path);
153         }
154         if (url == null) {
155             throw new FileNotFoundException JavaDoc(
156                     getDescription() + " cannot be resolved to URL because it does not exist");
157         }
158         return url;
159     }
160
161     /**
162      * This implementation returns a File reference for the underlying class path
163      * resource, provided that it refers to a file in the file system.
164      * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
165      */

166     public File JavaDoc getFile() throws IOException JavaDoc {
167         return ResourceUtils.getFile(getURL(), getDescription());
168     }
169
170     /**
171      * This implementation creates a ClassPathResource, applying the given path
172      * relative to the path of the underlying resource of this descriptor.
173      * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
174      */

175     public Resource createRelative(String JavaDoc relativePath) {
176         String JavaDoc pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
177         return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
178     }
179
180     /**
181      * This implementation returns the name of the file that this class path
182      * resource refers to.
183      * @see org.springframework.util.StringUtils#getFilename(String)
184      */

185     public String JavaDoc getFilename() {
186         return StringUtils.getFilename(this.path);
187     }
188
189     /**
190      * This implementation returns a description that includes the class path location.
191      */

192     public String JavaDoc getDescription() {
193         return "class path resource [" + this.path + "]";
194     }
195
196
197     /**
198      * This implementation compares the underlying class path locations.
199      */

200     public boolean equals(Object JavaDoc obj) {
201         if (obj == this) {
202             return true;
203         }
204         if (obj instanceof ClassPathResource) {
205             ClassPathResource otherRes = (ClassPathResource) obj;
206             return (this.path.equals(otherRes.path) &&
207                     ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
208                     ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
209         }
210         return false;
211     }
212
213     /**
214      * This implementation returns the hash code of the underlying
215      * class path location.
216      */

217     public int hashCode() {
218         return this.path.hashCode();
219     }
220
221 }
222
Popular Tags