Encapsulate some fields into protected methods

This commit is contained in:
Dave Syer
2015-01-02 08:59:45 +00:00
parent d3c2f89049
commit cfa00e2801
4 changed files with 67 additions and 51 deletions

View File

@@ -1,6 +1,12 @@
package org.springframework.cloud.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.DeferredImportSelector;
@@ -11,69 +17,76 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Selects a single configuration to load defined by the generic type T.
* @author Spencer Gibb
* @author Dave Syer
*/
@Slf4j
public abstract class SingleImplementationImportSelector<T> implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
public abstract class SingleImplementationImportSelector<T> implements
DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
protected ClassLoader beanClassLoader;
private ClassLoader beanClassLoader;
protected Class<T> annotationClass;
protected Environment environment;
private Class<T> annotationClass;
private Environment environment;
@SuppressWarnings("unchecked")
protected SingleImplementationImportSelector() {
annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(this.getClass(), SingleImplementationImportSelector.class);
}
@SuppressWarnings("unchecked")
protected SingleImplementationImportSelector() {
annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(
this.getClass(), SingleImplementationImportSelector.class);
}
@Override
public String[] selectImports(AnnotationMetadata metadata) {
if (!isEnabled()) {
return new String[0];
}
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(annotationClass.getName(),
true));
@Override
public String[] selectImports(AnnotationMetadata metadata) {
if (!isEnabled()) {
return new String[0];
}
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(annotationClass.getName(), true));
Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is "
+ metadata.getClassName() + " annotated with @" + getSimpleName() + "?");
Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is "
+ metadata.getClassName() + " annotated with @" + getSimpleName() + "?");
// Find all possible auto configuration classes, filtering duplicates
List<String> factories = new ArrayList<>(new LinkedHashSet<>(
SpringFactoriesLoader.loadFactoryNames(annotationClass,
this.beanClassLoader)));
// Find all possible auto configuration classes, filtering duplicates
List<String> factories = new ArrayList<>(new LinkedHashSet<>(
SpringFactoriesLoader.loadFactoryNames(annotationClass,
this.beanClassLoader)));
if (factories.size() > 1) {
String factory = factories.get(0);
//there should only every be one DiscoveryClient
log.warn("More than one implementation of @{}. Using {} out of available {}", getSimpleName(), factory, factories);
factories = Collections.singletonList(factory);
}
if (factories.size() > 1) {
String factory = factories.get(0);
// there should only every be one DiscoveryClient
log.warn(
"More than one implementation of @{}. Using {} out of available {}",
getSimpleName(), factory, factories);
factories = Collections.singletonList(factory);
}
return factories.toArray(new String[factories.size()]);
return factories.toArray(new String[factories.size()]);
}
}
protected abstract boolean isEnabled();
protected abstract boolean isEnabled();
protected String getSimpleName() {
return annotationClass.getSimpleName();
}
protected String getSimpleName() {
return annotationClass.getSimpleName();
}
protected Class<T> getAnnotationClass() {
return annotationClass;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
protected Environment getEnvironment() {
return environment;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
}

View File

@@ -13,6 +13,6 @@ public class EnableCircuitBreakerImportSelector extends SingleImplementationImpo
@Override
protected boolean isEnabled() {
return new RelaxedPropertyResolver(environment).getProperty("spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);
return new RelaxedPropertyResolver(getEnvironment()).getProperty("spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE);
}
}

View File

@@ -9,10 +9,12 @@ import org.springframework.core.annotation.Order;
* @author Spencer Gibb
*/
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableDiscoveryClientImportSelector extends SingleImplementationImportSelector<EnableDiscoveryClient> {
public class EnableDiscoveryClientImportSelector extends
SingleImplementationImportSelector<EnableDiscoveryClient> {
@Override
protected boolean isEnabled() {
return new RelaxedPropertyResolver(environment).getProperty("spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
return new RelaxedPropertyResolver(getEnvironment()).getProperty(
"spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
}
}

View File

@@ -12,7 +12,7 @@ public class SingleImplementationImportSelectorTests {
@Test
public void testFindAnnotation() {
MyAnnotationImportSelector selector = new MyAnnotationImportSelector();
assertEquals("annotationClass was wrong", MyAnnotation.class, selector.annotationClass);
assertEquals("annotationClass was wrong", MyAnnotation.class, selector.getAnnotationClass());
}
public static @interface MyAnnotation {}
@@ -21,5 +21,6 @@ public class SingleImplementationImportSelectorTests {
protected boolean isEnabled() {
return true;
}
}
}