/** * Get the service connector object of the specified type if there is only one such candidate service * * This is equivalent to the <cloud:service connector-type="T.class"/> element. * * @return service connector object * @throws CloudException if there are either 0 or more than 1 candidate services. */ public <T> T service(Class<T> serviceConnectorType) { return cloud.getSingletonServiceConnector(serviceConnectorType, null); }
/** * Get the {@link Cluster} object associated with the only Cassandra service bound to * the app configured as specified. * * @param config configuration for the Cluster created * @return the Cassandra {@link Cluster} * @throws org.springframework.cloud.CloudException if there are either 0 or more than * 1 Cassandra database services. */ @Override public Cluster cluster(CassandraClusterConfig config) { return cloud.getSingletonServiceConnector(Cluster.class, config); }
/** * Get the {@link Cluster} object associated with the only Cassandra service bound to * the app configured as specified. * * @param config configuration for the Cluster created * @return the Cassandra {@link Cluster} * @throws org.springframework.cloud.CloudException if there are either 0 or more than * 1 Cassandra database services. */ @Override public Cluster cluster(CassandraClusterConfig config) { return cloud.getSingletonServiceConnector(Cluster.class, config); }
/** * Get the {@link MongoDbFactory} object associated with the only MongoDB service bound to the app * configured as specified. * * This is equivalent to the {@code <cloud:mongo-db-factory>} element with a nested {@code <cloud:mongo-options>} element. * * @param mongoDbFactoryConfig * configuration for the mondo db factory created * @return mongo db factory * @throws CloudException * if there are either 0 or more than 1 mongodb services. */ @Override public MongoDbFactory mongoDbFactory(MongoDbFactoryConfig mongoDbFactoryConfig) { return cloud.getSingletonServiceConnector(MongoDbFactory.class, mongoDbFactoryConfig); }
/** * Get the {@link RedisConnectionFactory} object associated with the only Redis service bound to the app * configured as specified. * * This is equivalent to the {@code <cloud:redis-connection-factory service-id="serviceId">} element * with a nested {@code <cloud:pool>} element. * * @param redisConnectionFactoryConfig * configuration for the {@link RedisConnectionFactory} created * @return redis connection factory * @throws CloudException * if there are either 0 or more than 1 redis services. */ @Override public RedisConnectionFactory redisConnectionFactory(PooledServiceConnectorConfig redisConnectionFactoryConfig) { return cloud.getSingletonServiceConnector(RedisConnectionFactory.class, redisConnectionFactoryConfig); }
/** * Get the {@link MongoDbFactory} object associated with the only MongoDB service bound to the app * configured as specified. * * This is equivalent to the <cloud:mongo-db-factory> element with a nested <cloud:mongo-options> element. * * @param mongoDbFactoryConfig configuration for the mondo db factory created * @return mongo db factory * @throws CloudException if there are either 0 or more than 1 mongodb services. */ public MongoDbFactory mongoDbFactory(MongoDbFactoryConfig mongoDbFactoryConfig) { return cloud.getSingletonServiceConnector(MongoDbFactory.class, mongoDbFactoryConfig); }
/** * Get the {@link ConnectionFactory} object associated with the only RabbitMQ service bound to the app * configured as specified. * * This is equivalent to the {@code <cloud:rabbit-connection-factory>} element * with a nested {@code <cloud:rabbit-options>} element. * * @param rabbitConnectionFactoryConfig * configuration for the rabbit connection factory created * @return rabbit connection factory * @throws CloudException * if there are either 0 or more than 1 RabbitMQ services. */ @Override public ConnectionFactory rabbitConnectionFactory(RabbitConnectionFactoryConfig rabbitConnectionFactoryConfig) { return cloud.getSingletonServiceConnector(ConnectionFactory.class, rabbitConnectionFactoryConfig); }
/** * Get the {@link MongoDbFactory} object associated with the only MongoDB service bound to the app * configured as specified. * * This is equivalent to the {@code <cloud:mongo-db-factory>} element with a nested {@code <cloud:mongo-options>} element. * * @param mongoDbFactoryConfig * configuration for the mondo db factory created * @return mongo db factory * @throws CloudException * if there are either 0 or more than 1 mongodb services. */ @Override public MongoDbFactory mongoDbFactory(MongoDbFactoryConfig mongoDbFactoryConfig) { return cloud.getSingletonServiceConnector(MongoDbFactory.class, mongoDbFactoryConfig); }
/** * Get the service connector object of the specified type if there is only one such candidate service * * This is equivalent to the {@code <cloud:service connector-type="T.class"/>} element. * * @param <T> * the type of the service connector to be returned * @param serviceConnectorType * the class of the service connector to be returned * @return service connector object * @throws CloudException * if there are either 0 or more than 1 candidate services. */ @Override public <T> T service(Class<T> serviceConnectorType) { return cloud.getSingletonServiceConnector(serviceConnectorType, null); }
/** * Get the {@link DataSource} object associated with the only relational database service bound to the app * configured as specified. * * This is equivalent to the <cloud:data-source> element with a nested <cloud:connection> * and/or <cloud:pool> elements. * * @param dataSourceConfig configuration for the data source created * @return data source * @throws CloudException if there are either 0 or more than 1 relational database services. */ public DataSource dataSource(DataSourceConfig dataSourceConfig) { return cloud.getSingletonServiceConnector(DataSource.class, dataSourceConfig); }
/** * Get the {@link ConnectionFactory} object associated with the only RabbitMQ service bound to the app * configured as specified. * * This is equivalent to the <cloud:rabbit-connection-factory> element * with a nested <cloud:rabbit-options> element. * * @param rabbitConnectionFactoryConfig configuration for the rabbit connection factory created * @return rabbit connection factory * @throws CloudException if there are either 0 or more than 1 RabbitMQ services. */ public ConnectionFactory rabbitConnectionFactory(RabbitConnectionFactoryConfig rabbitConnectionFactoryConfig) { return cloud.getSingletonServiceConnector(ConnectionFactory.class, rabbitConnectionFactoryConfig); }
/** * Get the {@link RedisConnectionFactory} object associated with the only Redis service bound to the app * configured as specified. * * This is equivalent to the <cloud:redis-connection-factory service-id="serviceId"> element * with a nested <cloud:pool> element. * * @param redisConnectionFactoryConfig configuration for the {@link RedisConnectionFactory} created * @return redis connection factory * @throws CloudException if there are either 0 or more than 1 redis services. */ public RedisConnectionFactory redisConnectionFactory(PooledServiceConnectorConfig redisConnectionFactoryConfig) { return cloud.getSingletonServiceConnector(RedisConnectionFactory.class, redisConnectionFactoryConfig); }
@Bean RedisConnectionFactory redisConnectionFactory(Cloud cloud) { return cloud.getSingletonServiceConnector(RedisConnectionFactory.class, null); } }
@Bean ConnectionFactory redisConnectionFactory(Cloud cloud) { return cloud.getSingletonServiceConnector(ConnectionFactory.class, null); } }
@Bean public DataSource dataSource() { return cloud().getSingletonServiceConnector(DataSource.class, null); }
public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( VaultConnectorsConfig.class, VaultConfig.class); context.start(); Cloud cloud = context.getBean(Cloud.class); SslConfiguration sslConfiguration = context.getBean(SslConfiguration.class); VaultServiceInfo vaultServiceInfo = (VaultServiceInfo) cloud .getServiceInfos(VaultOperations.class).get(0); VaultServiceConnectorConfig config = VaultServiceConnectorConfig.builder() .sslConfiguration(sslConfiguration).build(); VaultOperations vaultOperations = cloud .getSingletonServiceConnector(VaultOperations.class, config); VaultResponse response = vaultOperations .read(vaultServiceInfo.getBackends().get("generic") + "/application"); log.info("Retrieved app-key: {}", response.getData().get("app-key")); context.stop(); }
@Test(expected=CloudException.class) public void getSingletonServiceConnectorNoService() { StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); testCloud.getSingletonServiceConnector(null, null); }
@Test(expected=CloudException.class) public void getSingletonServiceConnectorNoMatchingServiceConnectorCreator() { // Think an app bound to a (user) service that doesn't have a corresponding // registered ServiceConnectorCreator. When user asks for singleton service connector // for another type (with a corresponding creator registered), // getSingletonServiceConnector() should throw a CloudException. BaseServiceInfo testServiceInfo = new BaseServiceInfo("user-service"); StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo); serviceConnectorCreators.add(new StubServiceConnectorCreator()); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); testCloud.getSingletonServiceConnector(StubServiceConnector.class, null); }
@Test public void getSingletonServiceConnectorSingleService() { 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.getSingletonServiceConnector(null, null); assertStubService(testServiceInfo, connector, 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); serviceConnectorCreators.add(new StubServiceConnectorCreator()); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); testCloud.getSingletonServiceConnector(null, null); }