private void registerServiceBeans(Cloud cloud, ConfigurableListableBeanFactory beanFactory) { List<ServiceInfo> serviceInfos = cloud.getServiceInfos(); for(ServiceInfo serviceInfo: serviceInfos) { registerServiceBean(beanFactory, serviceInfo); } }
/** * 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); }
/** * Get the singleton service connector for the given connector type, configured with the given config * * @param <SC> The class of the service connector to return. * @param serviceConnectorType The expected class of service connector such as, DataSource.class. * @param serviceConnectorConfig service connector configuration (such as pooling parameters). * @return the single service connector of the specified type with the given configuration applied * */ 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); }
/** * Get the singleton service connector for the given connector type, configured with the given config * * @param <SC> The class of the service connector to return. * @param serviceConnectorType The expected class of service connector such as, DataSource.class. * @param serviceConnectorConfig service connector configuration (such as pooling parameters). * @return the single service connector of the specified type with the given configuration applied * */ 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); }
/** * Get {@link ServiceInfo} for the given service id * * @param serviceId service id * @return info for the serviceId */ public ServiceInfo getServiceInfo(String serviceId) { for (ServiceInfo serviceInfo : getServiceInfos()) { if (serviceInfo.getId().equals(serviceId)) { return serviceInfo; } } throw new CloudException("No service with id " + serviceId + " found"); }
/** * Get {@link ServiceInfo} for the given service id * * @param serviceId service id * @return info for the serviceId */ public ServiceInfo getServiceInfo(String serviceId) { for (ServiceInfo serviceInfo : getServiceInfos()) { if (serviceInfo.getId().equals(serviceId)) { return serviceInfo; } } throw new CloudException("No service with id " + serviceId + " found"); }
/** * Get the singleton service connector for the given connector type, configured with the given config * * @param <SC> The class of the service connector to return. * @param serviceConnectorType The expected class of service connector such as, DataSource.class. * @param serviceConnectorConfig service connector configuration (such as pooling parameters). * @return the single service connector of the specified type with the given configuration applied * */ 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); }
/** * Get {@link ServiceInfo} for the given service id * * @param serviceId service id * @return info for the serviceId */ public ServiceInfo getServiceInfo(String serviceId) { for (ServiceInfo serviceInfo : getServiceInfos()) { if (serviceInfo.getId().equals(serviceId)) { return serviceInfo; } } throw new CloudException("No service with id " + serviceId + " found"); }
public void registerServiceBeans(BeanDefinitionRegistry registry) { initializeCloud(registry); List<ServiceInfo> serviceInfos = cloud.getServiceInfos(); for(ServiceInfo serviceInfo: serviceInfos) { registerServiceBean(registry, serviceInfo); } }
/** * Get {@link ServiceInfo} for the given service id * * @param serviceId service id * @return info for the serviceId */ public ServiceInfo getServiceInfo(String serviceId) { for (ServiceInfo serviceInfo : getServiceInfos()) { if (serviceInfo.getId().equals(serviceId)) { return serviceInfo; } } throw new CloudException("No service with id " + serviceId + " found"); }
public void registerServiceBeans(BeanDefinitionRegistry registry) { initializeCloud(registry); List<ServiceInfo> serviceInfos = cloud.getServiceInfos(); for(ServiceInfo serviceInfo: serviceInfos) { registerServiceBean(registry, serviceInfo); } }
@Override public void afterPropertiesSet() throws Exception { ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory(); if (cloud == null) { if(beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) { beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory()); } CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next(); cloud = cloudFactory.getCloud(); } if (!StringUtils.hasText(serviceId)) { List<? extends ServiceInfo> infos = cloud.getServiceInfos(serviceConnectorType); if (infos.size() != 1) { throw new CloudException("Expected 1 service matching " + serviceConnectorType.getName() + " type, but found " + infos.size()); } serviceId = infos.get(0).getId(); } super.afterPropertiesSet(); }
@Override public void afterPropertiesSet() throws Exception { ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory(); if (cloud == null) { if (beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) { beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory()); } CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next(); cloud = cloudFactory.getCloud(); } if (!StringUtils.hasText(serviceId)) { List<? extends ServiceInfo> infos = cloud.getServiceInfos(serviceConnectorType); if (infos.size() != 1) { throw new CloudException("Expected 1 service matching " + serviceConnectorType.getName() + " type, but found " + infos.size()); } serviceId = infos.get(0).getId(); } super.afterPropertiesSet(); }
@Override public void afterPropertiesSet() throws Exception { ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory(); if (cloud == null) { if (beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) { beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory()); } CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next(); cloud = cloudFactory.getCloud(); } if (!StringUtils.hasText(serviceId)) { List<? extends ServiceInfo> infos = cloud.getServiceInfos(serviceConnectorType); if (infos.size() != 1) { throw new CloudException("Expected 1 service matching " + serviceConnectorType.getName() + " type, but found " + infos.size()); } serviceId = infos.get(0).getId(); } super.afterPropertiesSet(); }
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 public void serviceInfoNoServices() { StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); assertEquals(0, testCloud.getServiceInfos().size()); assertEquals(0, testCloud.getServiceInfos(StubServiceInfo.class).size()); }
@Test public void serviceInfoMultipleServicesOfTheSameType() { StubServiceInfo testServiceInfo1 = new StubServiceInfo("test-id1", "test-host", 1000, "test-username", "test-password"); StubServiceInfo testServiceInfo2 = new StubServiceInfo("test-id2", "test-host", 1000, "test-username", "test-password"); StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo1, testServiceInfo2); serviceConnectorCreators.add(new StubServiceConnectorCreator()); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); assertEquals(2, testCloud.getServiceInfos().size()); assertEquals(2, testCloud.getServiceInfos(StubServiceConnector.class).size()); }
@Before public void init() throws Exception { when(configurableEnvironment.getActiveProfilesContainer()).thenCallRealMethod(); when(configurableEnvironment.getActiveProfiles()).thenCallRealMethod(); doCallRealMethod().when(configurableEnvironment).setActiveProfilesContainer(any()); doCallRealMethod().when(configurableEnvironment).addActiveProfile(any(String.class)); configurableEnvironment.setActiveProfilesContainer(new HashSet<>()); when(applicationContext.getEnvironment()).thenReturn(configurableEnvironment); when(cloud.getServiceInfos()).thenReturn( Collections.singletonList( new MysqlServiceInfo("mysql", "localhost"))); doCallRealMethod().when(contextInitializer).initialize(any(GenericApplicationContext.class)); }
@Test public void serviceInfosForConnectorType() { String serviceId = "mysql-db"; CloudConnector stubCloudConnector = getTestCloudConnector(createMysqlService(serviceId)); Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators); List<ServiceInfo> serviceInfos = testCloud.getServiceInfos(DataSource.class); assertEquals(1, serviceInfos.size()); }
@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()); }