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 c3b91ee..feef830 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
@@ -115,6 +115,53 @@ public class Cloud {
return matchingServiceInfos;
}
+ /**
+ * Get all {@link ServiceInfo}s for the given service info type.
+ *
+ *
+ * Unlike {@link #getServiceInfos(Class)} which checks if the service info
+ * can be mapped to the given service connector type, this method only
+ * checks the type of the service info.
+ *
+ * @param the class of service info to return
+ * @param serviceInfoType
+ * service info type
+ * @return a list of service info of the given type
+ */
+ @SuppressWarnings("unchecked")
+ public List getServiceInfosByType(Class serviceInfoType) {
+ List allServiceInfos = getServiceInfos();
+
+ List matchingServiceInfos = new ArrayList();
+ for (ServiceInfo serviceInfo : allServiceInfos) {
+ if (serviceInfoType.isAssignableFrom(serviceInfo.getClass())) {
+ matchingServiceInfos.add((T) serviceInfo);
+ }
+ }
+
+ return matchingServiceInfos;
+ }
+
+ /**
+ * Get the singleton {@link ServiceInfo} for the given service info type.
+ *
+ * @param the class of service info to return
+ * @param serviceInfoType
+ * service info type
+ * @return the single service info of the given type
+ * @throws CloudException
+ * if there are either 0 or more than 1 service info of the
+ * given type.
+ */
+ public T getSingletonServiceInfoByType(Class serviceInfoType) {
+ List serviceInfos = getServiceInfosByType(serviceInfoType);
+ if (serviceInfos.size() != 1) {
+ throw new CloudException(
+ "No unique service info " + serviceInfoType + " found. Expected 1, found " + serviceInfos.size());
+ }
+ return serviceInfos.get(0);
+ }
+
/**
* Get a service connector for the given service id, the connector type, configured with the given config
*
diff --git a/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTest.java b/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTest.java
index a9abcc3..299ec2f 100644
--- a/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTest.java
+++ b/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTest.java
@@ -16,6 +16,8 @@ import org.springframework.cloud.CloudTestUtil.StubApplicationInstanceInfo;
import org.springframework.cloud.CloudTestUtil.StubCloudConnector;
import org.springframework.cloud.CloudTestUtil.StubCompositeServiceInfo;
import org.springframework.cloud.CloudTestUtil.StubServiceInfo;
+import org.springframework.cloud.CloudTestUtil.TestServiceInfoTypeA;
+import org.springframework.cloud.CloudTestUtil.TestServiceInfoTypeB;
import org.springframework.cloud.service.BaseServiceInfo;
import org.springframework.cloud.service.ServiceConnectorConfig;
import org.springframework.cloud.service.ServiceConnectorCreator;
@@ -217,7 +219,48 @@ public class CloudTest {
assertNotNull(testCloud.getServiceInfo("test-id-2a"));
assertNotNull(testCloud.getServiceInfo("test-id-2b"));
}
-
+
+ @Test
+ public void getServiceInfosByType() {
+ StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
+ TestServiceInfoTypeA testServiceInfoTypeA1 = new TestServiceInfoTypeA("test-id-a1");
+ TestServiceInfoTypeA testServiceInfoTypeA2 = new TestServiceInfoTypeA("test-id-a2");
+ TestServiceInfoTypeB testServiceInfoTypeB = new TestServiceInfoTypeB("test-id-b");
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo, testServiceInfoTypeA1, testServiceInfoTypeA2, testServiceInfoTypeB);
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
+
+ List actualServiceInfoTypeA = testCloud.getServiceInfosByType(TestServiceInfoTypeA.class);
+ assertEquals(2, actualServiceInfoTypeA.size());
+ assertEquals(1, testCloud.getServiceInfosByType(TestServiceInfoTypeB.class).size());
+ }
+
+ @Test(expected=CloudException.class)
+ public void getSingletonServiceInfoByTypeNoService() {
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector();
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
+
+ testCloud.getSingletonServiceInfoByType(StubServiceInfo.class);
+ }
+
+ @Test(expected=CloudException.class)
+ public void getSingletonServiceInfoByTypeMultipleServices() {
+ 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);
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
+
+ testCloud.getSingletonServiceInfoByType(StubServiceInfo.class);
+ }
+
+ @Test
+ public void getSingletonServiceInfoByTypeSingleService() {
+ StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
+ StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo);
+ Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
+
+ assertNotNull(testCloud.getSingletonServiceInfoByType(StubServiceInfo.class));
+ }
+
private void assertStubServiceProp(String leadKey, StubServiceInfo serviceInfo, Properties cloudProperties) {
CloudTestUtil.assertBasicProps(leadKey, serviceInfo, cloudProperties);
diff --git a/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTestUtil.java b/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTestUtil.java
index 6a7eb14..eabcee8 100644
--- a/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTestUtil.java
+++ b/spring-cloud-core/src/test/java/org/springframework/cloud/CloudTestUtil.java
@@ -9,12 +9,12 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
-import org.springframework.cloud.CloudConnector;
import org.springframework.cloud.app.ApplicationInstanceInfo;
+import org.springframework.cloud.service.BaseServiceInfo;
import org.springframework.cloud.service.CompositeServiceInfo;
-import org.springframework.cloud.service.UriBasedServiceInfo;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
+import org.springframework.cloud.service.UriBasedServiceInfo;
/**
* Class to support testing.
@@ -98,6 +98,18 @@ public class CloudTestUtil {
}
}
+ public static class TestServiceInfoTypeA extends BaseServiceInfo {
+ public TestServiceInfoTypeA(String id) {
+ super(id);
+ }
+ }
+
+ public static class TestServiceInfoTypeB extends BaseServiceInfo {
+ public TestServiceInfoTypeB(String id) {
+ super(id);
+ }
+ }
+
public static class StubCompositeServiceInfo implements CompositeServiceInfo {
private String id;
private List constituents;