-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
ClassPathResource.java
290 lines (260 loc) · 8.94 KB
/
ClassPathResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.io;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@link Resource} implementation for class path resources. Uses either a
* given {@link ClassLoader} or a given {@link Class} for loading resources.
*
* <p>Supports resolution as {@code java.io.File} if the class path
* resource resides in the file system, but not for resources in a JAR.
* Always supports resolution as {@code java.net.URL}.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 28.12.2003
* @see ClassLoader#getResourceAsStream(String)
* @see ClassLoader#getResource(String)
* @see Class#getResourceAsStream(String)
* @see Class#getResource(String)
*/
public class ClassPathResource extends AbstractFileResolvingResource {
/**
* Internal representation of the original path supplied by the user,
* used for creating relative paths and resolving URLs and InputStreams.
*/
private final String path;
private final String absolutePath;
@Nullable
private final ClassLoader classLoader;
@Nullable
private final Class<?> clazz;
/**
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* <p>A leading slash will be removed, as the {@code ClassLoader} resource
* access methods will not accept it.
* <p>The default class loader will be used for loading the resource.
* @param path the absolute path within the class path
* @see ClassUtils#getDefaultClassLoader()
*/
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
/**
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* <p>A leading slash will be removed, as the {@code ClassLoader} resource
* access methods will not accept it.
* <p>If the supplied {@code ClassLoader} is {@code null}, the default class
* loader will be used for loading the resource.
* @param path the absolute path within the class path
* @param classLoader the class loader to load the resource with
* @see ClassUtils#getDefaultClassLoader()
*/
public ClassPathResource(String path, @Nullable ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
String pathToUse = StringUtils.cleanPath(path);
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;
this.absolutePath = pathToUse;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.clazz = null;
}
/**
* Create a new {@code ClassPathResource} for {@code Class} usage.
* <p>The path can be relative to the given class, or absolute within
* the class path via a leading slash.
* <p>If the supplied {@code Class} is {@code null}, the default class
* loader will be used for loading the resource.
* <p>This is also useful for resource access within the module system,
* loading a resource from the containing module of a given {@code Class}.
* See {@link ModuleResource} and its javadoc.
* @param path relative or absolute path within the class path
* @param clazz the class to load resources with
* @see ClassUtils#getDefaultClassLoader()
* @see ModuleResource
*/
public ClassPathResource(String path, @Nullable Class<?> clazz) {
Assert.notNull(path, "Path must not be null");
this.path = StringUtils.cleanPath(path);
String absolutePath = this.path;
if (clazz != null && !absolutePath.startsWith("/")) {
absolutePath = ClassUtils.classPackageAsResourcePath(clazz) + "/" + absolutePath;
}
else if (absolutePath.startsWith("/")) {
absolutePath = absolutePath.substring(1);
}
this.absolutePath = absolutePath;
this.classLoader = null;
this.clazz = clazz;
}
/**
* Return the <em>absolute path</em> for this resource, as a
* {@linkplain StringUtils#cleanPath(String) cleaned} resource path within
* the class path.
* <p>The path returned by this method does not have a leading slash and is
* suitable for use with {@link ClassLoader#getResource(String)}.
*/
public final String getPath() {
return this.absolutePath;
}
/**
* Return the {@link ClassLoader} that this resource will be obtained from.
*/
@Nullable
public final ClassLoader getClassLoader() {
return (this.clazz != null ? this.clazz.getClassLoader() : this.classLoader);
}
/**
* This implementation checks for the resolution of a resource URL.
* @see ClassLoader#getResource(String)
* @see Class#getResource(String)
*/
@Override
public boolean exists() {
return (resolveURL() != null);
}
/**
* This implementation checks for the resolution of a resource URL upfront,
* then proceeding with {@link AbstractFileResolvingResource}'s length check.
* @see ClassLoader#getResource(String)
* @see Class#getResource(String)
*/
@Override
public boolean isReadable() {
URL url = resolveURL();
return (url != null && checkReadable(url));
}
/**
* Resolves a {@link URL} for the underlying class path resource.
* @return the resolved URL, or {@code null} if not resolvable
*/
@Nullable
protected URL resolveURL() {
try {
if (this.clazz != null) {
return this.clazz.getResource(this.path);
}
else if (this.classLoader != null) {
return this.classLoader.getResource(this.absolutePath);
}
else {
return ClassLoader.getSystemResource(this.absolutePath);
}
}
catch (IllegalArgumentException ex) {
// Should not happen according to the JDK's contract:
// see https://github.com/openjdk/jdk/pull/2662
return null;
}
}
/**
* This implementation opens an {@link InputStream} for the underlying class
* path resource, if available.
* @see ClassLoader#getResourceAsStream(String)
* @see Class#getResourceAsStream(String)
* @see ClassLoader#getSystemResourceAsStream(String)
*/
@Override
public InputStream getInputStream() throws IOException {
InputStream is;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
}
else if (this.classLoader != null) {
is = this.classLoader.getResourceAsStream(this.absolutePath);
}
else {
is = ClassLoader.getSystemResourceAsStream(this.absolutePath);
}
if (is == null) {
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
}
return is;
}
/**
* This implementation returns a URL for the underlying class path resource,
* if available.
* @see ClassLoader#getResource(String)
* @see Class#getResource(String)
*/
@Override
public URL getURL() throws IOException {
URL url = resolveURL();
if (url == null) {
throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
}
/**
* This implementation creates a {@code ClassPathResource}, applying the given
* path relative to the path used to create this descriptor.
* @see StringUtils#applyRelativePath(String, String)
*/
@Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return (this.clazz != null ? new ClassPathResource(pathToUse, this.clazz) :
new ClassPathResource(pathToUse, this.classLoader));
}
/**
* This implementation returns the name of the file that this class path
* resource refers to.
* @see StringUtils#getFilename(String)
*/
@Override
@Nullable
public String getFilename() {
return StringUtils.getFilename(this.absolutePath);
}
/**
* This implementation returns a description that includes the absolute
* class path location.
*/
@Override
public String getDescription() {
return "class path resource [" + this.absolutePath + "]";
}
/**
* This implementation compares the underlying class path locations and
* associated class loaders.
* @see #getPath()
* @see #getClassLoader()
*/
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof ClassPathResource that &&
this.absolutePath.equals(that.absolutePath) &&
ObjectUtils.nullSafeEquals(getClassLoader(), that.getClassLoader())));
}
/**
* This implementation returns the hash code of the underlying class path location.
* @see #getPath()
*/
@Override
public int hashCode() {
return this.absolutePath.hashCode();
}
}