Browse Source

Encapsulate some fields into protected methods

pull/5/head
Dave Syer 10 years ago
parent
commit
cfa00e2801
  1. 133
      src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java
  2. 2
      src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java
  3. 6
      src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java
  4. 3
      src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java

133
src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java

@ -1,6 +1,12 @@ @@ -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; @@ -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 {
protected ClassLoader beanClassLoader;
protected Class<T> annotationClass;
protected Environment environment;
@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));
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)));
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()]);
}
protected abstract boolean isEnabled();
protected String getSimpleName() {
return annotationClass.getSimpleName();
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
public abstract class SingleImplementationImportSelector<T> implements
DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
private ClassLoader beanClassLoader;
private Class<T> annotationClass;
private Environment environment;
@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));
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)));
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()]);
}
protected abstract boolean isEnabled();
protected String getSimpleName() {
return annotationClass.getSimpleName();
}
protected Class<T> getAnnotationClass() {
return annotationClass;
}
protected Environment getEnvironment() {
return environment;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
}

2
src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java

@ -13,6 +13,6 @@ public class EnableCircuitBreakerImportSelector extends SingleImplementationImpo @@ -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);
}
}

6
src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java

@ -9,10 +9,12 @@ import org.springframework.core.annotation.Order; @@ -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);
}
}

3
src/test/java/org/springframework/cloud/client/SingleImplementationImportSelectorTests.java

@ -12,7 +12,7 @@ public class SingleImplementationImportSelectorTests { @@ -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 { @@ -21,5 +21,6 @@ public class SingleImplementationImportSelectorTests {
protected boolean isEnabled() {
return true;
}
}
}

Loading…
Cancel
Save