DATACMNS-993 - Polishing.

Removed custom JUnit integration as we can just create HidingClassLoader instances in the test and the integration actually causes more code being needed (additional JUnit rule, method level annotations etc.).

Tweaked ShadowingClassLoader to make obvious what has been changed over the Spring Framework variant. Created upstream ticket [0] to ask for the tweaks that would allow us to remove the class again.

Changed SpringDataWebConfigurationImportSelector to be lenient against the ResourceLoader not being available.

Original pull request: #202.
Related ticket: SPR-15439
[0] https://jira.spring.io/browse/SPR-15439
This commit is contained in:
Oliver Gierke
2017-04-12 07:47:32 +02:00
parent 65734739bd
commit 9d315af70a
7 changed files with 97 additions and 272 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2017 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.
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Import;
@@ -82,17 +83,7 @@ public @interface EnableSpringDataWebSupport {
*/
static class SpringDataWebConfigurationImportSelector implements ImportSelector, ResourceLoaderAware {
private Environment environment;
private ResourceLoader resourceLoader;
/*
* (non-Javadoc)
* @see org.springframework.context.EnvironmentAware#setEnvironment(org.springframework.core.env.Environment)
*/
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
private Optional<ClassLoader> resourceLoader = Optional.empty();
/*
* (non-Javadoc)
@@ -100,7 +91,7 @@ public @interface EnableSpringDataWebSupport {
*/
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
this.resourceLoader = Optional.of(resourceLoader).map(ResourceLoader::getClassLoader);
}
/*
@@ -112,14 +103,15 @@ public @interface EnableSpringDataWebSupport {
List<String> imports = new ArrayList<>();
imports.add(ClassUtils.isPresent("org.springframework.hateoas.Link", resourceLoader.getClassLoader())
? HateoasAwareSpringDataWebConfiguration.class.getName()
: SpringDataWebConfiguration.class.getName());
imports.add(resourceLoader//
.filter(it -> ClassUtils.isPresent("org.springframework.hateoas.Link", it))//
.map(it -> HateoasAwareSpringDataWebConfiguration.class.getName())//
.orElseGet(() -> SpringDataWebConfiguration.class.getName()));
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", resourceLoader.getClassLoader())) {
imports.addAll(
SpringFactoriesLoader.loadFactoryNames(SpringDataJacksonModules.class, resourceLoader.getClassLoader()));
}
resourceLoader//
.filter(it -> ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", it))//
.map(it -> SpringFactoriesLoader.loadFactoryNames(SpringDataJacksonModules.class, it))//
.ifPresent(it -> imports.addAll(it));
return imports.toArray(new String[imports.size()]);
}