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 @@
package org.springframework.cloud.client; 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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.DeferredImportSelector; 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.core.type.AnnotationMetadata;
import org.springframework.util.Assert; 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. * Selects a single configuration to load defined by the generic type T.
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer
*/ */
@Slf4j @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() { @SuppressWarnings("unchecked")
annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(this.getClass(), SingleImplementationImportSelector.class); protected SingleImplementationImportSelector() {
} annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(
this.getClass(), SingleImplementationImportSelector.class);
@Override }
public String[] selectImports(AnnotationMetadata metadata) {
if (!isEnabled()) { @Override
return new String[0]; public String[] selectImports(AnnotationMetadata metadata) {
} if (!isEnabled()) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata return new String[0];
.getAnnotationAttributes(annotationClass.getName(), }
true)); 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<>( // Find all possible auto configuration classes, filtering duplicates
SpringFactoriesLoader.loadFactoryNames(annotationClass, List<String> factories = new ArrayList<>(new LinkedHashSet<>(
this.beanClassLoader))); SpringFactoriesLoader.loadFactoryNames(annotationClass,
this.beanClassLoader)));
if (factories.size() > 1) {
String factory = factories.get(0); if (factories.size() > 1) {
//there should only every be one DiscoveryClient String factory = factories.get(0);
log.warn("More than one implementation of @{}. Using {} out of available {}", getSimpleName(), factory, factories); // there should only every be one DiscoveryClient
factories = Collections.singletonList(factory); log.warn(
} "More than one implementation of @{}. Using {} out of available {}",
getSimpleName(), factory, factories);
return factories.toArray(new String[factories.size()]); factories = Collections.singletonList(factory);
}
}
return factories.toArray(new String[factories.size()]);
protected abstract boolean isEnabled();
}
protected String getSimpleName() {
return annotationClass.getSimpleName(); protected abstract boolean isEnabled();
}
protected String getSimpleName() {
@Override return annotationClass.getSimpleName();
public void setEnvironment(Environment environment) { }
this.environment = environment;
} protected Class<T> getAnnotationClass() {
return annotationClass;
@Override }
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader; 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
@Override @Override
protected boolean isEnabled() { 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;
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Order(Ordered.LOWEST_PRECEDENCE - 100) @Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableDiscoveryClientImportSelector extends SingleImplementationImportSelector<EnableDiscoveryClient> { public class EnableDiscoveryClientImportSelector extends
SingleImplementationImportSelector<EnableDiscoveryClient> {
@Override @Override
protected boolean isEnabled() { 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 {
@Test @Test
public void testFindAnnotation() { public void testFindAnnotation() {
MyAnnotationImportSelector selector = new MyAnnotationImportSelector(); MyAnnotationImportSelector selector = new MyAnnotationImportSelector();
assertEquals("annotationClass was wrong", MyAnnotation.class, selector.annotationClass); assertEquals("annotationClass was wrong", MyAnnotation.class, selector.getAnnotationClass());
} }
public static @interface MyAnnotation {} public static @interface MyAnnotation {}
@ -21,5 +21,6 @@ public class SingleImplementationImportSelectorTests {
protected boolean isEnabled() { protected boolean isEnabled() {
return true; return true;
} }
} }
} }

Loading…
Cancel
Save