@Override public <T> T service(String serviceId, Class<T> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig){ return cloud.getServiceConnector(serviceId, serviceConnectorType, serviceConnectorConfig); } }
/** * Get the service connector object of the specified type and service id * * This is equivalent to the <cloud:service service-id="serviceId" connector-type="T.class"/> element. * * @return service connector object * @throws CloudException if the specified service doesn't exist */ public <T> T service(String serviceId, Class<T> serviceConnectorType) { return cloud.getServiceConnector(serviceId, serviceConnectorType, null); } }
@Override public <T> T service(String serviceId, Class<T> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig){ return cloud.getServiceConnector(serviceId, serviceConnectorType, serviceConnectorConfig); } }
/** * Get the {@link Cluster} object associated with the Cassandra {@code serviceId} * bound to the app configured as specified. * * @param serviceId the Cassandra serviceId * @param config configuration for the Cluster created * @return the Cassandra {@link Cluster} * @throws org.springframework.cloud.CloudException if there is no service bound with * the {@code serviceId} */ @Override public Cluster cluster(String serviceId, CassandraClusterConfig config) { return cloud.getServiceConnector(serviceId, Cluster.class, config); }
/** * Get the {@link MongoDbFactory} object for the specified MongoDB service configured as specified. * * This is equivalent to the <cloud:mongo-db-factory service-id="serviceId"> element * with a nested <cloud:mongo-options> element. * * @param serviceId the name of the service * @param mongoDbFactoryConfig configuration for the mongo db factory created * @return mongo db factory * @throws CloudException if the specified service doesn't exist */ public MongoDbFactory mongoDbFactory(String serviceId, MongoDbFactoryConfig mongoDbFactoryConfig) { return cloud.getServiceConnector(serviceId, MongoDbFactory.class, mongoDbFactoryConfig); }
/** * Get the {@link RedisConnectionFactory} object for the specified Redis service configured as specified. * * This is equivalent to the <cloud:redis-connection-factory service-id="serviceId"> element * with a nested <cloud:pool> element. * * @param serviceId the name of the service * @param redisConnectionFactoryConfig configuration for the {@link RedisConnectionFactory} created * @return redis connection factory * @throws CloudException if the specified service doesn't exist */ public RedisConnectionFactory redisConnectionFactory(String serviceId, PooledServiceConnectorConfig redisConnectionFactoryConfig) { return cloud.getServiceConnector(serviceId, RedisConnectionFactory.class, redisConnectionFactoryConfig); }
/** * Get a service connector for the given service id, the connector type, configured with the given config * * @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); return getServiceConnector(serviceInfo, serviceConnectorType, serviceConnectorConfig); }
@SuppressWarnings("unchecked") public S createService() { if (serviceInstance == null && cloud != null) { serviceInstance = cloud.getServiceConnector(serviceId, serviceConnectorType, serviceConnectorConfiguration); if (serviceInstance != null && FactoryBean.class.isAssignableFrom(serviceInstance.getClass())) { try { serviceInstance = (S) ((FactoryBean) serviceInstance).getObject(); } catch (Exception e) { return null; } } } return serviceInstance; }
@SuppressWarnings("unchecked") public S createService() { if (serviceInstance == null && cloud != null) { serviceInstance = cloud.getServiceConnector(serviceId, serviceConnectorType, serviceConnectorConfiguration); if (serviceInstance != null && FactoryBean.class.isAssignableFrom(serviceInstance.getClass())) { try { serviceInstance = (S) ((FactoryBean) serviceInstance).getObject(); } catch (Exception e) { return null; } } } return serviceInstance; }
private DataSource getCloudDataSource(String serviceName) { DataSource dataSource = null; try { if (serviceName != null && !serviceName.isEmpty()) { int maxPoolSize = configuration.getDbConnectionThreads(); DataSourceConfig config = new DataSourceConfig(new PoolConfig(maxPoolSize, 30000), null); dataSource = getSpringCloud().getServiceConnector(serviceName, DataSource.class, config); } } catch (CloudException e) { // Do nothing } return dataSource; }
@Test public void creatingBoundServiceWithoutConnectorType() throws Exception { when(mockCloud.getServiceConnector(serviceId, null, null)).thenReturn(mockDataSource); GenericCloudServiceConnectorFactory testFactory = new GenericCloudServiceConnectorFactory(serviceId, null); testFactory.setCloud(mockCloud); testFactory.afterPropertiesSet(); assertSame(mockDataSource, testFactory.getObject()); }
@Test public void creatingServiceConnectorWithoutIdReturnsTheSignletonService() throws Exception { String id = "my-service"; List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>(); serviceInfos.add(getTestServiceInfo(id)); when(mockCloud.getServiceConnector(id, getConnectorType(), null)).thenReturn(getMockConnector()); when(mockCloud.getServiceInfos(getConnectorType())).thenReturn(serviceInfos); SCF testSCF = createTestCloudServiceConnectorFactory(null, null); testSCF.setCloud(mockCloud); testSCF.afterPropertiesSet(); assertSame(getMockConnector(), testSCF.getObject()); }
@Test public void creatingBoundServiceWithConnectorType() throws Exception { when(mockCloud.getServiceConnector(serviceId, DataSource.class, null)).thenReturn(mockDataSource); GenericCloudServiceConnectorFactory testFactory = new GenericCloudServiceConnectorFactory(serviceId, null); testFactory.setServiceConnectorType(DataSource.class); testFactory.setCloud(mockCloud); testFactory.afterPropertiesSet(); assertSame(mockDataSource, testFactory.getObject()); }
@Test public void creatingBoundServiceWithIncorrectConnectorType() throws Exception { when(mockCloud.getServiceConnector(serviceId, DataSource.class, null)).thenReturn(mockDataSource); GenericCloudServiceConnectorFactory testFactory = new GenericCloudServiceConnectorFactory(serviceId, null); testFactory.setServiceConnectorType(String.class); testFactory.setCloud(mockCloud); testFactory.afterPropertiesSet(); assertNull(testFactory.getObject()); }
@Test public void creatingNonExistingServiceShouldFail() throws Exception { String id = "my-service"; when(mockCloud.getServiceConnector(id, getConnectorType(), null)).thenReturn(null); SCF testSCF = createTestCloudServiceConnectorFactory(id, null); testSCF.setCloud(mockCloud); testSCF.afterPropertiesSet(); assertNull(testSCF.getObject()); }
@Test public void creatingBoundServiceWithFactoryConnectorType() throws Exception { when(mockCloud.getServiceConnector(serviceId, DataSourceFactory.class, null)).thenReturn(new DataSourceFactory()); GenericCloudServiceConnectorFactory testFactory = new GenericCloudServiceConnectorFactory(serviceId, null); testFactory.setServiceConnectorType(DataSourceFactory.class); testFactory.setCloud(mockCloud); testFactory.afterPropertiesSet(); assertSame(mockDataSource, testFactory.getObject()); }
@Test public void serviceConnectorCreation() { String serviceId = "mysql-db"; CloudConnector stubCloudConnector = getTestCloudConnector(createMysqlService(serviceId)); Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators); DataSource serviceConnector = testCloud.getServiceConnector(serviceId, DataSource.class, null); assertNotNull(serviceConnector); }
@Test public void creatingBoundService() throws Exception { String id = "my-service"; when(mockCloud.getServiceConnector(id, getConnectorType(), null)).thenReturn(getMockConnector()); SCF testSCF = createTestCloudServiceConnectorFactory(id, null); testSCF.setCloud(mockCloud); testSCF.afterPropertiesSet(); assertSame(getMockConnector(), testSCF.getObject()); }
@Test public void serviceConnectorCreationDefaultTypeAndConfig() { StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password"); StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo); serviceConnectorCreators.add(new StubServiceConnectorCreator()); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); StubServiceConnector connector = testCloud.getServiceConnector(testServiceInfo.getId(), null, null); assertStubService(testServiceInfo, connector, null); }
@Test public void serviceConnectorCreationSpecifiedTypeAndConfig() { StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password"); StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo); serviceConnectorCreators.add(new StubServiceConnectorCreator()); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); StubServiceConnectorConfig config = new StubServiceConnectorConfig("test-config"); StubServiceConnector connector = testCloud.getServiceConnector(testServiceInfo.getId(), StubServiceConnector.class, config); assertStubService(testServiceInfo, connector, config); }