17
17
package org .springframework .boot .autoconfigure ;
18
18
19
19
import java .io .IOException ;
20
+ import java .net .MalformedURLException ;
20
21
import java .net .URL ;
21
22
import java .net .URLClassLoader ;
22
23
import java .util .Arrays ;
40
41
import org .springframework .core .Ordered ;
41
42
import org .springframework .core .annotation .Order ;
42
43
import org .springframework .core .io .Resource ;
44
+ import org .springframework .core .io .UrlResource ;
43
45
import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
44
46
import org .springframework .core .type .AnnotatedTypeMetadata ;
47
+ import org .springframework .util .ResourceUtils ;
45
48
import org .springframework .util .StringUtils ;
46
49
47
50
import static org .springframework .util .StringUtils .commaDelimitedListToStringArray ;
@@ -146,6 +149,10 @@ private static class ExtendedPathMatchingResourcePatternResolver extends
146
149
private static final Log logger = LogFactory
147
150
.getLog (PathMatchingResourcePatternResolver .class );
148
151
152
+ private static final String JAR_FILE_EXTENSION = ".jar" ;
153
+
154
+ private static final String JAR_URL_PREFIX = "jar:" ;
155
+
149
156
public ExtendedPathMatchingResourcePatternResolver (ClassLoader classLoader ) {
150
157
super (classLoader );
151
158
}
@@ -160,42 +167,66 @@ protected Resource[] findAllClassPathResources(String location)
160
167
if ("" .equals (path )) {
161
168
Set <Resource > result = new LinkedHashSet <Resource >(16 );
162
169
result .addAll (Arrays .asList (super .findAllClassPathResources (location )));
163
- addAllClassLoaderJarUrls (getClassLoader (), result );
170
+ addAllClassLoaderJarRoots (getClassLoader (), result );
164
171
return result .toArray (new Resource [result .size ()]);
165
172
}
166
173
return super .findAllClassPathResources (location );
167
174
}
168
175
169
- private void addAllClassLoaderJarUrls (ClassLoader classLoader ,
176
+ private void addAllClassLoaderJarRoots (ClassLoader classLoader ,
170
177
Set <Resource > result ) {
171
178
if (classLoader != null ) {
172
179
if (classLoader instanceof URLClassLoader ) {
173
- addAllClassLoaderJarUrls (((URLClassLoader ) classLoader ).getURLs (),
174
- result );
180
+ try {
181
+ addAllClassLoaderJarUrls (
182
+ ((URLClassLoader ) classLoader ).getURLs (), result );
183
+ }
184
+ catch (Exception ex ) {
185
+ if (logger .isDebugEnabled ()) {
186
+ logger .debug ("Cannot introspect jar files since "
187
+ + "ClassLoader [" + classLoader
188
+ + "] does not support 'getURLs()': " + ex );
189
+ }
190
+ }
191
+ }
192
+ try {
193
+ addAllClassLoaderJarRoots (classLoader .getParent (), result );
194
+ }
195
+ catch (Exception ex ) {
196
+ if (logger .isDebugEnabled ()) {
197
+ logger .debug ("Cannot introspect jar files in parent "
198
+ + "ClassLoader since [" + classLoader
199
+ + "] does not support 'getParent()': " + ex );
200
+ }
175
201
}
176
- addAllClassLoaderJarUrls (classLoader .getParent (), result );
177
202
}
178
203
}
179
204
180
205
private void addAllClassLoaderJarUrls (URL [] urls , Set <Resource > result ) {
181
206
for (URL url : urls ) {
182
- if ("file" .equals (url .getProtocol ())
183
- && url .toString ().toLowerCase ().endsWith (".jar" )) {
207
+ if (isJarFileUrl (url )) {
184
208
try {
185
- URL jarUrl = new URL ("jar:" + url .toString () + "!/" );
186
- jarUrl .openConnection ();
187
- result .add (convertClassLoaderURL (jarUrl ));
209
+ UrlResource jarResource = new UrlResource (JAR_URL_PREFIX
210
+ + url .toString () + ResourceUtils .JAR_URL_SEPARATOR );
211
+ if (jarResource .exists ()) {
212
+ result .add (jarResource );
213
+ }
188
214
}
189
- catch (Exception ex ) {
190
- if (logger .isWarnEnabled ()) {
191
- logger .warn ("Cannot search for matching files underneath "
215
+ catch (MalformedURLException ex ) {
216
+ if (logger .isDebugEnabled ()) {
217
+ logger .debug ("Cannot search for matching files underneath "
192
218
+ url + " because it cannot be accessed as a JAR" , ex );
193
219
}
194
220
}
195
221
}
196
222
}
197
223
}
198
224
225
+ private boolean isJarFileUrl (URL url ) {
226
+ return ResourceUtils .URL_PROTOCOL_FILE .equals (url .getProtocol ())
227
+ && url .getPath ().toLowerCase ().endsWith (JAR_FILE_EXTENSION );
228
+ }
229
+
199
230
}
200
231
201
232
}
0 commit comments