From 99302432e94400d6f1c8dee2e04085ee970a44ef Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Wed, 9 Jul 2014 19:06:07 -0500 Subject: [PATCH] pull up KeyValuePair class from HerokuConnector to AbstractCloudConnector --- .../cloud/AbstractCloudConnector.java | 34 ++++++++++--- .../cloud/heroku/HerokuConnector.java | 50 ++++++------------- .../heroku/HerokuServiceInfoCreator.java | 20 ++++---- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/AbstractCloudConnector.java b/spring-cloud-core/src/main/java/org/springframework/cloud/AbstractCloudConnector.java index 6a73afa..a515766 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/AbstractCloudConnector.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/AbstractCloudConnector.java @@ -10,25 +10,25 @@ import org.springframework.cloud.service.ServiceInfo; /** * Helper abstract class to simplify {@link CloudConnector} implementations. - * + * * User the {@link ServiceLoader} approach to looks for file name matching the class passed in constructor * and registers {@link ServiceInfoCreator} found there. - * + * * Implementation of {@link CloudConnector}s that wish to support the recommended service scanning approach * should extends this approach to gain that functionality automatically. - * + * * @author Ramnivas Laddad * */ public abstract class AbstractCloudConnector implements CloudConnector { private static Logger logger = Logger.getLogger(AbstractCloudConnector.class.getName()); - + protected List> serviceInfoCreators = new ArrayList>(); protected abstract List getServicesData(); protected abstract FallbackServiceInfoCreator getFallbackServiceInfoCreator(); - + public AbstractCloudConnector(Class> serviceInfoCreatorClass) { scanServiceInfoCreators(serviceInfoCreatorClass); } @@ -39,7 +39,7 @@ public abstract class AbstractCloudConnector implements CloudConnector { for (SD serviceData : getServicesData()) { serviceInfos.add(getServiceInfo(serviceData)); } - + return serviceInfos; } @@ -54,18 +54,36 @@ public abstract class AbstractCloudConnector implements CloudConnector { registerServiceInfoCreator(serviceInfoCreator); } } - + private ServiceInfo getServiceInfo(SD serviceData) { for (ServiceInfoCreator serviceInfoCreator : serviceInfoCreators) { if (serviceInfoCreator.accept(serviceData)) { return serviceInfoCreator.createServiceInfo(serviceData); } } - + // Fallback with a warning ServiceInfo fallackServiceInfo = getFallbackServiceInfoCreator().createServiceInfo(serviceData); logger.warning("No suitable service info creator found for service " + fallackServiceInfo.getId() + " Did you forget to add a ServiceInfoCreator?"); return fallackServiceInfo; } + + public static class KeyValuePair { + private final String key; + private final String value; + + public KeyValuePair(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + } } \ No newline at end of file diff --git a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java index 374b7f8..ac39931 100644 --- a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java +++ b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java @@ -6,30 +6,30 @@ import java.util.List; import java.util.Map; import org.springframework.cloud.AbstractCloudConnector; +import org.springframework.cloud.AbstractCloudConnector.KeyValuePair; import org.springframework.cloud.CloudException; import org.springframework.cloud.FallbackServiceInfoCreator; import org.springframework.cloud.ServiceInfoCreator; import org.springframework.cloud.app.ApplicationInstanceInfo; -import org.springframework.cloud.heroku.HerokuConnector.KeyValuePair; import org.springframework.cloud.service.BaseServiceInfo; import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.util.EnvironmentAccessor; /** * Implementation of CloudConnector for Heroku - * + * * Currently support Postgres (default provided), Mysql (Cleardb), MongoDb (MongoLab, MongoHQ, MongoSoup), * Redis (RedisToGo, RedisCloud, OpenRedis, RedisGreen), and AMQP (CloudAmqp). - * + * * @author Ramnivas Laddad * */ -public class HerokuConnector extends AbstractCloudConnector { +public class HerokuConnector extends AbstractCloudConnector { private EnvironmentAccessor environment = new EnvironmentAccessor(); - private ApplicationInstanceInfoCreator applicationInstanceInfoCreator + private ApplicationInstanceInfoCreator applicationInstanceInfoCreator = new ApplicationInstanceInfoCreator(environment); - + private List serviceEnvPrefixes; @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -41,28 +41,28 @@ public class HerokuConnector extends AbstractCloudConnector serviceInfoCreator) { + protected void registerServiceInfoCreator(ServiceInfoCreator serviceInfoCreator) { super.registerServiceInfoCreator(serviceInfoCreator); HerokuServiceInfoCreator herokuServiceInfoCreator = (HerokuServiceInfoCreator)serviceInfoCreator; String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes(); - + // need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this if (serviceEnvPrefixes == null) { serviceEnvPrefixes = new ArrayList(); @@ -75,17 +75,17 @@ public class HerokuConnector extends AbstractCloudConnector * Returns map whose key is the env key and value is the associated url *

- * @return information about services bound to the app + * @return information about services bound to the app */ protected List getServicesData() { List serviceData = new ArrayList(); - + Map env = environment.getEnv(); - + for (Map.Entry envEntry : env.entrySet()) { for (String envPrefix : serviceEnvPrefixes) { if (envEntry.getKey().startsWith(envPrefix)) { - serviceData.add(new KeyValuePair(envEntry.getKey(), envEntry.getValue())); + serviceData.add(new KeyValuePair(envEntry.getKey(), envEntry.getValue())); } } } @@ -97,24 +97,6 @@ public class HerokuConnector extends AbstractCloudConnector getFallbackServiceInfoCreator() { return new HerokuFallbackServiceInfoCreator(); } - - public static class KeyValuePair { - private String key; - private String value; - - public KeyValuePair(String key, String value) { - this.key = key; - this.value = value; - } - - public String getKey() { - return key; - } - - public String getValue() { - return value; - } - } } class HerokuFallbackServiceInfoCreator extends FallbackServiceInfoCreator { diff --git a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuServiceInfoCreator.java b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuServiceInfoCreator.java index 15ed4aa..00b6c6b 100644 --- a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuServiceInfoCreator.java +++ b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuServiceInfoCreator.java @@ -1,11 +1,11 @@ package org.springframework.cloud.heroku; +import org.springframework.cloud.AbstractCloudConnector.KeyValuePair; import org.springframework.cloud.ServiceInfoCreator; -import org.springframework.cloud.heroku.HerokuConnector.KeyValuePair; import org.springframework.cloud.service.ServiceInfo; /** - * + * * @author Ramnivas Laddad * */ @@ -16,25 +16,25 @@ public abstract class HerokuServiceInfoCreator implement public HerokuServiceInfoCreator(String urlProtocol) { this.urlProtocol = urlProtocol; } - + public boolean accept(KeyValuePair serviceData) { return serviceData.getValue().toString().startsWith(urlProtocol + "://"); } - + public abstract SI createServiceInfo(String id, String uri); - + public SI createServiceInfo(KeyValuePair serviceData) { return createServiceInfo(serviceData.getKey(), serviceData.getValue()); } - + /** * Get prefixes for env variable with which the associated {@link ServiceInfo} may be created. - * + * * Unlike CloudFoundry which exposes VCAP_SERVICES as a single environment to encompass all services bound * to the app, Heroku expose one environment variable per app. This method allows each info creator to declare - * appropriate env variables. - * - * @return prefixes for the relevant environment variables + * appropriate env variables. + * + * @return prefixes for the relevant environment variables */ public abstract String[] getEnvPrefixes(); }