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
97d7ffd8
Commit
97d7ffd8
authored
Dec 28, 2016
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.4.x' into 1.5.x
parents
8b705571
61c93194
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
213 additions
and
6 deletions
+213
-6
ClassLoaderFilesResourcePatternResolver.java
...ools/restart/ClassLoaderFilesResourcePatternResolver.java
+82
-4
Restarter.java
.../org/springframework/boot/devtools/restart/Restarter.java
+4
-2
ClassLoaderFilesResourcePatternResolverTests.java
...restart/ClassLoaderFilesResourcePatternResolverTests.java
+120
-0
pom.xml
spring-boot-samples/spring-boot-sample-war/pom.xml
+7
-0
No files found.
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ClassLoaderFilesResourcePatternResolver.java
View file @
97d7ffd8
...
...
@@ -29,32 +29,54 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kin
import
org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler
;
import
org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles
;
import
org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.core.io.AbstractResource
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.core.io.support.PathMatchingResourcePatternResolver
;
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.web.context.WebApplicationContext
;
import
org.springframework.web.context.support.ServletContextResource
;
import
org.springframework.web.context.support.ServletContextResourcePatternResolver
;
/**
* A {@code ResourcePatternResolver} that considers {@link ClassLoaderFiles} when
* resolving resources.
*
* @author Andy Wilkinson
* @author Phillip Webb
*/
final
class
ClassLoaderFilesResourcePatternResolver
implements
ResourcePatternResolver
{
private
static
final
String
[]
LOCATION_PATTERN_PREFIXES
=
{
CLASSPATH_ALL_URL_PREFIX
,
CLASSPATH_URL_PREFIX
};
private
final
ResourcePatternResolver
delegate
=
new
PathMatchingResourcePatternResolver
();
private
static
final
String
WEB_CONTEXT_CLASS
=
"org.springframework.web.context."
+
"WebApplicationContext"
;
private
final
AntPathMatcher
antPathMatcher
=
new
AntPathMatcher
();
private
final
ResourcePatternResolver
delegate
;
private
final
PathMatcher
antPathMatcher
=
new
AntPathMatcher
();
private
final
ClassLoaderFiles
classLoaderFiles
;
ClassLoaderFilesResourcePatternResolver
(
ClassLoaderFiles
classLoaderFiles
)
{
ClassLoaderFilesResourcePatternResolver
(
ApplicationContext
applicationContext
,
ClassLoaderFiles
classLoaderFiles
)
{
this
.
classLoaderFiles
=
classLoaderFiles
;
this
.
delegate
=
getResourcePatternResolverFactory
()
.
getResourcePatternResolver
(
applicationContext
);
}
private
ResourcePatternResolverFactory
getResourcePatternResolverFactory
()
{
if
(
ClassUtils
.
isPresent
(
WEB_CONTEXT_CLASS
,
null
))
{
return
new
WebResourcePatternResolverFactory
();
}
return
new
ResourcePatternResolverFactory
();
}
@Override
...
...
@@ -137,7 +159,7 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
* A {@link Resource} that represents a {@link ClassLoaderFile} that has been
* {@link Kind#DELETED deleted}.
*/
private
final
class
DeletedClassLoaderFileResource
extends
AbstractResource
{
static
final
class
DeletedClassLoaderFileResource
extends
AbstractResource
{
private
final
String
name
;
...
...
@@ -162,4 +184,60 @@ final class ClassLoaderFilesResourcePatternResolver implements ResourcePatternRe
}
/**
* Factory used to create the {@link ResourcePatternResolver} delegate.
*/
private
static
class
ResourcePatternResolverFactory
{
public
ResourcePatternResolver
getResourcePatternResolver
(
ApplicationContext
applicationContext
)
{
return
new
PathMatchingResourcePatternResolver
();
}
}
/**
* {@link ResourcePatternResolverFactory} to be used when the classloader can access
* {@link WebApplicationContext}.
*/
private
static
class
WebResourcePatternResolverFactory
extends
ResourcePatternResolverFactory
{
@Override
public
ResourcePatternResolver
getResourcePatternResolver
(
ApplicationContext
applicationContext
)
{
if
(
applicationContext
instanceof
WebApplicationContext
)
{
return
new
ServletContextResourcePatternResolver
(
new
WebApplicationContextResourceLoader
(
(
WebApplicationContext
)
applicationContext
));
}
return
super
.
getResourcePatternResolver
(
applicationContext
);
}
}
/**
* {@link ResourceLoader} that optionally supports {@link ServletContextResource
* ServletContextResources}.
*/
private
static
class
WebApplicationContextResourceLoader
extends
DefaultResourceLoader
{
private
final
WebApplicationContext
applicationContext
;
WebApplicationContextResourceLoader
(
WebApplicationContext
applicationContext
)
{
this
.
applicationContext
=
applicationContext
;
}
@Override
protected
Resource
getResourceByPath
(
String
path
)
{
if
(
this
.
applicationContext
.
getServletContext
()
!=
null
)
{
return
new
ServletContextResource
(
this
.
applicationContext
.
getServletContext
(),
path
);
}
return
super
.
getResourceByPath
(
path
);
}
}
}
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java
View file @
97d7ffd8
...
...
@@ -52,6 +52,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ReflectionUtils
;
...
...
@@ -433,8 +434,9 @@ public class Restarter {
}
private
void
prepare
(
GenericApplicationContext
applicationContext
)
{
applicationContext
.
setResourceLoader
(
new
ClassLoaderFilesResourcePatternResolver
(
this
.
classLoaderFiles
));
ResourceLoader
resourceLoader
=
new
ClassLoaderFilesResourcePatternResolver
(
applicationContext
,
this
.
classLoaderFiles
);
applicationContext
.
setResourceLoader
(
resourceLoader
);
}
private
LeakSafeThread
getLeakSafeThread
()
{
...
...
spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/ClassLoaderFilesResourcePatternResolverTests.java
0 → 100644
View file @
97d7ffd8
/*
* Copyright 2012-2016 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
*
* http://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
.
boot
.
devtools
.
restart
;
import
java.io.File
;
import
java.io.IOException
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.DeletedClassLoaderFileResource
;
import
org.springframework.boot.devtools.restart.classloader.ClassLoaderFile
;
import
org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind
;
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.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
;
/**
* Tests for {@link ClassLoaderFilesResourcePatternResolver}.
*
* @author Phillip Webb
*/
public
class
ClassLoaderFilesResourcePatternResolverTests
{
@Rule
public
TemporaryFolder
temp
=
new
TemporaryFolder
();
private
ClassLoaderFiles
files
;
private
ClassLoaderFilesResourcePatternResolver
resolver
;
@Before
public
void
setup
()
{
this
.
files
=
new
ClassLoaderFiles
();
this
.
resolver
=
new
ClassLoaderFilesResourcePatternResolver
(
new
GenericApplicationContext
(),
this
.
files
);
}
@Test
public
void
getClassLoaderShouldReturnClassLoader
()
throws
Exception
{
assertThat
(
this
.
resolver
.
getClassLoader
()).
isNotNull
();
}
@Test
public
void
getResourceShouldReturnResource
()
throws
Exception
{
Resource
resource
=
this
.
resolver
.
getResource
(
"index.html"
);
assertThat
(
resource
).
isNotNull
().
isInstanceOf
(
ClassPathResource
.
class
);
}
@Test
public
void
getResourceWhenHasServeletContextShouldReturnServletResource
()
throws
Exception
{
GenericWebApplicationContext
context
=
new
GenericWebApplicationContext
(
new
MockServletContext
());
this
.
resolver
=
new
ClassLoaderFilesResourcePatternResolver
(
context
,
this
.
files
);
Resource
resource
=
this
.
resolver
.
getResource
(
"index.html"
);
assertThat
(
resource
).
isNotNull
().
isInstanceOf
(
ServletContextResource
.
class
);
}
@Test
public
void
getResourceWhenDeletedShouldReturnDeletedResource
()
throws
Exception
{
File
folder
=
this
.
temp
.
newFolder
();
File
file
=
createFile
(
folder
,
"name.class"
);
this
.
files
.
addFile
(
folder
.
getName
(),
"name.class"
,
new
ClassLoaderFile
(
Kind
.
DELETED
,
null
));
Resource
resource
=
this
.
resolver
.
getResource
(
"file:"
+
file
.
getAbsolutePath
());
assertThat
(
resource
).
isNotNull
()
.
isInstanceOf
(
DeletedClassLoaderFileResource
.
class
);
}
@Test
public
void
getResourcesShouldReturnResources
()
throws
Exception
{
File
folder
=
this
.
temp
.
newFolder
();
createFile
(
folder
,
"name.class"
);
Resource
[]
resources
=
this
.
resolver
.
getResources
(
"file:"
+
folder
.
getAbsolutePath
()
+
"/**"
);
assertThat
(
resources
).
isNotEmpty
();
}
@Test
public
void
getResourcesWhenDeletedShouldFilterDeleted
()
throws
Exception
{
File
folder
=
this
.
temp
.
newFolder
();
createFile
(
folder
,
"name.class"
);
this
.
files
.
addFile
(
folder
.
getName
(),
"name.class"
,
new
ClassLoaderFile
(
Kind
.
DELETED
,
null
));
Resource
[]
resources
=
this
.
resolver
.
getResources
(
"file:"
+
folder
.
getAbsolutePath
()
+
"/**"
);
assertThat
(
resources
).
isEmpty
();
}
private
File
createFile
(
File
folder
,
String
name
)
throws
IOException
{
File
file
=
new
File
(
folder
,
name
);
FileCopyUtils
.
copy
(
"test"
.
getBytes
(),
file
);
return
file
;
}
}
spring-boot-samples/spring-boot-sample-war/pom.xml
View file @
97d7ffd8
...
...
@@ -42,6 +42,13 @@
</exclusion>
</exclusions>
</dependency>
<!-- Provided -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<scope>
provided
</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
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