Skip to content

Commit

Permalink
Merge 496978a into d0f788d
Browse files Browse the repository at this point in the history
  • Loading branch information
speedlog committed Jun 28, 2018
2 parents d0f788d + 496978a commit 9755ef3
Show file tree
Hide file tree
Showing 28 changed files with 999 additions and 219 deletions.
9 changes: 9 additions & 0 deletions cascades-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example usage

* Run PostgreSQL server in docker
`docker run --name cascades-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:10.4`

* Build project with profile `-Ppostgresql`
It adds PostgreSQL driver.

* Run application with profile `mvn spring-boot:run --Dspring.profiles.activeproduction,hibernate,postgresqldocker`
13 changes: 13 additions & 0 deletions cascades-server/app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@
</plugins>
</build>

<profiles>
<profile>
<id>postgresql</id>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# run PostgreSQL in docker: docker run --name cascades-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:10.4
cascades.managedServers[0].serverId=testdb
cascades.managedServers[0].type=pgsql
cascades.managedServers[0].dbname=postgres
cascades.managedServers[0].user=postgres
cascades.managedServers[0].password=mysecretpassword
cascades.managedServers[0].host=localhost
cascades.managedServers[0].port=5432
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
@AllArgsConstructor
@RequiredArgsConstructor
class ConnectionConfiguration {
private String driver;
private String url;
private String driverClass;
private String jdbcUrlTemplate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
*/
public class ConnectionConfigurator {

ConnectionConfiguration getConnectionConfiguration(ServerDef serverDef) {
String driver;
String url;
switch (serverDef.getType()) {
ConnectionConfiguration getConnectionConfiguration(String databaseType) {
String driverClass;
String jdbdUrlTemplate;
switch (databaseType) {
case "ora12c":
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@//%s:%d/%s";
driverClass = "oracle.jdbc.driverClass.OracleDriver";
jdbdUrlTemplate = "jdbc:oracle:thin:@//%s:%d/%s";
break;
case "pgsql":
driver = "org.postgresql.Driver";
url = "jdbc:postgresql://%s:%d/%s";
driverClass = "org.postgresql.Driver";
jdbdUrlTemplate = "jdbc:postgresql://%s:%d/%s";
break;
default:
throw new EidIllegalArgumentException(
"20170728:150904",
"Given driver hasn't been recognised."
String.format("Given database type '%s' hasn't been recognised.", databaseType)
);
}
return new ConnectionConfiguration(
driver,
url
driverClass,
jdbdUrlTemplate
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import pl.gov.coi.cascades.server.domain.DatabaseTemplateGateway;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -18,29 +17,30 @@
public class DatabaseEndpointConfiguration {

@Bean
Map<String, DriverManagerDataSource> produceDriverManagerDataSource(ServerConfigurationService service,
ConnectionConfigurator connectionConfigurator) {
Map<String, DriverManagerDataSource> managers = new HashMap<>();
for (ServerDef serverDef : service.getManagedServers()) {
ConnectionConfiguration configuration = connectionConfigurator.getConnectionConfiguration(serverDef);
DriverManagerDataSource manager = new DriverManagerDataSource();
manager.setDriverClassName(configuration.getDriver());
manager.setUrl(String.format(
configuration.getUrl(),
serverDef.getHost(),
serverDef.getPort(),
serverDef.getDbname())
);
manager.setPassword(serverDef.getPassword());
manager.setUsername(serverDef.getUser());
managers.put(serverDef.getServerId(), manager);
}
return managers;
Map<String, DriverManagerDataSource> produceDriverManagerDataSource(DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
return driverManagerDataSourceHelper.getManagersMap();
}

@Bean
DatabaseManager produceDatabaseManager(Map<String, DriverManagerDataSource> driver) {
return new DatabaseEndpointManager(driver);
DriverManagerDataSourceProvider produceDriverManagerDataSourceProvider() {
return new DriverManagerDataSourceProviderImpl();
}

@Bean
DriverManagerDataSourceHelper produceDriverManagerDataSourceHelper(ConnectionConfigurator connectionConfigurator,
ServerConfigurationService serverConfigurationService,
DriverManagerDataSourceProvider driverManagerDataSourceProvider) {
return new DriverManagerDataSourceHelper(
connectionConfigurator,
serverConfigurationService,
driverManagerDataSourceProvider
);
}

@Bean
DatabaseManager produceDatabaseManager(Map<String, DriverManagerDataSource> driverManagerMap,
DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
return new DatabaseEndpointManager(driverManagerMap, driverManagerDataSourceHelper);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException;

import javax.inject.Inject;
import java.sql.SQLException;
import java.util.Map;

/**
Expand All @@ -15,14 +14,17 @@
public class DatabaseEndpointManager implements DatabaseManager {

private final Map<String, DriverManagerDataSource> managers;
private final DriverManagerDataSourceHelper driverManagerDataSourceHelper;

@Inject
DatabaseEndpointManager(Map<String, DriverManagerDataSource> managers) {
DatabaseEndpointManager(Map<String, DriverManagerDataSource> managers,
DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
this.managers = managers;
this.driverManagerDataSourceHelper = driverManagerDataSourceHelper;
}

@Override
public ConnectionDatabase get(String serverId) throws SQLException {
public ConnectionDatabase getConnectionToServer(String serverId) {
DriverManagerDataSource manager = managers.get(serverId);

if (manager == null) {
Expand All @@ -38,4 +40,13 @@ public ConnectionDatabase get(String serverId) throws SQLException {
);
}

@Override
public ConnectionDatabase getConnectionToTemplate(String serverId, String templateName) {
DriverManagerDataSource manager = driverManagerDataSourceHelper.getManager(serverId, templateName);
return new ConnectionDatabase(
new JdbcTemplate(manager),
manager.getUrl()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
*/
public interface DatabaseManager {

ConnectionDatabase get(String serverId) throws SQLException;
/**
* Returns connection to database with sepcified serverId.
* @param serverId server ID
* @return connection to database
*/
ConnectionDatabase getConnectionToServer(String serverId) throws SQLException;

/**
* Returns connection to template in database with sepcified serverId.
* @param serverId server ID
* @param templateName template name
* @return connection to database
*/
ConnectionDatabase getConnectionToTemplate(String serverId, String templateName) throws SQLException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package pl.gov.coi.cascades.server;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import pl.wavesoftware.eid.exceptions.EidIllegalStateException;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* @author <a href="mailto:mariusz.wyszomierski@coi.gov.pl">Mariusz Wyszomierski</a>
*/
public class DriverManagerDataSourceHelper {

private final ConnectionConfigurator connectionConfigurator;
private final ServerConfigurationService serverConfigurationService;
private final DriverManagerDataSourceProvider driverManagerDataSourceProvider;

@Inject
public DriverManagerDataSourceHelper(ConnectionConfigurator connectionConfigurator,
ServerConfigurationService serverConfigurationService,
DriverManagerDataSourceProvider driverManagerDataSourceProvider) {
this.connectionConfigurator = connectionConfigurator;
this.serverConfigurationService = serverConfigurationService;
this.driverManagerDataSourceProvider = driverManagerDataSourceProvider;
}

/**
* Returns connection to database with given database name on server with given serverId
* defined in application configuration {@link ServerConfigurationService}.
*
* @param serverId server ID
* @param databaseName database name
* @return {@link DriverManagerDataSource}
*/
DriverManagerDataSource getManager(String serverId, String databaseName) {
ServerDef serverConfiguration = getServerDef(serverId);
ConnectionConfiguration connectionConfiguration = connectionConfigurator.getConnectionConfiguration(serverConfiguration.getType());
ServerDef newServerConfiguration = serverConfiguration.getWithNewDatabaseName(databaseName);
return createDriverManagerDataSource(connectionConfiguration, newServerConfiguration);
}

/**
* Returns map of connections to servers defined in application configuration {@link ServerConfigurationService}.
* @return map of connections, serverId is the key
*/
Map<String, DriverManagerDataSource> getManagersMap() {
Map<String, DriverManagerDataSource> managers = new HashMap<>();
for (ServerDef serverDef : serverConfigurationService.getManagedServers()) {
ConnectionConfiguration configuration = connectionConfigurator.getConnectionConfiguration(serverDef.getType());
DriverManagerDataSource manager = createDriverManagerDataSource(configuration, serverDef);
managers.put(serverDef.getServerId(), manager);
}
return managers;
}

private DriverManagerDataSource createDriverManagerDataSource(ConnectionConfiguration connectionConfiguration,
ServerDef serverConfiguration) {
DriverManagerDataSource manager = driverManagerDataSourceProvider.produce();
manager.setDriverClassName(connectionConfiguration.getDriverClass());
manager.setUrl(String.format(
connectionConfiguration.getJdbcUrlTemplate(),
serverConfiguration.getHost(),
serverConfiguration.getPort(),
serverConfiguration.getDbname())
);
manager.setPassword(serverConfiguration.getPassword());
manager.setUsername(serverConfiguration.getUser());
return manager;
}

private ServerDef getServerDef(String serverId) {
Optional<ServerDef> matchingServerConfiguration = serverConfigurationService.getManagedServers().stream()
.filter(server -> serverId.equals(server.getServerId()))
.findFirst();
if (!matchingServerConfiguration.isPresent()) {
String errorMessage = String.format("There is no configuration for serverId: %s", serverId);
throw new EidIllegalStateException("20180625:205951", errorMessage);
}
return matchingServerConfiguration.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.gov.coi.cascades.server;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
* @author <a href="mailto:mariusz.wyszomierski@coi.gov.pl">Mariusz Wyszomierski</a>
*/
public interface DriverManagerDataSourceProvider {

/**
* Provides instance of {@link DriverManagerDataSource}.
* @return {@link DriverManagerDataSource}
*/
DriverManagerDataSource produce();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.gov.coi.cascades.server;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
* @author <a href="mailto:mariusz.wyszomierski@coi.gov.pl">Mariusz Wyszomierski</a>
*/
public class DriverManagerDataSourceProviderImpl implements DriverManagerDataSourceProvider {

@Override
public DriverManagerDataSource produce() {
return new DriverManagerDataSource();
}
}

0 comments on commit 9755ef3

Please sign in to comment.