From 3592f34bf3ea9091e5fbe052451d248d930ddd66 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 27 May 2015 09:50:09 -0500 Subject: [PATCH] Use tabs for indentation consistently. --- .../java/org/springframework/cloud/Cloud.java | 584 +++++++++--------- .../springframework/cloud/CloudFactory.java | 128 ++-- .../cloud/FallbackServiceInfoCreator.java | 21 +- .../service/BaseCompositeServiceInfo.java | 20 +- .../cloud/service/CompositeServiceInfo.java | 4 +- .../FallbackBaseServiceInfoCreator.java | 8 +- .../service/ServiceConnectorCreator.java | 50 +- .../service/common/MongoServiceInfo.java | 2 +- .../service/common/MysqlServiceInfo.java | 10 +- .../service/common/OracleServiceInfo.java | 4 +- .../service/common/PostgresqlServiceInfo.java | 4 +- .../service/common/RedisServiceInfo.java | 6 +- .../ServiceLoaderWithExceptionControl.java | 81 ++- .../cloud/heroku/AmqpServiceInfoCreator.java | 8 +- .../cloud/heroku/HerokuUtil.java | 22 +- .../cloud/heroku/MongoServiceInfoCreator.java | 8 +- .../cloud/heroku/MysqlServiceInfoCreator.java | 8 +- .../heroku/PostgresqlServiceInfoCreator.java | 8 +- .../cloud/heroku/RedisServiceInfoCreator.java | 8 +- ...tHerokuConnectorRelationalServiceTest.java | 50 +- .../heroku/AbstractHerokuConnectorTest.java | 4 +- .../HerokuConnectorAmqpServiceTest.java | 64 +- .../HerokuConnectorApplicationTest.java | 14 +- .../HerokuConnectorMongoServiceTest.java | 70 +-- .../HerokuConnectorMysqlServiceTest.java | 32 +- .../HerokuConnectorPostgresqlServiceTest.java | 32 +- .../HerokuConnectorRedisServiceTest.java | 64 +- .../BasicDbcpPooledDataSourceCreator.java | 34 +- .../CommonPoolCloudConfigTestHelper.java | 32 +- .../config/java/CloudScanJavaConfigTest.java | 10 +- ...qlServiceCreatorWithDefaultDriverTest.java | 4 +- ...ysqlServiceCreatorWithMysqlDriverTest.java | 8 +- 32 files changed, 684 insertions(+), 718 deletions(-) diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/Cloud.java b/spring-cloud-core/src/main/java/org/springframework/cloud/Cloud.java index 109f3f1..c3b91ee 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/Cloud.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/Cloud.java @@ -40,349 +40,337 @@ import org.springframework.cloud.service.ServiceInfo.ServiceProperty; * */ public class Cloud { - private CloudConnector cloudConnector; - private ServiceConnectorCreatorRegistry serviceConnectorCreatorRegistry = new ServiceConnectorCreatorRegistry(); + private CloudConnector cloudConnector; + private ServiceConnectorCreatorRegistry serviceConnectorCreatorRegistry = new ServiceConnectorCreatorRegistry(); - /** - * Package-access constructor. - * - * @param cloudConnector - * the underlying connector - * @param serviceConnectorCreators - * service connector creators - */ - Cloud(CloudConnector cloudConnector, List> serviceConnectorCreators) { - this.cloudConnector = cloudConnector; + /** + * Package-access constructor. + * + * @param cloudConnector the underlying connector + * @param serviceConnectorCreators service connector creators + */ + Cloud(CloudConnector cloudConnector, List> serviceConnectorCreators) { + this.cloudConnector = cloudConnector; - for (ServiceConnectorCreator serviceCreator : serviceConnectorCreators) { - registerServiceConnectorCreator(serviceCreator); - } - } + for (ServiceConnectorCreator serviceCreator : serviceConnectorCreators) { + registerServiceConnectorCreator(serviceCreator); + } + } - /** - * @see CloudConnector#getApplicationInstanceInfo() - * - * @return information about the application instance - */ - public ApplicationInstanceInfo getApplicationInstanceInfo() { - return cloudConnector.getApplicationInstanceInfo(); - } + /** + * @see CloudConnector#getApplicationInstanceInfo() + * + * @return information about the application instance + */ + public ApplicationInstanceInfo getApplicationInstanceInfo() { + return cloudConnector.getApplicationInstanceInfo(); + } - /** - * Get {@link ServiceInfo} for the given service id - * - * @param serviceId - * service id - * @return info for the serviceId - */ - public ServiceInfo getServiceInfo(String serviceId) { - for (ServiceInfo serviceInfo : getServiceInfos()) { - if (serviceInfo.getId().equals(serviceId)) { - return serviceInfo; - } - } - throw new CloudException("No service with id " + serviceId + " found"); - } + /** + * Get {@link ServiceInfo} for the given service id + * + * @param serviceId service id + * @return info for the serviceId + */ + public ServiceInfo getServiceInfo(String serviceId) { + for (ServiceInfo serviceInfo : getServiceInfos()) { + if (serviceInfo.getId().equals(serviceId)) { + return serviceInfo; + } + } + throw new CloudException("No service with id " + serviceId + " found"); + } - /** - * @see CloudConnector#getServiceInfos() - * @return information about all services bound to the application - */ - public List getServiceInfos() { - return flatten(cloudConnector.getServiceInfos()); - } + /** + * @see CloudConnector#getServiceInfos() + * @return information about all services bound to the application + */ + public List getServiceInfos() { + return flatten(cloudConnector.getServiceInfos()); + } - /** - * Get {@link ServiceInfo}s for the bound services that could be mapped to the given service connector type. - * - *

- * For example, if the connector type is {@link DataSource}, then the method will return all {@link ServiceInfo} objects - * matching bound relational database services. - *

- * - * @param - * The class of the connector to find services for. - * @param serviceConnectorType - * service connector type. - * Passing null returns all {@link ServiceInfo}s (matching that of {@link Cloud#getServiceInfos()} - * @return information about services bound to the application that could be transformed into the given connector type - */ - public List getServiceInfos(Class serviceConnectorType) { - List allServiceInfos = getServiceInfos(); - List matchingServiceInfos = new ArrayList(); + /** + * Get {@link ServiceInfo}s for the bound services that could be mapped to the given service connector type. + * + *

+ * For example, if the connector type is {@link DataSource}, then the method will return all {@link ServiceInfo} objects + * matching bound relational database services. + *

+ * + * @param The class of the connector to find services for. + * @param serviceConnectorType service connector type. + * Passing null returns all {@link ServiceInfo}s (matching that of {@link Cloud#getServiceInfos()} + * @return information about services bound to the application that could be transformed into the given connector type + */ + public List getServiceInfos(Class serviceConnectorType) { + List allServiceInfos = getServiceInfos(); + List matchingServiceInfos = new ArrayList(); - for (ServiceInfo serviceInfo : allServiceInfos) { - if (serviceConnectorCreatorRegistry.canCreate(serviceConnectorType, serviceInfo)) { - matchingServiceInfos.add(serviceInfo); - } - } + for (ServiceInfo serviceInfo : allServiceInfos) { + if (serviceConnectorCreatorRegistry.canCreate(serviceConnectorType, serviceInfo)) { + matchingServiceInfos.add(serviceInfo); + } + } - return matchingServiceInfos; - } + return matchingServiceInfos; + } - /** - * Get a service connector for the given service id, the connector type, configured with the given config - * - * - * @param - * The class of the service connector to return. - * @param serviceId - * the service id - * @param serviceConnectorType - * The expected class of service connector such as, DataSource.class. - * @param serviceConnectorConfig - * service connector configuration (such as pooling parameters). - * @return a service connector of the specified type with the given configuration applied - * - */ - public SC getServiceConnector(String serviceId, Class serviceConnectorType, - ServiceConnectorConfig serviceConnectorConfig) { - ServiceInfo serviceInfo = getServiceInfo(serviceId); + /** + * Get a service connector for the given service id, the connector type, configured with the given config + * + * + * @param The class of the service connector to return. + * @param serviceId the service id + * @param serviceConnectorType The expected class of service connector such as, DataSource.class. + * @param serviceConnectorConfig service connector configuration (such as pooling parameters). + * @return a service connector of the specified type with the given configuration applied + * + */ + public SC getServiceConnector(String serviceId, Class serviceConnectorType, + ServiceConnectorConfig serviceConnectorConfig) { + ServiceInfo serviceInfo = getServiceInfo(serviceId); - return getServiceConnector(serviceInfo, serviceConnectorType, serviceConnectorConfig); - } + return getServiceConnector(serviceInfo, serviceConnectorType, serviceConnectorConfig); + } - /** - * Get the singleton service connector for the given connector type, configured with the given config - * - * @param - * The class of the service connector to return. - * @param serviceConnectorType - * The expected class of service connector such as, DataSource.class. - * @param serviceConnectorConfig - * service connector configuration (such as pooling parameters). - * @return the single service connector of the specified type with the given configuration applied - * - */ - public SC getSingletonServiceConnector(Class serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) { - List matchingServiceInfos = getServiceInfos(serviceConnectorType); + /** + * Get the singleton service connector for the given connector type, configured with the given config + * + * @param The class of the service connector to return. + * @param serviceConnectorType The expected class of service connector such as, DataSource.class. + * @param serviceConnectorConfig service connector configuration (such as pooling parameters). + * @return the single service connector of the specified type with the given configuration applied + * + */ + public SC getSingletonServiceConnector(Class serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) { + List matchingServiceInfos = getServiceInfos(serviceConnectorType); - if (matchingServiceInfos.size() != 1) { - throw new CloudException("No unique service matching " + serviceConnectorType + " found. Expected 1, found " - + matchingServiceInfos.size()); - } + if (matchingServiceInfos.size() != 1) { + throw new CloudException("No unique service matching " + serviceConnectorType + " found. Expected 1, found " + + matchingServiceInfos.size()); + } - ServiceInfo matchingServiceInfo = matchingServiceInfos.get(0); + ServiceInfo matchingServiceInfo = matchingServiceInfos.get(0); - return getServiceConnector(matchingServiceInfo, serviceConnectorType, serviceConnectorConfig); - } + return getServiceConnector(matchingServiceInfo, serviceConnectorType, serviceConnectorConfig); + } - /** - * Register a new service connector creator - * - * @param serviceConnectorCreator the service connector to register - */ - public void registerServiceConnectorCreator(ServiceConnectorCreator serviceConnectorCreator) { - serviceConnectorCreatorRegistry.registerCreator(serviceConnectorCreator); - } + /** + * Register a new service connector creator + * + * @param serviceConnectorCreator the service connector to register + */ + public void registerServiceConnectorCreator(ServiceConnectorCreator serviceConnectorCreator) { + serviceConnectorCreatorRegistry.registerCreator(serviceConnectorCreator); + } - /** - * Get properties for app and services. - * - * - * - *

- * Application properties always include cloud.application.app-id and cloud.application.instance-id - * with values bound to application id and instance id. The rest of the properties are cloud-provider specific, but take the - * cloud.application.<property-name> form.

-     * cloud.application.app-id = helloworld
-     * cloud.application.instance-id = instance-0-0fab098f
-     * cloud.application.<property-name> = <property-value>
-     * 
- * - *

- * Service specific properties are exposed for each bound service, with each key starting in cloud.services. Like - * application properties, these too are cloud and service specific. Each key for a specific service starts with - * cloud.services.<service-id>

-     * cloud.services.customerDb.type = mysql-5.1
-     * cloud.services.customerDb.plan = free
-     * cloud.services.customerDb.connection.hostname = ...
-     * cloud.services.customerDb.connection.port = ...
-     * etc...
-     * 
- * - *

- * If a there is only a single service of a given type (as defined by the {link ServiceInfo.ServiceLabel} - * annoation's value of the corresponding {@link ServiceInfo} class), that service is aliased - * to the service type. Keys for such properties start in cloud.services.<service-type>. - * For example, if there is only a single MySQL service bound to the application, the service properties - * will also be exposed starting with 'cloud.services.mysql' key:

-     * cloud.services.mysql.type = mysql-5.1
-     * cloud.services.mysql.plan = free
-     * cloud.services.mysql.connection.hostname = ...
-     * cloud.services.mysql.connection.port = ...
-     * etc...
-     * 
- * - * @return the properties object - */ - public Properties getCloudProperties() { - Map> mappedServiceInfos = new HashMap>(); - for (ServiceInfo serviceInfo : getServiceInfos()) { - String key = getServiceLabel(serviceInfo); - List serviceInfosForLabel = mappedServiceInfos.get(key); - if (serviceInfosForLabel == null) { - serviceInfosForLabel = new ArrayList(); - mappedServiceInfos.put(key, serviceInfosForLabel); - } - serviceInfosForLabel.add(serviceInfo); - } + /** + * Get properties for app and services. + * + * + * + *

+ * Application properties always include cloud.application.app-id and cloud.application.instance-id + * with values bound to application id and instance id. The rest of the properties are cloud-provider specific, but take the + * cloud.application.<property-name> form.

+	 * cloud.application.app-id = helloworld
+	 * cloud.application.instance-id = instance-0-0fab098f
+	 * cloud.application.<property-name> = <property-value>
+	 * 
+ * + *

+ * Service specific properties are exposed for each bound service, with each key starting in cloud.services. Like + * application properties, these too are cloud and service specific. Each key for a specific service starts with + * cloud.services.<service-id>

+	 * cloud.services.customerDb.type = mysql-5.1
+	 * cloud.services.customerDb.plan = free
+	 * cloud.services.customerDb.connection.hostname = ...
+	 * cloud.services.customerDb.connection.port = ...
+	 * etc...
+	 * 
+ * + *

+ * If a there is only a single service of a given type (as defined by the {link ServiceInfo.ServiceLabel} + * annoation's value of the corresponding {@link ServiceInfo} class), that service is aliased + * to the service type. Keys for such properties start in cloud.services.<service-type>. + * For example, if there is only a single MySQL service bound to the application, the service properties + * will also be exposed starting with 'cloud.services.mysql' key:

+	 * cloud.services.mysql.type = mysql-5.1
+	 * cloud.services.mysql.plan = free
+	 * cloud.services.mysql.connection.hostname = ...
+	 * cloud.services.mysql.connection.port = ...
+	 * etc...
+	 * 
+ * + * @return the properties object + */ + public Properties getCloudProperties() { + Map> mappedServiceInfos = new HashMap>(); + for (ServiceInfo serviceInfo : getServiceInfos()) { + String key = getServiceLabel(serviceInfo); + List serviceInfosForLabel = mappedServiceInfos.get(key); + if (serviceInfosForLabel == null) { + serviceInfosForLabel = new ArrayList(); + mappedServiceInfos.put(key, serviceInfosForLabel); + } + serviceInfosForLabel.add(serviceInfo); + } - final String servicePropKeyLead = "cloud.services."; - Properties cloudProperties = new Properties(); - for (Entry> mappedServiceInfo : mappedServiceInfos.entrySet()) { - List serviceInfos = mappedServiceInfo.getValue(); + final String servicePropKeyLead = "cloud.services."; + Properties cloudProperties = new Properties(); + for (Entry> mappedServiceInfo : mappedServiceInfos.entrySet()) { + List serviceInfos = mappedServiceInfo.getValue(); - for (ServiceInfo serviceInfo : serviceInfos) { - String idBasedKey = servicePropKeyLead + serviceInfo.getId(); - cloudProperties.putAll(getServiceProperties(idBasedKey, serviceInfo)); + for (ServiceInfo serviceInfo : serviceInfos) { + String idBasedKey = servicePropKeyLead + serviceInfo.getId(); + cloudProperties.putAll(getServiceProperties(idBasedKey, serviceInfo)); - // If there is only one service for a given label, put props with that label instead of just id - if (serviceInfos.size() == 1) { - String labelBasedKey = servicePropKeyLead + mappedServiceInfo.getKey(); - cloudProperties.putAll(getServiceProperties(labelBasedKey, serviceInfo)); - } - } - } + // If there is only one service for a given label, put props with that label instead of just id + if (serviceInfos.size() == 1) { + String labelBasedKey = servicePropKeyLead + mappedServiceInfo.getKey(); + cloudProperties.putAll(getServiceProperties(labelBasedKey, serviceInfo)); + } + } + } - cloudProperties.putAll(getAppProperties()); + cloudProperties.putAll(getAppProperties()); - return cloudProperties; - } + return cloudProperties; + } - private SC getServiceConnector(ServiceInfo serviceInfo, Class serviceConnectorType, - ServiceConnectorConfig serviceConnectorConfig) { - ServiceConnectorCreator serviceConnectorCreator = serviceConnectorCreatorRegistry.getServiceCreator( - serviceConnectorType, serviceInfo); - return serviceConnectorCreator.create(serviceInfo, serviceConnectorConfig); - } + private SC getServiceConnector(ServiceInfo serviceInfo, Class serviceConnectorType, + ServiceConnectorConfig serviceConnectorConfig) { + ServiceConnectorCreator serviceConnectorCreator = serviceConnectorCreatorRegistry.getServiceCreator( + serviceConnectorType, serviceInfo); + return serviceConnectorCreator.create(serviceInfo, serviceConnectorConfig); + } - private Properties getAppProperties() { - final String appPropLeadKey = "cloud.application."; + private Properties getAppProperties() { + final String appPropLeadKey = "cloud.application."; - Properties appProperties = new Properties(); - appProperties.put(appPropLeadKey + "instance-id", getApplicationInstanceInfo().getInstanceId()); - appProperties.put(appPropLeadKey + "app-id", getApplicationInstanceInfo().getAppId()); - for (Map.Entry entry : getApplicationInstanceInfo().getProperties().entrySet()) { - if (entry.getValue() != null) { - appProperties.put(appPropLeadKey + entry.getKey(), entry.getValue()); - } - } + Properties appProperties = new Properties(); + appProperties.put(appPropLeadKey + "instance-id", getApplicationInstanceInfo().getInstanceId()); + appProperties.put(appPropLeadKey + "app-id", getApplicationInstanceInfo().getAppId()); + for (Map.Entry entry : getApplicationInstanceInfo().getProperties().entrySet()) { + if (entry.getValue() != null) { + appProperties.put(appPropLeadKey + entry.getKey(), entry.getValue()); + } + } - return appProperties; - } + return appProperties; + } - private Properties getServiceProperties(String keyLead, ServiceInfo serviceInfo) { - Properties cloudProperties = new Properties(); + private Properties getServiceProperties(String keyLead, ServiceInfo serviceInfo) { + Properties cloudProperties = new Properties(); - try { - BeanInfo beanInfo = Introspector.getBeanInfo(serviceInfo.getClass()); - PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); - for (PropertyDescriptor propDescriptor : propDescriptors) { - ServiceProperty propAnnotation = propDescriptor.getReadMethod().getAnnotation(ServiceProperty.class); - String key = keyLead; + try { + BeanInfo beanInfo = Introspector.getBeanInfo(serviceInfo.getClass()); + PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propDescriptor : propDescriptors) { + ServiceProperty propAnnotation = propDescriptor.getReadMethod().getAnnotation(ServiceProperty.class); + String key = keyLead; - if (propAnnotation != null) { - if (!propAnnotation.category().isEmpty()) { - key = key + "." + propAnnotation.category(); - } - if (!propAnnotation.name().isEmpty()) { - key = key + "." + propAnnotation.name(); - } else { - key = key + "." + propDescriptor.getName().toLowerCase(); - } + if (propAnnotation != null) { + if (!propAnnotation.category().isEmpty()) { + key = key + "." + propAnnotation.category(); + } + if (!propAnnotation.name().isEmpty()) { + key = key + "." + propAnnotation.name(); + } else { + key = key + "." + propDescriptor.getName().toLowerCase(); + } - Object value = propDescriptor.getReadMethod().invoke(serviceInfo); + Object value = propDescriptor.getReadMethod().invoke(serviceInfo); - if (value != null) { - cloudProperties.put(key, value); - } - } - } - } catch (Exception e) { - throw new CloudException(e); - } + if (value != null) { + cloudProperties.put(key, value); + } + } + } + } catch (Exception e) { + throw new CloudException(e); + } - return cloudProperties; - } + return cloudProperties; + } - private static String getServiceLabel(ServiceInfo serviceInfo) { - Class serviceInfoClass = serviceInfo.getClass(); + private static String getServiceLabel(ServiceInfo serviceInfo) { + Class serviceInfoClass = serviceInfo.getClass(); - ServiceLabel labelAnnotation = serviceInfoClass.getAnnotation(ServiceInfo.ServiceLabel.class); + ServiceLabel labelAnnotation = serviceInfoClass.getAnnotation(ServiceInfo.ServiceLabel.class); - if (labelAnnotation == null) { - return null; - } else { - return labelAnnotation.value(); - } - } - - private static List flatten(List serviceInfos) { - List flattened = new ArrayList(); - - for (ServiceInfo serviceInfo : serviceInfos) { - if (serviceInfo instanceof CompositeServiceInfo) { - // recursively flatten any CompositeServiceInfos - CompositeServiceInfo compositeServiceInfo = (CompositeServiceInfo)serviceInfo; - flattened.addAll(flatten(compositeServiceInfo.getServiceInfos())); - } else { - flattened.add(serviceInfo); - } - } - - return flattened; - } + if (labelAnnotation == null) { + return null; + } else { + return labelAnnotation.value(); + } + } + + private static List flatten(List serviceInfos) { + List flattened = new ArrayList(); + + for (ServiceInfo serviceInfo : serviceInfos) { + if (serviceInfo instanceof CompositeServiceInfo) { + // recursively flatten any CompositeServiceInfos + CompositeServiceInfo compositeServiceInfo = (CompositeServiceInfo)serviceInfo; + flattened.addAll(flatten(compositeServiceInfo.getServiceInfos())); + } else { + flattened.add(serviceInfo); + } + } + + return flattened; + } } class ServiceConnectorCreatorRegistry { - private static Logger logger = Logger.getLogger(Cloud.class.getName()); + private static Logger logger = Logger.getLogger(Cloud.class.getName()); - private List> serviceConnectorCreators = new ArrayList>(); + private List> serviceConnectorCreators = new ArrayList>(); - public void registerCreator(ServiceConnectorCreator serviceConnectorCreator) { - serviceConnectorCreators.add(serviceConnectorCreator); - } + public void registerCreator(ServiceConnectorCreator serviceConnectorCreator) { + serviceConnectorCreators.add(serviceConnectorCreator); + } - public ServiceConnectorCreator getServiceCreator(Class serviceConnectorType, - SI serviceInfo) { - ServiceConnectorCreator serviceConnectorCreator = getServiceCreatorOrNull(serviceConnectorType, serviceInfo); + public ServiceConnectorCreator getServiceCreator(Class serviceConnectorType, + SI serviceInfo) { + ServiceConnectorCreator serviceConnectorCreator = getServiceCreatorOrNull(serviceConnectorType, serviceInfo); - if (serviceConnectorCreator != null) { - return serviceConnectorCreator; - } else { - throw new CloudException("No suitable ServiceConnectorCreator found: " - + "service id=" + serviceInfo.getId() + ", " - + "service info type=" + serviceInfo.getClass().getName() + ", " - + "connector type=" + serviceConnectorType); - } - } + if (serviceConnectorCreator != null) { + return serviceConnectorCreator; + } else { + throw new CloudException("No suitable ServiceConnectorCreator found: " + + "service id=" + serviceInfo.getId() + ", " + + "service info type=" + serviceInfo.getClass().getName() + ", " + + "connector type=" + serviceConnectorType); + } + } - public boolean canCreate(Class serviceConnectorType, SI serviceInfo) { - return getServiceCreatorOrNull(serviceConnectorType, serviceInfo) != null; - } + public boolean canCreate(Class serviceConnectorType, SI serviceInfo) { + return getServiceCreatorOrNull(serviceConnectorType, serviceInfo) != null; + } - public boolean accept(ServiceConnectorCreator creator, Class serviceConnectorType, - ServiceInfo serviceInfo) { - boolean typeBasedAccept = serviceConnectorType == null ? true : serviceConnectorType.isAssignableFrom(creator - .getServiceConnectorType()); - boolean infoBasedAccept = serviceInfo == null ? true : creator.getServiceInfoType() - .isAssignableFrom(serviceInfo.getClass()); + public boolean accept(ServiceConnectorCreator creator, Class serviceConnectorType, + ServiceInfo serviceInfo) { + boolean typeBasedAccept = serviceConnectorType == null || + serviceConnectorType.isAssignableFrom(creator.getServiceConnectorType()); + boolean infoBasedAccept = serviceInfo == null || + creator.getServiceInfoType().isAssignableFrom(serviceInfo.getClass()); - return typeBasedAccept && infoBasedAccept; - } + return typeBasedAccept && infoBasedAccept; + } - @SuppressWarnings("unchecked") - private ServiceConnectorCreator getServiceCreatorOrNull(Class serviceConnectorType, - SI serviceInfo) { - for (ServiceConnectorCreator serviceConnectorCreator : serviceConnectorCreators) { - logger.fine("Trying connector creator type " + serviceConnectorCreator); - if (accept(serviceConnectorCreator, serviceConnectorType, serviceInfo)) { - return (ServiceConnectorCreator) serviceConnectorCreator; - } - } - return null; - } + @SuppressWarnings("unchecked") + private ServiceConnectorCreator getServiceCreatorOrNull(Class serviceConnectorType, + SI serviceInfo) { + for (ServiceConnectorCreator serviceConnectorCreator : serviceConnectorCreators) { + logger.fine("Trying connector creator type " + serviceConnectorCreator); + if (accept(serviceConnectorCreator, serviceConnectorType, serviceInfo)) { + return (ServiceConnectorCreator) serviceConnectorCreator; + } + } + return null; + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/CloudFactory.java b/spring-cloud-core/src/main/java/org/springframework/cloud/CloudFactory.java index 69b4561..6f4a849 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/CloudFactory.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/CloudFactory.java @@ -28,78 +28,78 @@ import org.springframework.cloud.util.ServiceLoaderWithExceptionControl; * */ public class CloudFactory { - private List cloudConnectors = new ArrayList(); - private List> serviceCreators = new ArrayList>(); + private List cloudConnectors = new ArrayList(); + private List> serviceCreators = new ArrayList>(); - public CloudFactory() { - scanCloudConnectors(); - scanServiceConnectorCreators(); - } + public CloudFactory() { + scanCloudConnectors(); + scanServiceConnectorCreators(); + } - /** - * - * @return a cloud suitable for the current environment - * @throws CloudException - * if no suitable cloud found - */ - public Cloud getCloud() { - CloudConnector suitableCloudConnector = null; - for (CloudConnector cloudConnector : cloudConnectors) { - if (cloudConnector.isInMatchingCloud()) { - suitableCloudConnector = cloudConnector; - break; - } - } + /** + * + * @return a cloud suitable for the current environment + * @throws CloudException + * if no suitable cloud found + */ + public Cloud getCloud() { + CloudConnector suitableCloudConnector = null; + for (CloudConnector cloudConnector : cloudConnectors) { + if (cloudConnector.isInMatchingCloud()) { + suitableCloudConnector = cloudConnector; + break; + } + } - if (suitableCloudConnector == null) { - throw new CloudException("No suitable cloud connector found"); - } + if (suitableCloudConnector == null) { + throw new CloudException("No suitable cloud connector found"); + } - return new Cloud(suitableCloudConnector, serviceCreators); - } + return new Cloud(suitableCloudConnector, serviceCreators); + } - /** - * Register a cloud connector. - * - *

- * CloudConnector developers should prefer the declarative mechanism described in README.MD instead of calling this method. - *

- * - * @param cloudConnector - * the cloud connector to register for discovery - */ - public void registerCloudConnector(CloudConnector cloudConnector) { - cloudConnectors.add(cloudConnector); - } + /** + * Register a cloud connector. + * + *

+ * CloudConnector developers should prefer the declarative mechanism described in README.MD instead of calling this method. + *

+ * + * @param cloudConnector + * the cloud connector to register for discovery + */ + public void registerCloudConnector(CloudConnector cloudConnector) { + cloudConnectors.add(cloudConnector); + } - /* package access for testing */ - List getCloudConnectors() { - return cloudConnectors; - } + /* package access for testing */ + List getCloudConnectors() { + return cloudConnectors; + } - /* package access for testing */ - List> getServiceCreators() { - return serviceCreators; - } + /* package access for testing */ + List> getServiceCreators() { + return serviceCreators; + } - private void registerServiceCreator(ServiceConnectorCreator serviceConnectorCreator) { - serviceCreators.add(serviceConnectorCreator); - } + private void registerServiceCreator(ServiceConnectorCreator serviceConnectorCreator) { + serviceCreators.add(serviceConnectorCreator); + } - private void scanCloudConnectors() { - Iterable loader = ServiceLoader.load(CloudConnector.class); - for (CloudConnector cloudConnector : loader) { - registerCloudConnector(cloudConnector); - } - } + private void scanCloudConnectors() { + Iterable loader = ServiceLoader.load(CloudConnector.class); + for (CloudConnector cloudConnector : loader) { + registerCloudConnector(cloudConnector); + } + } - @SuppressWarnings({ "rawtypes", "unchecked" }) - private void scanServiceConnectorCreators() { - Iterable loader = ServiceLoaderWithExceptionControl.load(ServiceConnectorCreator.class); - for (ServiceConnectorCreator serviceConnectorCreator : loader) { - if (serviceConnectorCreator != null) { - registerServiceCreator(serviceConnectorCreator); - } - } - } + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void scanServiceConnectorCreators() { + Iterable loader = ServiceLoaderWithExceptionControl.load(ServiceConnectorCreator.class); + for (ServiceConnectorCreator serviceConnectorCreator : loader) { + if (serviceConnectorCreator != null) { + registerServiceCreator(serviceConnectorCreator); + } + } + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/FallbackServiceInfoCreator.java b/spring-cloud-core/src/main/java/org/springframework/cloud/FallbackServiceInfoCreator.java index 58625ce..3a267b4 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/FallbackServiceInfoCreator.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/FallbackServiceInfoCreator.java @@ -11,20 +11,17 @@ import org.springframework.cloud.service.ServiceInfo; * for it isn't placed properly to be picked up by scanning. By having a fallback mechanism, we can * extract as much info from the service data as possible (minimally, the service id) and issue a warning. * + * @param the type of {@link ServiceInfo} that this fallback creator will return + * @param the type of service definition data that this creator will consume * @author Ramnivas Laddad - * - * @param - * the type of {@link ServiceInfo} that this fallback creator will return - * @param - * the type of service definition data that this creator will consume */ public abstract class FallbackServiceInfoCreator implements ServiceInfoCreator { - /* - * Override to ensure that it accepts all service data - */ - @Override - public final boolean accept(SD serviceData) { - return true; - } + /* + * Override to ensure that it accepts all service data + */ + @Override + public final boolean accept(SD serviceData) { + return true; + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/BaseCompositeServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/BaseCompositeServiceInfo.java index 41dcea2..348b2e7 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/BaseCompositeServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/BaseCompositeServiceInfo.java @@ -10,16 +10,16 @@ import java.util.List; * */ public class BaseCompositeServiceInfo extends BaseServiceInfo implements CompositeServiceInfo { - - private List constituents; - public BaseCompositeServiceInfo(String id, ServiceInfo... constituents) { - super(id); - this.constituents = Arrays.asList(constituents); - } + private List constituents; - @Override - public List getServiceInfos() { - return constituents; - } + public BaseCompositeServiceInfo(String id, ServiceInfo... constituents) { + super(id); + this.constituents = Arrays.asList(constituents); + } + + @Override + public List getServiceInfos() { + return constituents; + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/CompositeServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/CompositeServiceInfo.java index c0d3127..ecf4446 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/CompositeServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/CompositeServiceInfo.java @@ -19,6 +19,6 @@ import org.springframework.cloud.Cloud; */ public interface CompositeServiceInfo extends ServiceInfo { - List getServiceInfos(); - + List getServiceInfos(); + } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/FallbackBaseServiceInfoCreator.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/FallbackBaseServiceInfoCreator.java index e819b76..cd25186 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/FallbackBaseServiceInfoCreator.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/FallbackBaseServiceInfoCreator.java @@ -3,8 +3,8 @@ package org.springframework.cloud.service; import org.springframework.cloud.FallbackServiceInfoCreator; public class FallbackBaseServiceInfoCreator extends FallbackServiceInfoCreator { - @Override - public BaseServiceInfo createServiceInfo(UriBasedServiceData serviceData) { - return new BaseServiceInfo(serviceData.getKey()); - } + @Override + public BaseServiceInfo createServiceInfo(UriBasedServiceData serviceData) { + return new BaseServiceInfo(serviceData.getKey()); + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/ServiceConnectorCreator.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/ServiceConnectorCreator.java index 627360d..6437ca8 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/ServiceConnectorCreator.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/ServiceConnectorCreator.java @@ -8,35 +8,31 @@ import org.springframework.cloud.service.common.MysqlServiceInfo; * * @author Ramnivas Laddad * - * @param - * service connector type - * @param - * service info type + * @param service connector type + * @param service info type */ public interface ServiceConnectorCreator { - /** - * Create service for the given service info and configured with the given - * configuration - * - * @param serviceInfo - * the {@link ServiceInfo} object containing the information necessary to connect to the service - * @param serviceConnectorConfig - * configuration information to be applied to the connection - * @return service connector - */ - SC create(SI serviceInfo, ServiceConnectorConfig serviceConnectorConfig); + /** + * Create service for the given service info and configured with the given + * configuration + * + * @param serviceInfo the {@link ServiceInfo} object containing the information necessary to connect to the service + * @param serviceConnectorConfig configuration information to be applied to the connection + * @return service connector + */ + SC create(SI serviceInfo, ServiceConnectorConfig serviceConnectorConfig); - /** - * Get the type of connector created (such as {@link DataSource}) - * - * @return service connector type - */ - Class getServiceConnectorType(); + /** + * Get the type of connector created (such as {@link DataSource}) + * + * @return service connector type + */ + Class getServiceConnectorType(); - /** - * Get the service info type this creator can work with (such as {@link MysqlServiceInfo}) - * - * @return service info type - */ - Class getServiceInfoType(); + /** + * Get the service info type this creator can work with (such as {@link MysqlServiceInfo}) + * + * @return service info type + */ + Class getServiceInfoType(); } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MongoServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MongoServiceInfo.java index 7bc4369..7e16359 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MongoServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MongoServiceInfo.java @@ -11,7 +11,7 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel; @ServiceLabel("mongo") public class MongoServiceInfo extends UriBasedServiceInfo { - public static final String MONGODB_SCHEME = "mongodb"; + public static final String MONGODB_SCHEME = "mongodb"; public MongoServiceInfo(String id, String host, int port, String username, String password, String db) { super(id, MONGODB_SCHEME, host, port, username, password, db); diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MysqlServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MysqlServiceInfo.java index 289d503..d00dae5 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MysqlServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/MysqlServiceInfo.java @@ -10,11 +10,11 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel; @ServiceLabel("mysql") public class MysqlServiceInfo extends RelationalServiceInfo { - private static final String JDBC_URL_TYPE = "mysql"; + private static final String JDBC_URL_TYPE = "mysql"; - public static final String MYSQL_SCHEME = JDBC_URL_TYPE; + public static final String MYSQL_SCHEME = JDBC_URL_TYPE; - public MysqlServiceInfo(String id, String url) { - super(id, url, MYSQL_SCHEME); - } + public MysqlServiceInfo(String id, String url) { + super(id, url, MYSQL_SCHEME); + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/OracleServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/OracleServiceInfo.java index 0dfec02..789d957 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/OracleServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/OracleServiceInfo.java @@ -5,9 +5,9 @@ import org.springframework.cloud.service.ServiceInfo; @ServiceInfo.ServiceLabel("oracle") public class OracleServiceInfo extends RelationalServiceInfo { - private static final String JDBC_URL_TYPE = "oracle"; + private static final String JDBC_URL_TYPE = "oracle"; - public static final String ORACLE_SCHEME = JDBC_URL_TYPE; + public static final String ORACLE_SCHEME = JDBC_URL_TYPE; public OracleServiceInfo(String id, String url) { super(id, url, JDBC_URL_TYPE); diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/PostgresqlServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/PostgresqlServiceInfo.java index ed168ca..bfc888f 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/PostgresqlServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/PostgresqlServiceInfo.java @@ -11,9 +11,9 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel; @ServiceLabel("postgresql") public class PostgresqlServiceInfo extends RelationalServiceInfo { - private static final String JDBC_URL_TYPE = "postgresql"; + private static final String JDBC_URL_TYPE = "postgresql"; - public static final String POSTGRES_SCHEME = "postgres"; + public static final String POSTGRES_SCHEME = "postgres"; public PostgresqlServiceInfo(String id, String url) { super(id, url, JDBC_URL_TYPE); diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java index 485e300..e5e2605 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/service/common/RedisServiceInfo.java @@ -11,13 +11,13 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel; @ServiceLabel("redis") public class RedisServiceInfo extends UriBasedServiceInfo { - public static final String REDIS_SCHEME = "redis"; + public static final String REDIS_SCHEME = "redis"; public RedisServiceInfo(String id, String host, int port, String password) { super(id, REDIS_SCHEME, host, port, null, password, null); } public RedisServiceInfo(String id, String uri) { - super(id, uri); - } + super(id, uri); + } } diff --git a/spring-cloud-core/src/main/java/org/springframework/cloud/util/ServiceLoaderWithExceptionControl.java b/spring-cloud-core/src/main/java/org/springframework/cloud/util/ServiceLoaderWithExceptionControl.java index dbb5544..c04b9d7 100644 --- a/spring-cloud-core/src/main/java/org/springframework/cloud/util/ServiceLoaderWithExceptionControl.java +++ b/spring-cloud-core/src/main/java/org/springframework/cloud/util/ServiceLoaderWithExceptionControl.java @@ -20,56 +20,55 @@ import org.springframework.cloud.service.ServiceConnectorCreator; * * @author Ramnivas Laddad * - * @param - * the type of the service being loaded + * @param the type of the service being loaded */ public class ServiceLoaderWithExceptionControl implements Iterable { - private Iterable underlying; + private Iterable underlying; - private static Logger logger = Logger.getLogger(ServiceLoaderWithExceptionControl.class.getName()); + private static Logger logger = Logger.getLogger(ServiceLoaderWithExceptionControl.class.getName()); - public static Iterable load(Class serviceType) { - ServiceLoader loader = ServiceLoader.load(serviceType); - return new ServiceLoaderWithExceptionControl(loader); - } + public static Iterable load(Class serviceType) { + ServiceLoader loader = ServiceLoader.load(serviceType); + return new ServiceLoaderWithExceptionControl(loader); + } - private ServiceLoaderWithExceptionControl(Iterable underlying) { - this.underlying = underlying; - } + private ServiceLoaderWithExceptionControl(Iterable underlying) { + this.underlying = underlying; + } - @Override - public Iterator iterator() { - return new ServiceLoaderIterator(underlying.iterator()); - } + @Override + public Iterator iterator() { + return new ServiceLoaderIterator(underlying.iterator()); + } - private class ServiceLoaderIterator implements Iterator { - private Iterator underlying; + private class ServiceLoaderIterator implements Iterator { + private Iterator underlying; - public ServiceLoaderIterator(Iterator underlying) { - this.underlying = underlying; - } + public ServiceLoaderIterator(Iterator underlying) { + this.underlying = underlying; + } - @Override - public boolean hasNext() { - return underlying.hasNext(); - } + @Override + public boolean hasNext() { + return underlying.hasNext(); + } - @Override - public T next() { - try { - return underlying.next(); - } catch (ServiceConfigurationError e) { - logger.log(Level.CONFIG, "Failed to load " + e); - if (hasNext()) { - return next(); - } - } - return null; - } + @Override + public T next() { + try { + return underlying.next(); + } catch (ServiceConfigurationError e) { + logger.log(Level.CONFIG, "Failed to load " + e); + if (hasNext()) { + return next(); + } + } + return null; + } - @Override - public void remove() { - underlying.remove(); - } - } + @Override + public void remove() { + underlying.remove(); + } + } } \ No newline at end of file diff --git a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/AmqpServiceInfoCreator.java b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/AmqpServiceInfoCreator.java index 303639e..05218a0 100644 --- a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/AmqpServiceInfoCreator.java +++ b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/AmqpServiceInfoCreator.java @@ -18,8 +18,8 @@ public class AmqpServiceInfoCreator extends HerokuServiceInfoCreator env = new HashMap(); - String amqpUrl = getAmqpServiceUrl("db"); - env.put("CLOUDAMQP_URL", amqpUrl); - when(mockEnvironment.getEnv()).thenReturn(env); - List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "CLOUDAMQP"); - assertNotNull(serviceInfo); - assertTrue(serviceInfo instanceof AmqpServiceInfo); - assertAmqpServiceInfo((AmqpServiceInfo)serviceInfo, "db"); - } - - private String getAmqpServiceUrl(String name) { - String template = "amqp://$username:$password@$hostname:$port/$virtualHost"; + @Test + public void amqpServiceCreation() { + Map env = new HashMap(); + String amqpUrl = getAmqpServiceUrl("db"); + env.put("CLOUDAMQP_URL", amqpUrl); + when(mockEnvironment.getEnv()).thenReturn(env); + + List serviceInfos = testCloudConnector.getServiceInfos(); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "CLOUDAMQP"); + assertNotNull(serviceInfo); + assertTrue(serviceInfo instanceof AmqpServiceInfo); + assertAmqpServiceInfo((AmqpServiceInfo) serviceInfo, "db"); + } + + private String getAmqpServiceUrl(String name) { + String template = "amqp://$username:$password@$hostname:$port/$virtualHost"; + + return template.replace("$username", username). + replace("$password", password). + replace("$hostname", hostname). + replace("$port", Integer.toString(port)). + replace("$virtualHost", name); + } + + protected void assertAmqpServiceInfo(AmqpServiceInfo serviceInfo, String virtualHost) { + assertEquals(hostname, serviceInfo.getHost()); + assertEquals(port, serviceInfo.getPort()); + assertEquals(username, serviceInfo.getUserName()); + assertEquals(password, serviceInfo.getPassword()); + assertEquals(virtualHost, serviceInfo.getPath()); + } - return template.replace("$username", username). - replace("$password", password). - replace("$hostname", hostname). - replace("$port", Integer.toString(port)). - replace("$virtualHost", name); - } - - protected void assertAmqpServiceInfo(AmqpServiceInfo serviceInfo, String virtualHost) { - assertEquals(hostname, serviceInfo.getHost()); - assertEquals(port, serviceInfo.getPort()); - assertEquals(username, serviceInfo.getUserName()); - assertEquals(password, serviceInfo.getPassword()); - assertEquals(virtualHost, serviceInfo.getPath()); - } - } diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorApplicationTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorApplicationTest.java index 5b6a499..80ffade 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorApplicationTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorApplicationTest.java @@ -10,35 +10,33 @@ import java.util.Map; import org.junit.Test; /** - * * @author Ramnivas Laddad - * */ public class HerokuConnectorApplicationTest extends AbstractHerokuConnectorTest { - @Test + @Test public void isInMatchingEnvironment() { when(mockEnvironment.getEnvValue("DYNO")).thenReturn("web.1"); assertTrue(testCloudConnector.isInMatchingCloud()); - + when(mockEnvironment.getEnvValue("DYNO")).thenReturn(null); assertFalse(testCloudConnector.isInMatchingCloud()); } - + @Test public void applicationInstanceInfo() { when(mockEnvironment.getEnvValue("SPRING_CLOUD_APP_NAME")).thenReturn("myapp"); when(mockEnvironment.getEnvValue("DYNO")).thenReturn("web.1"); when(mockEnvironment.getEnvValue("PORT")).thenReturn(Integer.toString(port)); when(mockEnvironment.getHost()).thenReturn(hostname); - + assertEquals("myapp", testCloudConnector.getApplicationInstanceInfo().getAppId()); assertEquals("web.1", testCloudConnector.getApplicationInstanceInfo().getInstanceId()); - Map appProps = testCloudConnector.getApplicationInstanceInfo().getProperties(); + Map appProps = testCloudConnector.getApplicationInstanceInfo().getProperties(); assertEquals(hostname, appProps.get("host")); assertEquals(Integer.toString(port), appProps.get("port")); } - + @Test public void applicationInstanceInfoNoSpringCloudAppName() { when(mockEnvironment.getEnvValue("DYNO")).thenReturn("web.1"); diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMongoServiceTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMongoServiceTest.java index 9550947..2b63367 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMongoServiceTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMongoServiceTest.java @@ -14,45 +14,43 @@ import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.service.common.MongoServiceInfo; /** - * * @author Ramnivas Laddad - * */ public class HerokuConnectorMongoServiceTest extends AbstractHerokuConnectorTest { - - @Test - public void mongoServiceCreation() { - for (String mongoEnv: new String[]{"MONGOLAB_URI", "MONGOHQ_URL", "MONGOSOUP_URL"}) { - Map env = new HashMap(); - String mongoUrl = getMongoServiceUrl("db"); - env.put(mongoEnv, mongoUrl); - when(mockEnvironment.getEnv()).thenReturn(env); - - List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, mongoEnv.substring(0, mongoEnv.length()-4)); - assertNotNull(serviceInfo); - assertTrue(serviceInfo instanceof MongoServiceInfo); - assertMongoServiceInfo((MongoServiceInfo)serviceInfo, "db"); - } - } - - private String getMongoServiceUrl(String name) { - String template = "mongodb://$username:$password@$hostname:$port/$db"; - return template.replace("$username", username). - replace("$password", password). - replace("$hostname", hostname). - replace("$port", Integer.toString(port)). - replace("$db", name); - } - - protected void assertMongoServiceInfo(MongoServiceInfo serviceInfo, String databaseName) { - assertEquals(hostname, serviceInfo.getHost()); - assertEquals(port, serviceInfo.getPort()); - assertEquals(username, serviceInfo.getUserName()); - assertEquals(password, serviceInfo.getPassword()); - assertEquals(databaseName, serviceInfo.getPath()); - } - + @Test + public void mongoServiceCreation() { + for (String mongoEnv : new String[]{"MONGOLAB_URI", "MONGOHQ_URL", "MONGOSOUP_URL"}) { + Map env = new HashMap(); + String mongoUrl = getMongoServiceUrl("db"); + env.put(mongoEnv, mongoUrl); + when(mockEnvironment.getEnv()).thenReturn(env); + + List serviceInfos = testCloudConnector.getServiceInfos(); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, mongoEnv.substring(0, mongoEnv.length() - 4)); + assertNotNull(serviceInfo); + assertTrue(serviceInfo instanceof MongoServiceInfo); + assertMongoServiceInfo((MongoServiceInfo) serviceInfo, "db"); + } + } + + private String getMongoServiceUrl(String name) { + String template = "mongodb://$username:$password@$hostname:$port/$db"; + + return template.replace("$username", username). + replace("$password", password). + replace("$hostname", hostname). + replace("$port", Integer.toString(port)). + replace("$db", name); + } + + protected void assertMongoServiceInfo(MongoServiceInfo serviceInfo, String databaseName) { + assertEquals(hostname, serviceInfo.getHost()); + assertEquals(port, serviceInfo.getPort()); + assertEquals(username, serviceInfo.getUserName()); + assertEquals(password, serviceInfo.getPassword()); + assertEquals(databaseName, serviceInfo.getPath()); + } + } diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMysqlServiceTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMysqlServiceTest.java index c8c94ed..988353d 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMysqlServiceTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorMysqlServiceTest.java @@ -13,26 +13,24 @@ import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.service.common.MysqlServiceInfo; /** - * * @author Ramnivas Laddad - * */ public class HerokuConnectorMysqlServiceTest extends AbstractHerokuConnectorRelationalServiceTest { - public HerokuConnectorMysqlServiceTest() { - super(MysqlServiceInfo.MYSQL_SCHEME); - } + public HerokuConnectorMysqlServiceTest() { + super(MysqlServiceInfo.MYSQL_SCHEME); + } - @Test - public void mysqlServiceCreation() { - Map env = new HashMap(); - String mysqlUrl = getRelationalServiceUrl("db"); - env.put("CLEARDB_DATABASE_URL", mysqlUrl); - when(mockEnvironment.getEnv()).thenReturn(env); + @Test + public void mysqlServiceCreation() { + Map env = new HashMap(); + String mysqlUrl = getRelationalServiceUrl("db"); + env.put("CLEARDB_DATABASE_URL", mysqlUrl); + when(mockEnvironment.getEnv()).thenReturn(env); - List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "CLEARDB_DATABASE"); - assertNotNull(serviceInfo); - assertTrue(serviceInfo instanceof MysqlServiceInfo); - assertReleationServiceInfo((MysqlServiceInfo)serviceInfo, "db"); - } + List serviceInfos = testCloudConnector.getServiceInfos(); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "CLEARDB_DATABASE"); + assertNotNull(serviceInfo); + assertTrue(serviceInfo instanceof MysqlServiceInfo); + assertReleationServiceInfo((MysqlServiceInfo) serviceInfo, "db"); + } } diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java index 2e03a14..969f6f6 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java @@ -13,26 +13,24 @@ import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.service.common.PostgresqlServiceInfo; /** - * * @author Ramnivas Laddad - * */ public class HerokuConnectorPostgresqlServiceTest extends AbstractHerokuConnectorRelationalServiceTest { - public HerokuConnectorPostgresqlServiceTest() { - super(PostgresqlServiceInfo.POSTGRES_SCHEME); - } + public HerokuConnectorPostgresqlServiceTest() { + super(PostgresqlServiceInfo.POSTGRES_SCHEME); + } - @Test - public void postgresqlServiceCreation() { - Map env = new HashMap(); - String postgresUrl = getRelationalServiceUrl("db"); - env.put("HEROKU_POSTGRESQL_YELLOW_URL", postgresUrl); - when(mockEnvironment.getEnv()).thenReturn(env); + @Test + public void postgresqlServiceCreation() { + Map env = new HashMap(); + String postgresUrl = getRelationalServiceUrl("db"); + env.put("HEROKU_POSTGRESQL_YELLOW_URL", postgresUrl); + when(mockEnvironment.getEnv()).thenReturn(env); - List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "HEROKU_POSTGRESQL_YELLOW"); - assertNotNull(serviceInfo); - assertTrue(serviceInfo instanceof PostgresqlServiceInfo); - assertReleationServiceInfo((PostgresqlServiceInfo)serviceInfo, "db"); - } + List serviceInfos = testCloudConnector.getServiceInfos(); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "HEROKU_POSTGRESQL_YELLOW"); + assertNotNull(serviceInfo); + assertTrue(serviceInfo instanceof PostgresqlServiceInfo); + assertReleationServiceInfo((PostgresqlServiceInfo) serviceInfo, "db"); + } } diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorRedisServiceTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorRedisServiceTest.java index 2879f17..d1ccaee 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorRedisServiceTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorRedisServiceTest.java @@ -14,42 +14,40 @@ import org.springframework.cloud.service.ServiceInfo; import org.springframework.cloud.service.common.RedisServiceInfo; /** - * * @author Ramnivas Laddad - * */ public class HerokuConnectorRedisServiceTest extends AbstractHerokuConnectorTest { - @Test - public void redisServiceCreation() { - for (String redisEnv: new String[]{"REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL"}) { - Map env = new HashMap(); - String redisUrl = getRedisServiceUrl(); - env.put(redisEnv, redisUrl); - when(mockEnvironment.getEnv()).thenReturn(env); - - List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, redisEnv.substring(0, redisEnv.length()-4)); - assertNotNull(serviceInfo); - assertTrue(serviceInfo instanceof RedisServiceInfo); - assertRedisServiceInfo((RedisServiceInfo)serviceInfo); - } - } - - private String getRedisServiceUrl() { - String template = "redis://$username:$password@$hostname:$port"; + @Test + public void redisServiceCreation() { + for (String redisEnv : new String[]{"REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL"}) { + Map env = new HashMap(); + String redisUrl = getRedisServiceUrl(); + env.put(redisEnv, redisUrl); + when(mockEnvironment.getEnv()).thenReturn(env); + + List serviceInfos = testCloudConnector.getServiceInfos(); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, redisEnv.substring(0, redisEnv.length() - 4)); + assertNotNull(serviceInfo); + assertTrue(serviceInfo instanceof RedisServiceInfo); + assertRedisServiceInfo((RedisServiceInfo) serviceInfo); + } + } + + private String getRedisServiceUrl() { + String template = "redis://$username:$password@$hostname:$port"; + + return template.replace("$username", username). + replace("$password", password). + replace("$hostname", hostname). + replace("$port", Integer.toString(port)); + } + + protected void assertRedisServiceInfo(RedisServiceInfo serviceInfo) { + assertEquals(hostname, serviceInfo.getHost()); + assertEquals(port, serviceInfo.getPort()); + assertEquals(username, serviceInfo.getUserName()); + assertEquals(password, serviceInfo.getPassword()); + } - return template.replace("$username", username). - replace("$password", password). - replace("$hostname", hostname). - replace("$port", Integer.toString(port)); - } - - protected void assertRedisServiceInfo(RedisServiceInfo serviceInfo) { - assertEquals(hostname, serviceInfo.getHost()); - assertEquals(port, serviceInfo.getPort()); - assertEquals(username, serviceInfo.getUserName()); - assertEquals(password, serviceInfo.getPassword()); - } - } diff --git a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/relational/BasicDbcpPooledDataSourceCreator.java b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/relational/BasicDbcpPooledDataSourceCreator.java index 56d3f5d..0eb5d37 100644 --- a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/relational/BasicDbcpPooledDataSourceCreator.java +++ b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/relational/BasicDbcpPooledDataSourceCreator.java @@ -15,21 +15,21 @@ import org.springframework.cloud.service.common.RelationalServiceInfo; */ public class BasicDbcpPooledDataSourceCreator extends DbcpLikePooledDataSourceCreator { - @Override - public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig, - String driverClassName, String validationQuery) { - if (hasClass("org.apache.commons.dbcp2.BasicDataSource")) { - logger.info("Found DBCP2 on the classpath. Using it for DataSource connection pooling."); - org.apache.commons.dbcp2.BasicDataSource ds = new org.apache.commons.dbcp2.BasicDataSource(); - setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery); - return ds; - } else if (hasClass("org.apache.commons.dbcp.BasicDataSource")) { - logger.info("Found DBCP on the classpath. Using it for DataSource connection pooling."); - org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource(); - setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery); - return ds; - } else { - return null; - } - } + @Override + public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig, + String driverClassName, String validationQuery) { + if (hasClass("org.apache.commons.dbcp2.BasicDataSource")) { + logger.info("Found DBCP2 on the classpath. Using it for DataSource connection pooling."); + org.apache.commons.dbcp2.BasicDataSource ds = new org.apache.commons.dbcp2.BasicDataSource(); + setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery); + return ds; + } else if (hasClass("org.apache.commons.dbcp.BasicDataSource")) { + logger.info("Found DBCP on the classpath. Using it for DataSource connection pooling."); + org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource(); + setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery); + return ds; + } else { + return null; + } + } } diff --git a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/CommonPoolCloudConfigTestHelper.java b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/CommonPoolCloudConfigTestHelper.java index 998a24f..ca6f5f8 100644 --- a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/CommonPoolCloudConfigTestHelper.java +++ b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/CommonPoolCloudConfigTestHelper.java @@ -5,21 +5,21 @@ import static org.junit.Assert.assertEquals; import org.springframework.test.util.ReflectionTestUtils; public class CommonPoolCloudConfigTestHelper { - - public static void assertCommonsPoolProperties(Object pool, int maxActive, int minIdle, long maxWait) { - assertEquals(maxActive, getValue(pool, "maxActive", "maxTotal")); - assertEquals(minIdle, getValue(pool, "minIdle")); - assertEquals(maxWait, getValue(pool, "maxWait", "maxWaitMillis")); - } - - protected static Object getValue(Object object, String... fieldNames) { - for (String fieldName : fieldNames) { - try { - return ReflectionTestUtils.getField(object, fieldName); - } catch (IllegalArgumentException ex) { - } - } - return null; - } + + public static void assertCommonsPoolProperties(Object pool, int maxActive, int minIdle, long maxWait) { + assertEquals(maxActive, getValue(pool, "maxActive", "maxTotal")); + assertEquals(minIdle, getValue(pool, "minIdle")); + assertEquals(maxWait, getValue(pool, "maxWait", "maxWaitMillis")); + } + + protected static Object getValue(Object object, String... fieldNames) { + for (String fieldName : fieldNames) { + try { + return ReflectionTestUtils.getField(object, fieldName); + } catch (IllegalArgumentException ex) { + } + } + return null; + } } diff --git a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/java/CloudScanJavaConfigTest.java b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/java/CloudScanJavaConfigTest.java index 548e1ab..7b8cbda 100644 --- a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/java/CloudScanJavaConfigTest.java +++ b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/config/java/CloudScanJavaConfigTest.java @@ -21,11 +21,11 @@ public class CloudScanJavaConfigTest extends AbstractCloudConfigServiceScanTest } @Test - public void cloudScanIntroducesApplicationInstanceInfo() { - ApplicationContext testContext = getTestApplicationContext(); - - assertNotNull(testContext.getBean(ApplicationInstanceInfo.class)); - } + public void cloudScanIntroducesApplicationInstanceInfo() { + ApplicationContext testContext = getTestApplicationContext(); + + assertNotNull(testContext.getBean(ApplicationInstanceInfo.class)); + } } @Configuration diff --git a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithDefaultDriverTest.java b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithDefaultDriverTest.java index 46cadac..104f99c 100644 --- a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithDefaultDriverTest.java +++ b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithDefaultDriverTest.java @@ -7,8 +7,8 @@ package org.springframework.cloud.service.relational; * */ public class MysqlServiceCreatorWithDefaultDriverTest extends MysqlServiceCreatorTest { - private static final String MYSQL_DRIVER_CLASS_NAME = "org.mariadb.jdbc.Driver"; - + private static final String MYSQL_DRIVER_CLASS_NAME = "org.mariadb.jdbc.Driver"; + @Override public String getDriverName() { return MYSQL_DRIVER_CLASS_NAME; diff --git a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithMysqlDriverTest.java b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithMysqlDriverTest.java index df0f9bd..5df28e4 100644 --- a/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithMysqlDriverTest.java +++ b/spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/relational/MysqlServiceCreatorWithMysqlDriverTest.java @@ -9,17 +9,17 @@ import org.junit.Before; * */ public class MysqlServiceCreatorWithMysqlDriverTest extends MysqlServiceCreatorTest { - private static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; - + private static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; + @Before public void setup() { super.setup(); - System.setProperty("spring-cloud.mysql.driver", MYSQL_DRIVER_CLASS_NAME); + System.setProperty("spring-cloud.mysql.driver", MYSQL_DRIVER_CLASS_NAME); } @After public void tearDown() { - System.setProperty("spring-cloud.mysql.driver", ""); + System.setProperty("spring-cloud.mysql.driver", ""); } @Override