From 9e0c103e65285aacc2e2ae8bc84c30d53b776294 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 2 Dec 2014 12:32:17 -0700 Subject: [PATCH] add @EnableDiscoveryClient and update travis --- .travis.yml | 24 ++++---- .../discovery/EnableDiscoveryClient.java | 17 ++++++ .../EnableDiscoveryClientImportSelector.java | 60 +++++++++++++++++++ 3 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java create mode 100644 src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java diff --git a/.travis.yml b/.travis.yml index c538a813..e963f6bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,21 @@ language: java before_install: - - git config user.name "$GIT_NAME" - - git config user.email "$GIT_EMAIL" - - git config credential.helper "store --file=.git/credentials" - - echo "https://$GH_TOKEN:@github.com" > .git/credentials - - gem install asciidoctor +- git config user.name "$GIT_NAME" +- git config user.email "$GIT_EMAIL" +- git config credential.helper "store --file=.git/credentials" +- echo "https://$GH_TOKEN:@github.com" > .git/credentials +- gem install asciidoctor install: - mvn --settings .settings.xml install -P docs -q -U -DskipTests=true -Dmaven.test.redirectTestOutputToFile=true - ./docs/src/main/asciidoc/ghpages.sh script: -- '[ "${TRAVIS_PULL_REQUEST}" != "false" ] || mvn --settings .settings.xml deploy -nsu -Dmaven.test.redirectTestOutputToFile=true' -- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] || mvn --settings .settings.xml install -nsu -Dmaven.test.redirectTestOutputToFile=true' +- '[ "${TRAVIS_PULL_REQUEST}" != "false" ] || mvn --settings .settings.xml deploy + -nsu -Dmaven.test.redirectTestOutputToFile=true' +- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] || mvn --settings .settings.xml install + -nsu -Dmaven.test.redirectTestOutputToFile=true' env: global: - - GIT_NAME="Dave Syer" - - GIT_EMAIL=dsyer@pivotal.io - - CI_DEPLOY_USERNAME=buildmaster - - secure: aeLXRC5oFSddwnZt1/7G2/OHr7jDbxz0ET7sej3I+eSbe3N5vbzQ6FC08es4l89l54ciXd90I1g2BMw7DTYKOO373FP78XPdAEbifJTU4DGd6fCELmoTtUPhjunBIk7E49hisPbv82892IYYA7qi/hzG548cPyZ1IgiJjq0NCsc= + - GIT_NAME="Spencer Gibb" + - GIT_EMAIL=sgibb@pivotal.io + - CI_DEPLOY_USERNAME=sgibb + - secure: WPmrAQJQGBziwGFKwTiDRGrG1nuy9dvoWOQt7WKHW0RBqDbUpZgGeHq6OD2FcE86eRo+tPDihyunsWltei+sfq5bUECkGFQBLGffcTuxyu2j4lTByVazeYKa3en3j7nOiQjI99MqgCHrdyRwtS9EF1SnYXRuSZXwb0Ic8h4Zns4= diff --git a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java new file mode 100644 index 00000000..4b6ab16a --- /dev/null +++ b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClient.java @@ -0,0 +1,17 @@ +package org.springframework.cloud.client.discovery; + +/** + * @author Spencer Gibb + */ + +import java.lang.annotation.*; + +import org.springframework.context.annotation.Import; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@Import(EnableDiscoveryClientImportSelector.class) +public @interface EnableDiscoveryClient { +} diff --git a/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java new file mode 100644 index 00000000..10c8ed87 --- /dev/null +++ b/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java @@ -0,0 +1,60 @@ +package org.springframework.cloud.client.discovery; + +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.annotation.DeferredImportSelector; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.annotation.Order; +import org.springframework.core.io.support.SpringFactoriesLoader; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.Assert; + +/** + * @author Spencer Gibb + */ +@Order(Ordered.LOWEST_PRECEDENCE - 100) +@Slf4j +public class EnableDiscoveryClientImportSelector implements DeferredImportSelector, + BeanClassLoaderAware { + + private ClassLoader beanClassLoader; + + @Override + public String[] selectImports(AnnotationMetadata metadata) { + AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata + .getAnnotationAttributes(EnableDiscoveryClient.class.getName(), + true)); + + Assert.notNull(attributes, "No EnableDiscoveryClient attributes found. Is " + + metadata.getClassName() + + " annotated with @EnableDiscoveryClient?"); + + // Find all possible auto configuration classes, filtering duplicates + List factories = new ArrayList<>(new LinkedHashSet<>( + SpringFactoriesLoader.loadFactoryNames(EnableDiscoveryClient.class, + 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 @EnableDiscoveryClient. Using {} out of available {}", factory, factories); + factories = Collections.singletonList(factory); + } + + return factories.toArray(new String[factories.size()]); + + } + + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + +}