Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4548af29
Commit
4548af29
authored
Jan 19, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x'
parents
5502fa29
531cf5d4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
15 deletions
+56
-15
ClassLoaderFilesResourcePatternResolver.java
...ools/restart/ClassLoaderFilesResourcePatternResolver.java
+30
-14
ClassLoaderFilesResourcePatternResolverTests.java
...restart/ClassLoaderFilesResourcePatternResolverTests.java
+26
-1
No files found.
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ClassLoaderFilesResourcePatternResolver.java
View file @
4548af29
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
7
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.
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.devtools.restart;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.lang.reflect.Field
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.ArrayList
;
...
...
@@ -40,6 +41,7 @@ import org.springframework.core.io.support.ResourcePatternResolver;
import
org.springframework.util.AntPathMatcher
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.PathMatcher
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.context.support.ServletContextResource
;
import
org.springframework.web.context.support.ServletContextResourcePatternResolver
;
...
...
@@ -59,7 +61,7 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
private
static
final
String
WEB_CONTEXT_CLASS
=
"org.springframework.web.context."
+
"WebApplicationContext"
;
private
final
ResourcePatternResolver
d
elegate
;
private
final
ResourcePatternResolver
patternResolverD
elegate
;
private
final
PathMatcher
antPathMatcher
=
new
AntPathMatcher
();
...
...
@@ -68,8 +70,19 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
ClassLoaderFilesResourcePatternResolver
(
ApplicationContext
applicationContext
,
ClassLoaderFiles
classLoaderFiles
)
{
this
.
classLoaderFiles
=
classLoaderFiles
;
this
.
delegate
=
getResourcePatternResolverFactory
()
.
getResourcePatternResolver
(
applicationContext
);
this
.
patternResolverDelegate
=
getResourcePatternResolverFactory
()
.
getResourcePatternResolver
(
applicationContext
,
retrieveResourceLoader
(
applicationContext
));
}
private
ResourceLoader
retrieveResourceLoader
(
ApplicationContext
applicationContext
)
{
Field
field
=
ReflectionUtils
.
findField
(
applicationContext
.
getClass
(),
"resourceLoader"
,
ResourceLoader
.
class
);
if
(
field
==
null
)
{
return
null
;
}
ReflectionUtils
.
makeAccessible
(
field
);
return
(
ResourceLoader
)
ReflectionUtils
.
getField
(
field
,
applicationContext
);
}
private
ResourcePatternResolverFactory
getResourcePatternResolverFactory
()
{
...
...
@@ -81,12 +94,12 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
@Override
public
ClassLoader
getClassLoader
()
{
return
this
.
d
elegate
.
getClassLoader
();
return
this
.
patternResolverD
elegate
.
getClassLoader
();
}
@Override
public
Resource
getResource
(
String
location
)
{
Resource
candidate
=
this
.
d
elegate
.
getResource
(
location
);
Resource
candidate
=
this
.
patternResolverD
elegate
.
getResource
(
location
);
if
(
isDeleted
(
candidate
))
{
return
new
DeletedClassLoaderFileResource
(
location
);
}
...
...
@@ -96,7 +109,8 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
@Override
public
Resource
[]
getResources
(
String
locationPattern
)
throws
IOException
{
List
<
Resource
>
resources
=
new
ArrayList
<
Resource
>();
Resource
[]
candidates
=
this
.
delegate
.
getResources
(
locationPattern
);
Resource
[]
candidates
=
this
.
patternResolverDelegate
.
getResources
(
locationPattern
);
for
(
Resource
candidate
:
candidates
)
{
if
(!
isDeleted
(
candidate
))
{
resources
.
add
(
candidate
);
...
...
@@ -190,8 +204,9 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
private
static
class
ResourcePatternResolverFactory
{
public
ResourcePatternResolver
getResourcePatternResolver
(
ApplicationContext
applicationContext
)
{
return
new
PathMatchingResourcePatternResolver
();
ApplicationContext
applicationContext
,
ResourceLoader
resourceLoader
)
{
return
new
PathMatchingResourcePatternResolver
(
resourceLoader
==
null
?
new
DefaultResourceLoader
()
:
resourceLoader
);
}
}
...
...
@@ -205,13 +220,14 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
@Override
public
ResourcePatternResolver
getResourcePatternResolver
(
ApplicationContext
applicationContext
)
{
ApplicationContext
applicationContext
,
ResourceLoader
resourceLoader
)
{
if
(
applicationContext
instanceof
WebApplicationContext
)
{
return
new
ServletContextResourcePatternResolver
(
new
WebApplicationContextResourceLoader
(
(
WebApplicationContext
)
applicationContext
));
return
new
ServletContextResourcePatternResolver
(
resourceLoader
==
null
?
new
WebApplicationContextResourceLoader
(
(
WebApplicationContext
)
applicationContext
)
:
resourceLoader
);
}
return
super
.
getResourcePatternResolver
(
applicationContext
);
return
super
.
getResourcePatternResolver
(
applicationContext
,
resourceLoader
);
}
}
...
...
spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ClassLoaderFilesResourcePatternResolverTests.java
View file @
4548af29
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
7
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.
...
...
@@ -31,17 +31,21 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
import
org.springframework.web.context.support.ServletContextResource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
/**
* Tests for {@link ClassLoaderFilesResourcePatternResolver}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public
class
ClassLoaderFilesResourcePatternResolverTests
{
...
...
@@ -111,6 +115,27 @@ public class ClassLoaderFilesResourcePatternResolverTests {
assertThat
(
resources
).
isEmpty
();
}
@Test
public
void
customResourceLoaderIsUsedInNonWebApplication
()
throws
Exception
{
GenericApplicationContext
context
=
new
GenericApplicationContext
();
ResourceLoader
resourceLoader
=
mock
(
ResourceLoader
.
class
);
context
.
setResourceLoader
(
resourceLoader
);
this
.
resolver
=
new
ClassLoaderFilesResourcePatternResolver
(
context
,
this
.
files
);
this
.
resolver
.
getResource
(
"foo.txt"
);
verify
(
resourceLoader
).
getResource
(
"foo.txt"
);
}
@Test
public
void
customResourceLoaderIsUsedInWebApplication
()
throws
Exception
{
GenericWebApplicationContext
context
=
new
GenericWebApplicationContext
(
new
MockServletContext
());
ResourceLoader
resourceLoader
=
mock
(
ResourceLoader
.
class
);
context
.
setResourceLoader
(
resourceLoader
);
this
.
resolver
=
new
ClassLoaderFilesResourcePatternResolver
(
context
,
this
.
files
);
this
.
resolver
.
getResource
(
"foo.txt"
);
verify
(
resourceLoader
).
getResource
(
"foo.txt"
);
}
private
File
createFile
(
File
folder
,
String
name
)
throws
IOException
{
File
file
=
new
File
(
folder
,
name
);
FileCopyUtils
.
copy
(
"test"
.
getBytes
(),
file
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment