Java Code Examples for org.springframework.cloud.service.ServiceConnectorConfig

The following examples show how to use org.springframework.cloud.service.ServiceConnectorConfig. 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
@Test
public void testCreate() {
    final CloudantServiceInfo badUrlServiceInfo = new CloudantServiceInfo(
            "id",
            "username",
            "password",
            "hostname",
            443,
            "url"
    );
    assertNull(creator.create(badUrlServiceInfo, new ServiceConnectorConfig() {
    }));

    final CloudantServiceInfo serviceInfo = new CloudantServiceInfo(
            "testId",
            "username",
            "password",
            "username.cloudant.com",
            443,
            "https://username:[email protected]"
    );
    assertTrue(creator.create(serviceInfo, new ServiceConnectorConfig() {
    }) instanceof CouchDbInstance);
}
 
Example 2
Source Project: spring-cloud-connectors   Source File: DataSourceCreator.java    License: Apache License 2.0 6 votes vote down vote up
private Collection<PooledDataSourceCreator<SI>> filterPooledDataSourceCreators(ServiceConnectorConfig serviceConnectorConfig) {
	if (serviceConnectorConfig != null) {
		List<String> pooledDataSourceNames = ((DataSourceConfig) serviceConnectorConfig).getPooledDataSourceNames();
		if (pooledDataSourceNames != null) {
			List<PooledDataSourceCreator<SI>> filtered = new ArrayList<>();

			for (String name : pooledDataSourceNames) {
				for (String key : pooledDataSourceCreators.keySet()) {
					if (key.contains(name)) {
						filtered.add(pooledDataSourceCreators.get(key));
					}
				}
			}

			return filtered;
		}
	}
	return pooledDataSourceCreators.values();
}
 
Example 3
private CachingConnectionFactory createSpringConnectionFactory(AmqpServiceInfo serviceInfo,
															   ServiceConnectorConfig serviceConnectorConfiguration,
															   com.rabbitmq.client.ConnectionFactory connectionFactory) {
	CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);

	if (serviceInfo.getUris() != null) {
		cachingConnectionFactory.setAddresses(getAddresses(serviceInfo));
	}

	if (serviceConnectorConfiguration != null) {
		Integer channelCacheSize = ((RabbitConnectionFactoryConfig) serviceConnectorConfiguration).getChannelCacheSize();
		if (channelCacheSize != null) {
			cachingConnectionFactory.setChannelCacheSize(channelCacheSize);
		}
	}

	return cachingConnectionFactory;
}
 
Example 4
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	try {
		Constructor<?> ctor = serviceConnectorFactoryType.getConstructor(String.class, ServiceConnectorConfig.class);
		AbstractCloudServiceConnectorFactory<?> serviceFactory = (AbstractCloudServiceConnectorFactory<?>)ctor.newInstance(serviceId, serviceConnectorConfig);
		serviceFactory.setServiceConnectorType((Class)serviceConnectorType);
		serviceFactory.setBeanFactory(beanFactory);
		serviceFactory.afterPropertiesSet();
		// id is the beanId if specified, otherwise the serviceId
		if (StringUtils.hasText(beanId)) {
			beanFactory.registerSingleton(beanId, serviceFactory);
		} else {
			beanFactory.registerSingleton(serviceFactory.getServiceId(), serviceFactory);
		}
	} catch (Exception ex) {
		throw new BeanCreationException("Error registering service factory", ex);
	}
}
 
Example 5
@Bean
public MessagingServiceFactory getMessagingServiceFactory() {
    ServiceConnectorConfig config = null; // currently there are no configurations for the MessagingService supported
    Cloud cloud = new CloudFactory().getCloud();
    // get the MessagingService via the service connector
    MessagingService messagingService = cloud.getSingletonServiceConnector(MessagingService.class, config);
    if (messagingService == null) {
        throw new IllegalStateException("Unable to create the MessagingService.");
    }
    return MessagingServiceFactoryCreator.createFactory(messagingService);
}
 
Example 6
@Bean
public MessagingServiceFactory getMessagingServiceFactory() {
    ServiceConnectorConfig config = null; // currently there are no configurations for the MessagingService supported
    Cloud cloud = new CloudFactory().getCloud();
    // get the MessagingService via the service connector
    MessagingService messagingService = cloud.getSingletonServiceConnector(MessagingService.class, config);
    if (messagingService == null) {
        throw new IllegalStateException("Unable to create the MessagingService.");
    }
    return MessagingServiceFactoryCreator.createFactory(messagingService);
}
 
Example 7
@Override
public KafkaRepository create(KafkaServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig) {
    log.info("creating kafka repo wth service info: " + serviceInfo);
    try {
        return new KafkaRepositoryFactory().create(serviceInfo);
    } catch (InterruptedException e) {
        log.error(e.getLocalizedMessage(), e);
        return null;
    }
}
 
Example 8
@Override
public CouchDbInstance create(CloudantServiceInfo serviceInfo,
        ServiceConnectorConfig serviceConnectorConfig) {
    HttpClient httpClient;
    try {
        httpClient = new StdHttpClient.Builder()
                .url(serviceInfo.getUrl())
                .build();
        return new StdCouchDbInstance(httpClient);
    } catch (MalformedURLException e) {
        LOG.logp(Level.WARNING, CloudantInstanceCreator.class.getName(), "create", "Error parsing URL", e);
        return null;
    }
}
 
Example 9
Source Project: spring-cloud-connectors   Source File: Cloud.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source Project: spring-cloud-connectors   Source File: DataSourceCreator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DataSource create(SI serviceInfo, ServiceConnectorConfig serviceConnectorConfig) {
	try {
		DataSource ds = createPooledDataSource(serviceInfo, serviceConnectorConfig);
		if (ds != null) {
			return ds;
		}
		// Only for testing outside Tomcat/CloudFoundry
		logger.warning("No connection pooling DataSource implementation found on the classpath - no pooling is in effect.");
		return new UrlDecodingDataSource(serviceInfo.getJdbcUrl());
	} catch (Exception e) {
		throw new ServiceConnectorCreationException(
				"Failed to created cloud datasource for " + serviceInfo.getId() + " service", e);
	}
}
 
Example 11
Source Project: spring-cloud-connectors   Source File: DataSourceCreator.java    License: Apache License 2.0 5 votes vote down vote up
private DataSource createPooledDataSource(SI serviceInfo, ServiceConnectorConfig serviceConnectorConfig) {
	Collection<PooledDataSourceCreator<SI>> delegates = filterPooledDataSourceCreators(serviceConnectorConfig);

	for (PooledDataSourceCreator<SI> delegate : delegates) {
		DataSource ds = delegate.create(serviceInfo, serviceConnectorConfig, getDriverClassName(serviceInfo), validationQuery);
		if (ds != null) {
			return ds;
		}
	}

	return null;
}
 
Example 12
@Override
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
                            String driverClassName, String validationQuery) {
	if (hasClass(TOMCAT_JDBC_DATASOURCE)) {
		logger.info("Found Tomcat JDBC connection pool on the classpath. Using it for DataSource connection pooling.");
		org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
		setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
		return new UrlDecodingDataSource(ds, "url");
	} else {
		return null;
	}
}
 
Example 13
@Override
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
                            String driverClassName, String validationQuery) {
	if (hasClass(TOMCAT_7_DBCP)) {
		logger.info("Found Tomcat 7 DBCP connection pool on the classpath. Using it for DataSource connection pooling.");
		return createDataSource(TOMCAT_7_DBCP, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
	} else if (hasClass(TOMCAT_8_DBCP)) {
		logger.info("Found Tomcat 8 DBCP connection pool on the classpath. Using it for DataSource connection pooling.");
		return createDataSource(TOMCAT_8_DBCP, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
	} else {
		return null;
	}
}
 
Example 14
private DataSource createDataSource(String className, RelationalServiceInfo serviceInfo,
									ServiceConnectorConfig serviceConnectorConfig,
									String driverClassName, String validationQuery) {
	try {
		DataSource dataSource = (DataSource) Class.forName(className).newInstance();
		setBasicDataSourceProperties(dataSource, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
		return new UrlDecodingDataSource(dataSource, "url");
	} catch (Throwable e) {
		throw new ServiceConnectorCreationException("Error instantiating Tomcat DBCP connection pool", e);
	}
}
 
Example 15
protected void setBasicDataSourceProperties(DataSource basicDataSource, RelationalServiceInfo serviceInfo,
										   ServiceConnectorConfig serviceConnectorConfig,
 											   String driverClassName, String validationQuery) {
	BeanWrapper target = new BeanWrapperImpl(basicDataSource);
	target.setPropertyValue("driverClassName", driverClassName);
	target.setPropertyValue("url", serviceInfo.getJdbcUrl());
	if (validationQuery != null) {
		target.setPropertyValue("validationQuery", validationQuery);
		target.setPropertyValue("testOnBorrow", true);
	}

	configurer.configure(basicDataSource, (DataSourceConfig)serviceConnectorConfig);
}
 
Example 16
@Override
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
						 String driverClassName, String validationQuery) {
	if (hasClass(DBCP2_BASIC_DATASOURCE)) {
		logger.info("Found DBCP2 on the classpath. Using it for DataSource connection pooling.");
		org.apache.commons.dbcp2.BasicDataSource ds = new org.apache.commons.dbcp2.BasicDataSource();
		setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
		return new UrlDecodingDataSource(ds, "url");
	}
	return null;
}
 
Example 17
protected void setBasicDataSourceProperties(DataSource basicDataSource, RelationalServiceInfo serviceInfo,
											ServiceConnectorConfig serviceConnectorConfig,
											String driverClassName, String validationQuery) {
	BeanWrapper target = new BeanWrapperImpl(basicDataSource);
	target.setPropertyValue("driverClassName", driverClassName);
	target.setPropertyValue("jdbcUrl", serviceInfo.getJdbcUrl());
	if (validationQuery != null) {
		target.setPropertyValue("connectionTestQuery", validationQuery);
	}

	configurer.configure(basicDataSource, (DataSourceConfig)serviceConnectorConfig);
}
 
Example 18
@Override
public DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
						 String driverClassName, String validationQuery) {
	if (hasClass(HIKARI_DATASOURCE)) {
		logger.info("Found HikariCP on the classpath. Using it for DataSource connection pooling.");
		HikariDataSource ds = new HikariDataSource();
		setBasicDataSourceProperties(ds, serviceInfo, serviceConnectorConfig, driverClassName, validationQuery);
		return new UrlDecodingDataSource(ds, "jdbcUrl");
	} else {
		return null;
	}
}
 
Example 19
Source Project: spring-cloud-connectors   Source File: MailSenderCreator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JavaMailSender create(SmtpServiceInfo serviceInfo, ServiceConnectorConfig config) {
	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

	mailSender.setHost(serviceInfo.getHost());
	mailSender.setPort(serviceInfo.getPort());
	mailSender.setUsername(serviceInfo.getUserName());
	mailSender.setPassword(serviceInfo.getPassword());

	return mailSender;
}
 
Example 20
public CloudServiceIntroducer(Class<? extends CloudServiceConnectorFactory<?>> serviceConnectorFactoryType, 
							  String beanId, String serviceId, ServiceConnectorConfig serviceConnectorConfig) {
	this.serviceConnectorFactoryType = serviceConnectorFactoryType;
	this.beanId = beanId;
	this.serviceId = serviceId;
	this.serviceConnectorConfig = serviceConnectorConfig;
}
 
Example 21
@Override
public GraphDatabaseService create(GraphDatabaseServiceInfo neo4JServiceInfo,
		ServiceConnectorConfig serviceConnectorConfig) {
	return new SpringRestGraphDatabase(neo4JServiceInfo.getNeo4jUri(), neo4JServiceInfo.getNeo4jUsername(),
			neo4JServiceInfo.getNeo4jPassword());
}
 
Example 22
@Override
public TwilioRestClient create(TwilioServiceInfo serviceInfo,
        ServiceConnectorConfig serviceConnectorConfig) {
    return new TwilioRestClient(serviceInfo.getAccountId(), serviceInfo.getAuthToken());
}
 
Example 23
Source Project: spring-cloud-connectors   Source File: Cloud.java    License: Apache License 2.0 4 votes vote down vote up
private <SC> SC getServiceConnector(ServiceInfo serviceInfo, Class<SC> serviceConnectorType,
	ServiceConnectorConfig serviceConnectorConfig) {
	ServiceConnectorCreator<SC, ServiceInfo> serviceConnectorCreator = serviceConnectorCreatorRegistry.getServiceCreator(
		serviceConnectorType, serviceInfo);
	return serviceConnectorCreator.create(serviceInfo, serviceConnectorConfig);
}
 
Example 24
@Override
public StubServiceConnector create(StubServiceInfo serviceInfo, ServiceConnectorConfig config) {
	return new StubServiceConnector(serviceInfo.getId(), serviceInfo.getHost(), serviceInfo.getPort(), serviceInfo.getUserName(), serviceInfo.getPassword(),
			config == null ? null : ((StubServiceConnectorConfig)config).config);
}
 
Example 25
public abstract DataSource create(RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig,
String driverClassName, String validationQuery);
 
Example 26
public CloudDataSourceFactory(String serviceId, ServiceConnectorConfig serviceConnectorConfiguration) {
	super(serviceId, DataSource.class, serviceConnectorConfiguration);
}
 
Example 27
public CassandraClusterFactory(String serviceId,
		ServiceConnectorConfig serviceConnectorConfiguration) {
	super(serviceId, Cluster.class, serviceConnectorConfiguration);
}
 
Example 28
@Override
public Cluster create(CassandraServiceInfo serviceInfo,
		ServiceConnectorConfig serviceConnectorConfig) {

	Builder builder = Cluster.builder()
			.addContactPoints(serviceInfo.getContactPoints().toArray(new String[0]))
			.withPort(serviceInfo.getPort());

	if (StringUtils.hasText(serviceInfo.getUsername())) {
		builder.withCredentials(serviceInfo.getUsername(), serviceInfo.getPassword());
	}

	if (serviceConnectorConfig instanceof CassandraClusterConfig) {

		CassandraClusterConfig config = (CassandraClusterConfig) serviceConnectorConfig;

		if (config.getCompression() != null) {
			builder.withCompression(config.getCompression());
		}

		builder.withPoolingOptions(config.getPoolingOptions());
		builder.withSocketOptions(config.getSocketOptions());
		builder.withQueryOptions(config.getQueryOptions());
		builder.withNettyOptions(config.getNettyOptions());
		builder.withLoadBalancingPolicy(config.getLoadBalancingPolicy());
		builder.withReconnectionPolicy(config.getReconnectionPolicy());
		builder.withRetryPolicy(config.getRetryPolicy());

		if (config.getProtocolVersion() != null) {
			builder.withProtocolVersion(config.getProtocolVersion());
		}

		if (!config.isMetricsEnabled()) {
			builder.withoutMetrics();
		}

		if (!config.isJmxReportingEnabled()) {
			builder.withoutJMXReporting();
		}
	}

	return builder.build();
}
 
Example 29
Source Project: spring-cloud-connectors   Source File: MailSenderFactory.java    License: Apache License 2.0 4 votes vote down vote up
public MailSenderFactory(String serviceId, ServiceConnectorConfig serviceConnectorConfiguration) {
	super(serviceId, JavaMailSender.class, serviceConnectorConfiguration);
}
 
Example 30
public RedisConnectionFactoryFactory(String serviceId, ServiceConnectorConfig serviceConnectorConfiguration) {
	super(serviceId, RedisConnectionFactory.class, serviceConnectorConfiguration);
}