1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.core.io;
18
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23
24 import org.springframework.util.Assert;
25 import org.springframework.util.ClassUtils;
26 import org.springframework.util.ObjectUtils;
27 import org.springframework.util.StringUtils;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ClassPathResource extends AbstractFileResolvingResource {
44
45 private final String path;
46
47 private ClassLoader classLoader;
48
49 private Class<?> clazz;
50
51
52
53
54
55
56
57
58
59
60
61
62 public ClassPathResource(String path) {
63 this(path, (ClassLoader) null);
64 }
65
66
67
68
69
70
71
72
73
74
75 public ClassPathResource(String path, ClassLoader classLoader) {
76 Assert.notNull(path, "Path must not be null");
77 String pathToUse = StringUtils.cleanPath(path);
78 if (pathToUse.startsWith("/")) {
79 pathToUse = pathToUse.substring(1);
80 }
81 this.path = pathToUse;
82 this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
83 }
84
85
86
87
88
89
90
91
92
93 public ClassPathResource(String path, Class<?> clazz) {
94 Assert.notNull(path, "Path must not be null");
95 this.path = StringUtils.cleanPath(path);
96 this.clazz = clazz;
97 }
98
99
100
101
102
103
104
105
106 protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
107 this.path = StringUtils.cleanPath(path);
108 this.classLoader = classLoader;
109 this.clazz = clazz;
110 }
111
112
113
114
115 public final String getPath() {
116 return this.path;
117 }
118
119
120
121
122 public final ClassLoader getClassLoader() {
123 return (this.classLoader != null ? this.classLoader : this.clazz.getClassLoader());
124 }
125
126
127
128
129
130
131 @Override
132 public boolean exists() {
133 URL url;
134 if (this.clazz != null) {
135 url = this.clazz.getResource(this.path);
136 }
137 else {
138 url = this.classLoader.getResource(this.path);
139 }
140 return (url != null);
141 }
142
143
144
145
146
147
148 public InputStream getInputStream() throws IOException {
149 InputStream is;
150 if (this.clazz != null) {
151 is = this.clazz.getResourceAsStream(this.path);
152 }
153 else {
154 is = this.classLoader.getResourceAsStream(this.path);
155 }
156 if (is == null) {
157 throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
158 }
159 return is;
160 }
161
162
163
164
165
166
167 @Override
168 public URL getURL() throws IOException {
169 URL url;
170 if (this.clazz != null) {
171 url = this.clazz.getResource(this.path);
172 }
173 else {
174 url = this.classLoader.getResource(this.path);
175 }
176 if (url == null) {
177 throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
178 }
179 return url;
180 }
181
182
183
184
185
186
187 @Override
188 public Resource createRelative(String relativePath) {
189 String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
190 return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
191 }
192
193
194
195
196
197
198 @Override
199 public String getFilename() {
200 return StringUtils.getFilename(this.path);
201 }
202
203
204
205
206 public String getDescription() {
207 StringBuilder builder = new StringBuilder("class path resource [");
208
209 String pathToUse = path;
210
211 if (this.clazz != null && !pathToUse.startsWith("/")) {
212 builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
213 builder.append('/');
214 }
215
216 if (pathToUse.startsWith("/")) {
217 pathToUse = pathToUse.substring(1);
218 }
219
220 builder.append(pathToUse);
221 builder.append(']');
222 return builder.toString();
223 }
224
225
226
227
228 @Override
229 public boolean equals(Object obj) {
230 if (obj == this) {
231 return true;
232 }
233 if (obj instanceof ClassPathResource) {
234 ClassPathResource otherRes = (ClassPathResource) obj;
235 return (this.path.equals(otherRes.path)
236 && ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && ObjectUtils.nullSafeEquals(
237 this.clazz, otherRes.clazz));
238 }
239 return false;
240 }
241
242
243
244
245
246 @Override
247 public int hashCode() {
248 return this.path.hashCode();
249 }
250
251 }