1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -50,9 +50,9 @@ public class ClassPathResource extends AbstractFileResolvingResource {
50
50
51
51
52
52
/**
53
- * Create a new ClassPathResource for ClassLoader usage.
54
- * A leading slash will be removed, as the ClassLoader
55
- * resource access methods will not accept it.
53
+ * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
54
+ * A leading slash will be removed, as the ClassLoader resource access
55
+ * methods will not accept it.
56
56
* <p>The thread context class loader will be used for
57
57
* loading the resource.
58
58
* @param path the absolute path within the class path
@@ -64,9 +64,9 @@ public ClassPathResource(String path) {
64
64
}
65
65
66
66
/**
67
- * Create a new ClassPathResource for ClassLoader usage.
68
- * A leading slash will be removed, as the ClassLoader
69
- * resource access methods will not accept it.
67
+ * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
68
+ * A leading slash will be removed, as the ClassLoader resource access
69
+ * methods will not accept it.
70
70
* @param path the absolute path within the classpath
71
71
* @param classLoader the class loader to load the resource with,
72
72
* or {@code null} for the thread context class loader
@@ -83,9 +83,9 @@ public ClassPathResource(String path, ClassLoader classLoader) {
83
83
}
84
84
85
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.
86
+ * Create a new {@code ClassPathResource} for {@code Class} usage.
87
+ * The path can be relative to the given class, or absolute within
88
+ * the classpath via a leading slash.
89
89
* @param path relative or absolute path within the class path
90
90
* @param clazz the class to load resources with
91
91
* @see java.lang.Class#getResourceAsStream
@@ -97,8 +97,8 @@ public ClassPathResource(String path, Class<?> clazz) {
97
97
}
98
98
99
99
/**
100
- * Create a new ClassPathResource with optional ClassLoader and Class.
101
- * Only for internal usage.
100
+ * Create a new {@code ClassPathResource} with optional {@code ClassLoader}
101
+ * and {@code Class}. Only for internal usage.
102
102
* @param path relative or absolute path within the classpath
103
103
* @param classLoader the class loader to load the resource with, if any
104
104
* @param clazz the class to load resources with, if any
@@ -109,6 +109,7 @@ protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz
109
109
this .clazz = clazz ;
110
110
}
111
111
112
+
112
113
/**
113
114
* Return the path for this resource (as resource path within the class path).
114
115
*/
@@ -120,24 +121,34 @@ public final String getPath() {
120
121
* Return the ClassLoader that this resource will be obtained from.
121
122
*/
122
123
public final ClassLoader getClassLoader () {
123
- return (this .classLoader != null ? this .classLoader : this . clazz .getClassLoader ());
124
+ return (this .clazz != null ? this .clazz .getClassLoader () : this . classLoader );
124
125
}
125
126
127
+
126
128
/**
127
129
* This implementation checks for the resolution of a resource URL.
128
130
* @see java.lang.ClassLoader#getResource(String)
129
131
* @see java.lang.Class#getResource(String)
130
132
*/
131
133
@ Override
132
134
public boolean exists () {
133
- URL url ;
135
+ return (resolveURL () != null );
136
+ }
137
+
138
+ /**
139
+ * Resolves a URL for the underlying class path resource.
140
+ * @return the resolved URL, or {@code null} if not resolvable
141
+ */
142
+ protected URL resolveURL () {
134
143
if (this .clazz != null ) {
135
- url = this .clazz .getResource (this .path );
144
+ return this .clazz .getResource (this .path );
145
+ }
146
+ else if (this .classLoader != null ) {
147
+ return this .classLoader .getResource (this .path );
136
148
}
137
149
else {
138
- url = this . classLoader . getResource (this .path );
150
+ return ClassLoader . getSystemResource (this .path );
139
151
}
140
- return (url != null );
141
152
}
142
153
143
154
/**
@@ -151,29 +162,27 @@ public InputStream getInputStream() throws IOException {
151
162
if (this .clazz != null ) {
152
163
is = this .clazz .getResourceAsStream (this .path );
153
164
}
154
- else {
165
+ else if ( this . classLoader != null ) {
155
166
is = this .classLoader .getResourceAsStream (this .path );
156
167
}
168
+ else {
169
+ is = ClassLoader .getSystemResourceAsStream (this .path );
170
+ }
157
171
if (is == null ) {
158
172
throw new FileNotFoundException (getDescription () + " cannot be opened because it does not exist" );
159
173
}
160
174
return is ;
161
175
}
162
176
163
177
/**
164
- * This implementation returns a URL for the underlying class path resource.
178
+ * This implementation returns a URL for the underlying class path resource,
179
+ * if available.
165
180
* @see java.lang.ClassLoader#getResource(String)
166
181
* @see java.lang.Class#getResource(String)
167
182
*/
168
183
@ Override
169
184
public URL getURL () throws IOException {
170
- URL url ;
171
- if (this .clazz != null ) {
172
- url = this .clazz .getResource (this .path );
173
- }
174
- else {
175
- url = this .classLoader .getResource (this .path );
176
- }
185
+ URL url = resolveURL ();
177
186
if (url == null ) {
178
187
throw new FileNotFoundException (getDescription () + " cannot be resolved to URL because it does not exist" );
179
188
}
0 commit comments