/** * 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 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); }
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); Cloud cloud = new Cloud(); Random random = new Random(); for (String s : WORDS) { for (int i = random.nextInt(50); i > 0; i--) { cloud.addTag(s); for (Tag tag : cloud.tags()) { final JLabel label = new JLabel(tag.getName()); label.setOpaque(false);
/** * 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); }
private Properties getAppProperties() { final String appPropLeadKey = "cloud.application."; Properties appProperties = new Properties(); addProperty(appProperties, appPropLeadKey + "instance-id", getApplicationInstanceInfo().getInstanceId()); addProperty(appProperties, appPropLeadKey + "app-id", getApplicationInstanceInfo().getAppId()); if (getApplicationInstanceInfo().getProperties() != null) { for (Map.Entry<String, Object> entry : getApplicationInstanceInfo().getProperties().entrySet()) { if (entry.getValue() != null) { addProperty(appProperties, appPropLeadKey + entry.getKey(), entry.getValue()); } } } return appProperties; }
for (ServiceInfo serviceInfo : getServiceInfos()) { String key = getServiceLabel(serviceInfo); List<ServiceInfo> serviceInfosForLabel = mappedServiceInfos.computeIfAbsent(key, k -> new ArrayList<>()); serviceInfosForLabel.add(serviceInfo); cloudProperties.putAll(getServiceProperties(idBasedKey, serviceInfo)); cloudProperties.putAll(getServiceProperties(labelBasedKey, serviceInfo)); cloudProperties.putAll(getAppProperties());
private void registerServiceBeans(Cloud cloud, ConfigurableListableBeanFactory beanFactory) { List<ServiceInfo> serviceInfos = cloud.getServiceInfos(); for(ServiceInfo serviceInfo: serviceInfos) { registerServiceBean(beanFactory, serviceInfo); } }
@Override public <T> T service(String serviceId, Class<T> serviceConnectorType, ServiceConnectorConfig serviceConnectorConfig){ return cloud.getServiceConnector(serviceId, serviceConnectorType, serviceConnectorConfig); } }
@Test public void appProps() { Map<String, Object> appProps = new HashMap<>(); appProps.put("foo", "bar"); appProps.put("users", null); // on v2, users property is null StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(new StubApplicationInstanceInfo("instance-id-1", "myapp", appProps)); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); assertEquals("myapp", testCloud.getCloudProperties().get("cloud.application.app-id")); assertEquals("instance-id-1", testCloud.getCloudProperties().get("cloud.application.instance-id")); assertEquals("bar", testCloud.getCloudProperties().get("cloud.application.foo")); }
@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(expected=CloudException.class) public void getSingletonServiceConnectorNoService() { StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); testCloud.getSingletonServiceConnector(null, null); }
@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(expected=CloudException.class) public void getServiceInfoByInvalidId() { StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(); Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators); testCloud.getServiceInfo("foo"); }
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(); }
@Override public ApplicationInstanceInfo getObject() throws Exception { return cloud.getApplicationInstanceInfo(); }
@Override public Properties getObject() throws Exception { return cloud.getCloudProperties(); }
@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<TestServiceInfoTypeA> 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); }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCloud = new Cloud(); //New instance will be created } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.dream_purpose_edit,container,false); purposeEText = (EditText)v.findViewById(R.id.purpose_edit); purposeEText.setText(mCloud.getPurposeText()); // 'mCloud' is a new object everytime and hence will not have your saved value return v; }
private ObjectStoreServiceInfo getServiceInfo() { if (StringUtils.isEmpty(serviceName)) { LOGGER.warn("service name not specified in config files"); return null; } try { CloudFactory cloudFactory = new CloudFactory(); Cloud cloud = cloudFactory.getCloud(); return (ObjectStoreServiceInfo) cloud.getServiceInfo(serviceName); } catch (CloudException e) { LOGGER.warn(MessageFormat.format("Failed to detect service info for service \"{0}\"!", serviceName), e); } return null; }