Java Code Examples for org.springframework.cloud.CloudException

The following examples show how to use org.springframework.cloud.CloudException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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();
        ServiceInfo serviceInfo = cloud.getServiceInfo(serviceName);
        if (serviceInfo instanceof ObjectStoreServiceInfo) {
            return (ObjectStoreServiceInfo) serviceInfo;
        }
        LOGGER.warn("Service instance did not match allowed label and plans.");
    } catch (CloudException e) {
        LOGGER.warn(MessageFormat.format("Failed to detect service info for service \"{0}\"!", serviceName), e);
    }
    return null;
}
 
Example 2
Source Project: spring-cloud-connectors   Source File: DataSourceCreator.java    License: Apache License 2.0 6 votes vote down vote up
public String getDriverClassName(SI serviceInfo) {
	String userSpecifiedDriver = System.getProperty(driverSystemPropKey);

	if (userSpecifiedDriver != null && !userSpecifiedDriver.isEmpty()) {
		return userSpecifiedDriver;
	} else {
		for (String driver : driverClasses) {
			try {
				Class.forName(driver);
				return driver;
			} catch (ClassNotFoundException ex) {
				// continue...
			}
		}
	}
	throw new CloudException("No suitable database driver found for " + serviceInfo.getId() + " service ");
}
 
Example 3
@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();
}
 
Example 4
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	super.doParse(element, parserContext, builder);

	String connectorTypeName = element.getAttribute(CONNECTOR_TYPE);
	if (StringUtils.hasText(connectorTypeName)) {
		try {
			connectorType = Class.forName(connectorTypeName);
			builder.addPropertyValue("serviceConnectorType", connectorType);
		} catch (ClassNotFoundException ex) {
			throw new CloudException("Failed to load " + connectorTypeName, ex);
		}
	}

	// TBD: Support generic (map-based?) service config
	builder.addConstructorArgValue(null);
}
 
Example 5
/**
 * Return object representation of the VCAP_SERVICES environment variable
 * <p>
 * Returns a list whose element is a map with service attributes. 
 * </p>
 * @return parsed service data
 */
@SuppressWarnings("unchecked")
protected List<Map<String,Object>> getServicesData() {
	String servicesString = environment.getEnvValue("VCAP_SERVICES");
	CloudFoundryRawServiceData rawServices = new CloudFoundryRawServiceData();
	
	if (servicesString != null && servicesString.length() > 0) {
		try {
			rawServices = objectMapper.readValue(servicesString, CloudFoundryRawServiceData.class);
		} catch (Exception e) {
			throw new CloudException(e);
		}
	}

	for (ServiceDataPostProcessor postProcessor : serviceDataPostProcessors) {
		rawServices = postProcessor.process(rawServices);
	}

	List<Map<String, Object>> flatServices = new ArrayList<Map<String, Object>>();
	for (Map.Entry<String, List<Map<String,Object>>> entry : rawServices.entrySet()) {
		flatServices.addAll(entry.getValue());
	}

	return flatServices;
}
 
Example 6
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example 7
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example 8
/**
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example 9
/** 
 * Returns cloud object if the application runs on cloud environment
 * 
 * @return cloud object if the application runs on cloud environment
 */
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example 10
private Cloud getCloud() {
	try {
		CloudFactory cloudFactory = new CloudFactory();
		return cloudFactory.getCloud();
	} catch (CloudException ce) {
		logger.error("no suitable cloud found");
		return null;
	}
}
 
Example 11
private DynatraceServiceInfo getDynatraceServiceInfo() {
    String serviceName = DynatraceServiceInfoCreator.DEFAULT_DYNATRACE_SERVICE_ID;
    try {
        CloudFactory cloudFactory = new CloudFactory();
        Cloud cloud = cloudFactory.getCloud();
        ServiceInfo serviceInfo = cloud.getServiceInfo(serviceName);
        if (serviceInfo instanceof DynatraceServiceInfo) {
            return (DynatraceServiceInfo) serviceInfo;
        }
        LOGGER.warn("Service instance did not match allowed name and type.");
    } catch (CloudException e) {
        LOGGER.warn(MessageFormat.format("Failed to detect service info for service \"{0}\"!", serviceName), e);
    }
    return null;
}
 
Example 12
private DataSource getCloudDataSource(String serviceName) {
    DataSource cloudDataSource = null;
    try {
        if (serviceName != null && !serviceName.isEmpty()) {
            int maxPoolSize = configuration.getDbConnectionThreads();
            DataSourceConfig config = new DataSourceConfig(new PoolConfig(maxPoolSize, 30000), null);
            cloudDataSource = getSpringCloud().getServiceConnector(serviceName, DataSource.class, config);
        }
    } catch (CloudException e) {
        // Do nothing
    }
    return cloudDataSource;
}
 
Example 13
private String getStoragePath(String serviceName) {
    if (StringUtils.isEmpty(serviceName)) {
        LOGGER.warn(Messages.FILE_SYSTEM_SERVICE_NAME_IS_NOT_SPECIFIED);
        return null;
    }
    try {
        CloudFactory cloudFactory = new CloudFactory();
        Cloud cloud = cloudFactory.getCloud();
        FileSystemServiceInfo serviceInfo = (FileSystemServiceInfo) cloud.getServiceInfo(serviceName);
        return serviceInfo.getStoragePath();
    } catch (CloudException e) {
        LOGGER.warn(MessageFormat.format(Messages.FAILED_TO_DETECT_FILE_SERVICE_STORAGE_PATH, serviceName), e);
    }
    return null;
}
 
Example 14
@Test
public void testFallBackToDefault() {
    when(configurationMock.getDbConnectionThreads()).thenReturn(30);
    when(springCloudMock.getServiceConnector(any(), any(), any())).thenThrow(new CloudException("unknown service"));

    testedFactory.setDefaultDataSource(defaultDataSource);
    testedFactory.setServiceName("any");
    testedFactory.setConfiguration(configurationMock);
    testedFactory.afterPropertiesSet();

    verify(springCloudMock, atLeastOnce()).getServiceConnector(any(), any(), any());
    assertEquals(testedFactory.getObject(), defaultDataSource);
}
 
Example 15
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException e) {
        return null;
    }
}
 
Example 16
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException e) {
        return null;
    }
}
 
Example 17
private Cloud getCloud() {
  try {
    CloudFactory cloudFactory = new CloudFactory();
    return cloudFactory.getCloud();
  } catch (CloudException ce) {
    return null;
  }
}
 
Example 18
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    if (cloud != null) return;
    try {
        cloud = new CloudFactory().getCloud();
    } catch (CloudException e) {
        return; // not running on a known cloud environment, so nothing to do
    }

    for (ServiceInfo serviceInfo : cloud.getServiceInfos()) {
        if (serviceInfo instanceof SsoServiceInfo) {
            Map<String, Object> map = new HashMap<>();
            SsoServiceInfo ssoServiceInfo = (SsoServiceInfo) serviceInfo;
            map.put("security.oauth2.client.clientId", ssoServiceInfo.getClientId());
            map.put("security.oauth2.client.clientSecret", ssoServiceInfo.getClientSecret());
            map.put("security.oauth2.client.accessTokenUri", ssoServiceInfo.getAuthDomain() + "/oauth/token");
            map.put("security.oauth2.client.userAuthorizationUri", ssoServiceInfo.getAuthDomain() + "/oauth/authorize");
            map.put("ssoServiceUrl", ssoServiceInfo.getAuthDomain());
            map.put("security.oauth2.resource.userInfoUri", ssoServiceInfo.getAuthDomain() + "/userinfo");
            map.put("security.oauth2.resource.tokenInfoUri", ssoServiceInfo.getAuthDomain() + "/check_token");
            map.put("security.oauth2.resource.jwk.key-set-uri", ssoServiceInfo.getAuthDomain() + "/token_keys");
            MapPropertySource mapPropertySource = new MapPropertySource("vcapPivotalSso", map);

            event.getEnvironment().getPropertySources().addFirst(mapPropertySource);
        }
    }
}
 
Example 19
protected Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException ce) {
        return null;
    }
}
 
Example 20
private Cloud getCloud() {
    try {
        CloudFactory cloudFactory = new CloudFactory();
        return cloudFactory.getCloud();
    } catch (CloudException ce) {
        return null;
    }
}
 
Example 21
public String getHost() {
	try {
		return InetAddress.getLocalHost().getHostAddress();
	} catch (UnknownHostException ex) {
		throw new CloudException(ex);
	}
}
 
Example 22
@Override
public ApplicationInstanceInfo getApplicationInstanceInfo() {
	try {
		@SuppressWarnings("unchecked")
		Map<String, Object> rawApplicationInstanceInfo 
			= objectMapper.readValue(environment.getEnvValue("VCAP_APPLICATION"), Map.class);
		return applicationInstanceInfoCreator.createApplicationInstanceInfo(rawApplicationInstanceInfo);
	} catch (Exception e) {
		throw new CloudException(e);
	} 
}
 
Example 23
Source Project: spring-cloud-connectors   Source File: HerokuConnector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ApplicationInstanceInfo getApplicationInstanceInfo() {
	try {
		return applicationInstanceInfoCreator.createApplicationInstanceInfo();
	} catch (Exception e) {
		throw new CloudException(e);
	}
}
 
Example 24
public HystrixAmqpServiceInfo(String id, String uri) throws CloudException {
	super(id);
	delegate = new AmqpServiceInfo(id, uri);
}
 
Example 25
Source Project: spring-cloud-connectors   Source File: AmqpServiceInfo.java    License: Apache License 2.0 4 votes vote down vote up
public AmqpServiceInfo(String id, String uri) throws CloudException {
	this(id, uri, null);
}
 
Example 26
Source Project: spring-cloud-connectors   Source File: AmqpServiceInfo.java    License: Apache License 2.0 4 votes vote down vote up
public AmqpServiceInfo(String id, String uri, String managementUri) throws CloudException {
	super(id, uri);
	this.managementUri = managementUri;
	this.virtualHost = decode(getUriInfo().getPath());
}
 
Example 27
@Test(expected = CloudException.class)
public void servicesInfosWithNonJsonServices() {
	when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn("some value");
	testCloudConnector.getServiceInfos();
}