Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/create instance database on postgres #43

Open
wants to merge 49 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0219ffd
bugfix/fix-run-on-production-mode Adding the missing implementation a…
Jun 25, 2018
dee19a6
bugfix/fix-run-on-production-mode add author annotation
Jun 25, 2018
de5a44f
bugfix/fix-run-on-production-mode addition of corrections reported in…
Jun 26, 2018
f1e78f3
bugfix/fix-run-on-production-mode corrections imports
Jun 26, 2018
d168e3e
bugfix/fix-run-on-production-mode improvement of object initializatio…
Jun 26, 2018
8ae2c63
bugfix/fix-run-on-production-mode add mock for tests
Jun 27, 2018
689dec8
feature/create-database-instance-save-in-h2-developer-mode add create…
Jun 28, 2018
496978a
Add feature to create template in PostgreSQL
speedlog Jun 28, 2018
1104f34
feature/create-database-instance-save-in-h2-developer-mode add tests …
Jul 2, 2018
8678018
feature/create-database-instance-save-in-h2-developer-mode remove unn…
Jul 2, 2018
0afa380
feature/create-database-instance-save-in-h2-developer-mode change the…
Jul 2, 2018
0cd57fe
feature/create-database-instance-save-in-h2-developer-mode add tests …
Jul 2, 2018
ec806f9
feature/create-database-instance-save-in-h2-developer-mode remove ;
Jul 2, 2018
a7a6c4e
feature/create-database-instance-save-in-h2-developer-mode remove emp…
Jul 2, 2018
d4a7eef
feature/create-database-instance-save-in-h2-developer-mode remove set…
Jul 2, 2018
8fc323f
feature/create-database-instance-save-in-h2-developer-mode refactor
Jul 3, 2018
f14c053
feature/save-user-in-database-and-assigning-user-rights add impl, stu…
Jul 4, 2018
9aea764
feature/save-user-in-database-and-assigning-user-rights adding method…
Jul 4, 2018
5e347a3
Merge branch 'develop' into feature/create-database-instance-save-in-…
Jul 5, 2018
2f3afc5
feature/create-database-instance-save-in-h2-developer-mode refactor t…
Jul 5, 2018
7aa9a33
feature/create-database-instance-save-in-h2-developer-mode refactor test
Jul 5, 2018
f239d2a
feature/create-database-instance-save-in-h2-developer-mode remove imp…
Jul 5, 2018
5a7e5e8
feature/save-user-in-database-and-assigning-user-rights add tests for…
Jul 5, 2018
7c829a4
feature/save-user-in-database-and-assigning-user-rights refactor afte…
Jul 5, 2018
82fa984
bugfix/remove-in-flow/tests-database-type remove DatabaseType and for…
Jul 6, 2018
11ed462
Merge branch 'feature/create-database-instance-save-in-h2-developer-m…
Jul 6, 2018
fc95c29
feature/save-user-in-database-and-assigning-user-rights adding a lost…
Jul 6, 2018
676af58
Merge branch 'bugfix/remove-in-flow-tests-database-type' into feature…
Jul 6, 2018
f387d74
feature/extracting-from-server-properties-type-database add DatabseIm…
Jul 6, 2018
ef1ba58
feature/extracting-from-server-properties-type-database refactor afte…
Jul 6, 2018
18daddf
feature/create-database-instance-save-in-h2-developer-mode refactor D…
Jul 6, 2018
21b299b
Merge branch 'feature/create-database-instance-save-in-h2-developer-m…
Jul 6, 2018
ccf78da
feature/extracting-from-server-properties-type-database refactor test…
Jul 6, 2018
9815bae
feature/extracting-from-server-properties-type-database refactor
Jul 6, 2018
87700b2
feature/extracting-from-server-properties-type-database remove Databa…
Jul 9, 2018
3aee77d
bugfix/removing-a-repeating-entry-from-the-secondary-database add id …
Jul 9, 2018
0e7ea27
feature/extracting-from-server-properties-type-database add tests
Jul 9, 2018
c225dd4
Merge branch 'feature/extracting-from-server-properties-type-database…
Jul 9, 2018
cffa572
bugfix/removing-a-repeating-entry-from-the-secondary-database refator…
Jul 9, 2018
776d484
bugfix/change-in-username-generation-for-a-database-user add letter i…
Jul 9, 2018
e75f7b4
Merge branch 'bugfix/change-in-username-generation-for-a-database-use…
Jul 9, 2018
65a9bc4
bugfix/create-template-on-oracle-12c improving sql queries and adding…
Jul 10, 2018
c2dd6dd
bugfix/create-template-on-oracle-12c add lib ojdbc7.jar
Jul 10, 2018
c4affde
feature/create-instance-database-on-oracle-12c-and-user add impl for …
Jul 10, 2018
16f7d6d
feature/create-instance-database-on-oracle-12c-and-user add user for …
Jul 11, 2018
c7ce5b3
feature/create-instance-database-on-oracle-12c-and-user add tests
Jul 12, 2018
8b2c442
feature/create-instance-database-on-oracle-12c-and-user add IT test
Jul 12, 2018
620d697
feature/create-instance-database-on-oracle-12c-and-user add test for …
Jul 12, 2018
159f91c
feature/create-instance-database-on-postgres add create database on p…
Jul 12, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add feature to create template in PostgreSQL
Main change in GeneralTemplateGatewayImpl.
Add configuration and description how to run it local.
  • Loading branch information
speedlog committed Jun 28, 2018
commit 496978ad257957545e3dfe1288843f6ea1248ffe
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();
}
}
Loading