Introduced getSingletonServiceConnector in Cloud.
This simplifies usage in common situations where only one service of a given type is bound to the app.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>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}</p>
|
||||
* <p>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}</p>
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
@@ -62,14 +63,6 @@ public class Cloud {
|
||||
return cloudConnector.getApplicationInstanceInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see CloudConnector#getServiceInfos()
|
||||
* @return
|
||||
*/
|
||||
public List<ServiceInfo> 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<ServiceInfo> 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> SC getServiceConnector(String serviceId, Class<SC> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) {
|
||||
ServiceInfo serviceInfo = getServiceInfo(serviceId);
|
||||
ServiceConnectorCreator<SC, ServiceInfo> 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> SC getSingletonServiceConnector(Class<SC> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) {
|
||||
List<ServiceInfo> 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> SC getServiceConnector(ServiceInfo serviceInfo, Class<SC> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig) {
|
||||
ServiceConnectorCreator<SC, ServiceInfo> serviceConnectorCreator = serviceConnectorCreatorRegistry.getServiceCreator(serviceConnectorType, serviceInfo);
|
||||
return serviceConnectorCreator.create(serviceInfo, serviceConnectorConfig);
|
||||
}
|
||||
|
||||
private Properties getAppProperties() {
|
||||
final String appPropLeadKey = "cloud.application.";
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user