-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
999 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
cascades-server/app/src/main/resources/application-production.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...nfiguration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
14 changes: 14 additions & 0 deletions
14
...uration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.