diff --git a/core/README.md b/core/README.md
index c9f17f4..c99448f 100644
--- a/core/README.md
+++ b/core/README.md
@@ -22,7 +22,7 @@ Usage pattern: Application Developers
* Obtain a suitable `Cloud` for the environment in which the application is running.
```java
- Cloud cloud = cloudFoundry.getCloud();
+ Cloud cloud = cloudFactory.getCloud();
```
* Use the `Cloud` instance to get access to application info, service infos, and create service connectors.
diff --git a/core/src/main/java/org/springframework/cloud/Cloud.java b/core/src/main/java/org/springframework/cloud/Cloud.java
index 25c5828..c41f744 100644
--- a/core/src/main/java/org/springframework/cloud/Cloud.java
+++ b/core/src/main/java/org/springframework/cloud/Cloud.java
@@ -29,8 +29,9 @@ import org.springframework.cloud.service.ServiceInfo.ServiceProperty;
* It also passes along information obtained through {@link CloudConnector} to let application take control on how
* to use bound services.
*
- *
NOTE: Users or cloud providers shouldn't need to instantiate an instance of this class (constructor is package-accessed).
- * Instead, they can obtain an appropriate instance through {@link CloudFactory}
+ * NOTE: Users or cloud providers shouldn't need to instantiate an instance of this class
+ * (constructor has package-access only for unit-testing purpose). Instead, they can obtain an appropriate
+ * instance through {@link CloudFactory}
*
* @author Ramnivas Laddad
*
@@ -62,14 +63,6 @@ public class Cloud {
return cloudConnector.getApplicationInstanceInfo();
}
- /**
- * @see CloudConnector#getServiceInfos()
- * @return
- */
- public List getServiceInfos() {
- return cloudConnector.getServiceInfos();
- }
-
/**
* Get {@link ServiceInfo} for the given service id
*
@@ -85,6 +78,14 @@ public class Cloud {
throw new CloudException("No service with id " + serviceId + " found");
}
+ /**
+ * @see CloudConnector#getServiceInfos()
+ * @return
+ */
+ public List getServiceInfos() {
+ return cloudConnector.getServiceInfos();
+ }
+
/**
* Get {@link ServiceInfo}s for the bound services that could be mapped to the given service connector type.
*
@@ -113,12 +114,35 @@ public class Cloud {
/**
* Get a service connector for the given service id, the connector type, configured with the given config
*
- * Any of the arguments may be null, in which case the corresponding filtering or configuration isn't applied.
+ * @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).
+ *
*/
public SC getServiceConnector(String serviceId, Class serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) {
ServiceInfo serviceInfo = getServiceInfo(serviceId);
- ServiceConnectorCreator serviceConnectorCreator = serviceConnectorCreatorRegistry.getServiceCreator(serviceConnectorType, serviceInfo);
- return serviceConnectorCreator.create(serviceInfo, serviceConnectorConfig);
+
+ return getServiceConnector(serviceInfo, serviceConnectorType, serviceConnectorConfig);
+ }
+
+ /**
+ * Get the singleton service connector for the given connector type, configured with the given config
+ *
+
+ * @param serviceConnectorType The expected class of service connector such as, DataSource.class.
+ * @param serviceConnectorConfig service connector configuration (such as pooling parameters).
+ *
+ */
+ 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());
+ }
+
+ ServiceInfo matchingServiceInfo = matchingServiceInfos.get(0);
+
+ return getServiceConnector(matchingServiceInfo, serviceConnectorType, serviceConnectorConfig);
}
/**
@@ -204,6 +228,11 @@ public class Cloud {
return cloudProperties;
}
+ 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.";
diff --git a/core/src/test/java/org/springframework/cloud/CloudTest.java b/core/src/test/java/org/springframework/cloud/CloudTest.java
index 4b49795..c37a35b 100644
--- a/core/src/test/java/org/springframework/cloud/CloudTest.java
+++ b/core/src/test/java/org/springframework/cloud/CloudTest.java
@@ -59,6 +59,37 @@ public class CloudTest {
assertStubService(testServiceInfo, connector, null);
}
+ @Test
+ public void getSingletonServiceConnectorSingleService() {
+ StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo);
+ serviceCreators.add(new StubServiceConnectorCreator());
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators);
+
+ StubServiceConnector connector = testCloud.getSingletonServiceConnector(null, null);
+
+ assertStubService(testServiceInfo, connector, null);
+ }
+
+ @Test(expected=CloudException.class)
+ public void getSingletonServiceConnectorNoService() {
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector();
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators);
+
+ testCloud.getSingletonServiceConnector(null, null);
+ }
+
+ @Test(expected=CloudException.class)
+ public void getSingletonServiceConnectorMultipleServices() {
+ StubServiceInfo testServiceInfo1 = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
+ StubServiceInfo testServiceInfo2 = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo1, testServiceInfo2);
+ serviceCreators.add(new StubServiceConnectorCreator());
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators);
+
+ testCloud.getSingletonServiceConnector(null, null);
+ }
+
@Test
public void serviceConnectorCreationSpecifiedTypeAndConfig() {
StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");