From 573637432540aa53350e27333865d026f740228c Mon Sep 17 00:00:00 2001 From: Celestino Bellone Date: Mon, 7 Nov 2016 23:04:44 +0100 Subject: [PATCH] creating a fake data source in order to start the application on cloud foundry. --- .../alfio/config/DataSourceConfiguration.java | 46 ++++++++++++++----- .../config/support/PlatformProvider.java | 2 +- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main/java/alfio/config/DataSourceConfiguration.java b/src/main/java/alfio/config/DataSourceConfiguration.java index 5dd85a835a..9c8b4913b9 100644 --- a/src/main/java/alfio/config/DataSourceConfiguration.java +++ b/src/main/java/alfio/config/DataSourceConfiguration.java @@ -49,6 +49,7 @@ import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.datasource.AbstractDataSource; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -64,6 +65,8 @@ import javax.sql.DataSource; import java.net.URISyntaxException; +import java.sql.Connection; +import java.sql.SQLException; import java.text.ParseException; import java.util.EnumSet; import java.util.Properties; @@ -92,19 +95,23 @@ public PlatformProvider getCloudProvider(Environment environment) { return current; } - @Bean(destroyMethod = "close") + @Bean public DataSource getDataSource(Environment env, PlatformProvider platform) throws URISyntaxException { - HikariDataSource dataSource = new HikariDataSource(); - dataSource.setJdbcUrl(platform.getUrl(env)); - dataSource.setUsername(platform.getUsername(env)); - dataSource.setPassword(platform.getPassword(env)); - dataSource.setDriverClassName(platform.getDriveClassName(env)); - int maxActive = platform.getMaxActive(env); - - dataSource.setMaximumPoolSize(maxActive); - - log.debug("Connection pool properties: max active {}, initial size {}", maxActive, dataSource.getMinimumIdle()); - return dataSource; + if(platform == PlatformProvider.CLOUD_FOUNDRY) { + return new FakeCFDataSource(); + } else { + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setJdbcUrl(platform.getUrl(env)); + dataSource.setUsername(platform.getUsername(env)); + dataSource.setPassword(platform.getPassword(env)); + dataSource.setDriverClassName(platform.getDriveClassName(env)); + int maxActive = platform.getMaxActive(env); + + dataSource.setMaximumPoolSize(maxActive); + + log.debug("Connection pool properties: max active {}, initial size {}", maxActive, dataSource.getMinimumIdle()); + return dataSource; + } } @Bean @@ -300,4 +307,19 @@ public SchedulerFactoryBean schedulerFactory(Environment env, PlatformProvider p public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } + + /** + * Fake DataSource used on Cloud Foundry. Oh yeah. + */ + private static class FakeCFDataSource extends AbstractDataSource { + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return null; + } + } } diff --git a/src/main/java/alfio/config/support/PlatformProvider.java b/src/main/java/alfio/config/support/PlatformProvider.java index ac0d5bd8cb..69f2edffe3 100644 --- a/src/main/java/alfio/config/support/PlatformProvider.java +++ b/src/main/java/alfio/config/support/PlatformProvider.java @@ -134,7 +134,7 @@ public boolean isHosting(Environment env) { CLOUD_FOUNDRY { @Override - public String getUrl(Environment env) { return ""; } + public String getUrl(Environment env) { return "url"; } @Override public String getUsername(Environment env) {