Skip to content

Commit

Permalink
creating a fake data source in order to start the application on clou…
Browse files Browse the repository at this point in the history
…d foundry.
  • Loading branch information
cbellone committed Nov 7, 2016
1 parent 6307d17 commit 5736374
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
46 changes: 34 additions & 12 deletions src/main/java/alfio/config/DataSourceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/alfio/config/support/PlatformProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5736374

Please sign in to comment.