Skip to content

Commit

Permalink
Create datasource and configuration to be able to use different datab…
Browse files Browse the repository at this point in the history
…ase for geonetwork information

remove unused import

change integration configuration file for pgsl GN port

update datasource name to avoid conflict

Correction on integration test

Change datasource name to fit configuration

change configuration for integration test

Update missing disctinction between geonetwork and georchestra connection
  • Loading branch information
pierrejego committed Jul 4, 2024
1 parent 3c8ff91 commit 97c08c6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class AdvancedDelegationDao {
private OrgsDao orgsDao;

@Autowired
private DataSource ds;
private DataSource dataSource;

public List<DelegationEntry> findByOrg(String org) throws SQLException {
final String sql = "SELECT uid, array_to_string(orgs, ',') AS orgs, array_to_string(roles, ',') AS roles FROM console.delegations WHERE ? = ANY(orgs)";
try (Connection c = ds.getConnection(); //
try (Connection c = dataSource.getConnection(); //
PreparedStatement byOrgStatement = c.prepareStatement(sql)) {
byOrgStatement.setString(1, org);
try (ResultSet resultSet = byOrgStatement.executeQuery()) {
Expand All @@ -44,7 +44,7 @@ public List<DelegationEntry> findByOrg(String org) throws SQLException {

public List<DelegationEntry> findByRole(String cn) throws SQLException {
final String sql = "SELECT uid, array_to_string(orgs, ',') AS orgs, array_to_string(roles, ',') AS roles FROM console.delegations WHERE ? = ANY(roles)";
try (Connection c = ds.getConnection(); //
try (Connection c = dataSource.getConnection(); //
PreparedStatement byRoleStatement = c.prepareStatement(sql)) {
byRoleStatement.setString(1, cn);
try (ResultSet resultSet = byRoleStatement.executeQuery()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@

import org.georchestra.ds.DataServiceException;
import org.georchestra.ds.users.Account;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -77,31 +74,39 @@ public class AccountGDPRDaoImpl implements AccountGDPRDao {
private static final String DELETE_OGCSTATS_RECORDS = "update ogcstatistics.ogc_services_log set user_name = ? where user_name = ?";

@Autowired
private DataSource ds;
private DataSource dataSource;

@Autowired
private DataSource dataSourceGeonetwork;

public void setDataSource(DataSource dataSource) {
this.ds = dataSource;
this.dataSource = dataSource;
}

public void setDataSourceGN(DataSource dataSource) {
this.dataSourceGeonetwork = dataSource;
}

/**
* Deletes (obfuscates) all GDPR sensitive records for the given account and
* s Deletes (obfuscates) all GDPR sensitive records for the given account and
* returns a summary of records affected. Deleting here means making the records
* untraceable to the account owner, but keep them under a "ghost" user name
* ({@code _deleted_account_}) for statistical purposes.
*/
public @Override DeletedRecords deleteAccountRecords(@NonNull Account account) throws DataServiceException {
try (Connection conn = ds.getConnection()) {
try (Connection conn = dataSource.getConnection(); Connection connGN = dataSourceGeonetwork.getConnection()) {
int metadataRecords;
int ogcStatsRecords = 0;
conn.setAutoCommit(false);
connGN.setAutoCommit(false);
try {
metadataRecords = deleteUserMetadataRecords(conn, account);
conn.commit();
metadataRecords = deleteUserMetadataRecords(connGN, account);
connGN.commit();
} catch (SQLException e) {
conn.rollback();
connGN.rollback();
throw new DataServiceException(e);
} finally {
conn.setAutoCommit(true);
connGN.setAutoCommit(true);
}
conn.setAutoCommit(false);
try {
Expand Down Expand Up @@ -168,9 +173,9 @@ private int deleteUserMetadataRecords(Connection conn, @NonNull Account account)
final String userName = owner.getUid();
try {
int reccount = visitRecords(QUERY_OGCSTATS_RECORDS, ps -> ps.setString(1, userName),
AccountGDPRDaoImpl::createOgcStatisticsRecord, consumer);
AccountGDPRDaoImpl::createOgcStatisticsRecord, consumer, dataSource.getConnection());
log.info("Extracted {} OGC statistics records for user {}", reccount, userName);
} catch (DataServiceException e) {
} catch (DataServiceException | SQLException e) {
throw new IllegalStateException(e);
}
}
Expand All @@ -180,9 +185,9 @@ private int deleteUserMetadataRecords(Connection conn, @NonNull Account account)
final String userName = owner.getUid();
try {
int reccount = visitRecords(QUERY_METADATA_RECORDS, ps -> ps.setString(1, userName),
AccountGDPRDaoImpl::createMetadataRecord, consumer);
AccountGDPRDaoImpl::createMetadataRecord, consumer, dataSourceGeonetwork.getConnection());
log.info("Extracted {} metadata records for user {}", reccount, userName);
} catch (DataServiceException e) {
} catch (DataServiceException | SQLException e) {
throw new IllegalStateException(e);
}
}
Expand All @@ -193,9 +198,9 @@ interface PreparedStatementBuilder {
}

private <R> int visitRecords(String psQuery, PreparedStatementBuilder psBuilder, Function<ResultSet, R> mapper,
@NonNull Consumer<R> consumer) throws DataServiceException {
@NonNull Consumer<R> consumer, Connection c) throws DataServiceException {

try (Connection c = ds.getConnection(); PreparedStatement ps = c.prepareStatement(psQuery)) {
try (PreparedStatement ps = c.prepareStatement(psQuery)) {

psBuilder.accept(ps);

Expand Down
55 changes: 35 additions & 20 deletions console/src/main/webapp/WEB-INF/spring/webmvc-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,42 @@
<property name="driverClassName" value="org.postgresql.Driver"/>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDb">
<property name="jdbcUrl" value="jdbc:postgresql://${pgsqlHost}:${pgsqlPort}/${pgsqlDatabase}"/>
<property name="user" value="${pgsqlUser}"/>
<property name="password" value="${pgsqlPassword}"/>
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="initialPoolSize" value="2"/>
<property name="minPoolSize" value="${dataSource.minPoolSize:2}"/>
<property name="maxPoolSize" value="${dataSource.maxPoolSize:10}"/>
<property name="checkoutTimeout" value = "${dataSource.timeout:1000}"/>
<property name="idleConnectionTestPeriod" value="30"/>
<property name="maxIdleTime" value = "${dataSource.maxIdleTime:60}"/>
<property name="acquireIncrement" value="2"/>
<property name="testConnectionOnCheckout" value="true"/> <!-- Uses JDBC4's Connection.isValid() -->
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDb">
<property name="jdbcUrl" value="jdbc:postgresql://${pgsqlHost}:${pgsqlPort}/${pgsqlDatabase}"/>
<property name="user" value="${pgsqlUser}"/>
<property name="password" value="${pgsqlPassword}"/>
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="initialPoolSize" value="2"/>
<property name="minPoolSize" value="${dataSource.minPoolSize:2}"/>
<property name="maxPoolSize" value="${dataSource.maxPoolSize:10}"/>
<property name="checkoutTimeout" value = "${dataSource.timeout:1000}"/>
<property name="idleConnectionTestPeriod" value="30"/>
<property name="maxIdleTime" value = "${dataSource.maxIdleTime:60}"/>
<property name="acquireIncrement" value="2"/>
<property name="testConnectionOnCheckout" value="true"/> <!-- Uses JDBC4's Connection.isValid() -->
</bean>

<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName" value="org.postgresql.Driver"/>-->
<!--<property name="url" value="jdbc:postgresql://${pgsqlHost}:${pgsqlPort}/${pgsqlDatabase}"/>-->
<!--<property name="username" value="${pgsqlUser}"/>-->
<!--<property name="password" value="${pgsqlPassword}"/>-->
<!--</bean>-->
<bean id="waitForDbGeonetwork" class="org.georchestra.commons.WaitForDb">
<property name="url" value="jdbc:postgresql://${pgsqlGNHost}:${pgsqlGNPort}/${pgsqlGNDatabase}"/>
<property name="username" value="${pgsqlGNUser}"/>
<property name="password" value="${pgsqlGNPassword}"/>
<property name="driverClassName" value="org.postgresql.Driver"/>
</bean>

<bean id="dataSourceGeonetwork" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDbGeonetwork">
<property name="jdbcUrl" value="jdbc:postgresql://${pgsqlGNHost}:${pgsqlGNPort}/${pgsqlGNDatabase}"/>
<property name="user" value="${pgsqlGNUser}"/>
<property name="password" value="${pgsqlGNPassword}"/>
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="initialPoolSize" value="2"/>
<property name="minPoolSize" value="${dataSource.minPoolSize:2}"/>
<property name="maxPoolSize" value="${dataSource.maxPoolSize:10}"/>
<property name="checkoutTimeout" value = "${dataSource.timeout:1000}"/>
<property name="idleConnectionTestPeriod" value="30"/>
<property name="maxIdleTime" value = "${dataSource.maxIdleTime:60}"/>
<property name="acquireIncrement" value="2"/>
<property name="testConnectionOnCheckout" value="true"/> <!-- Uses JDBC4's Connection.isValid() -->
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class ConnectionPoolBehaviorIT extends ConsoleIntegrationTest {

public @Rule @Autowired IntegrationTestSupport support;

private @Autowired DataSource ds;
private @Autowired DataSource dataSource;

private @Value("${dataSource.maxPoolSize}") int maxPoolSize;

Expand All @@ -75,7 +75,7 @@ public class ConnectionPoolBehaviorIT extends ConsoleIntegrationTest {
List<Connection> consumeAll = new ArrayList<>();
for (int i = 0; i < maxPoolSize; i++) {
try {
Connection connection = ds.getConnection();
Connection connection = dataSource.getConnection();
consumeAll.add(connection);
if (testConnection == null) {// keep one to be used after the server closed all connections
testConnection = connection;
Expand Down Expand Up @@ -117,7 +117,7 @@ public class ConnectionPoolBehaviorIT extends ConsoleIntegrationTest {

// but the pool should be able to provision up to maxPoolSize connections again
for (int i = 0; i < maxPoolSize; i++) {
try (Connection newConnection = ds.getConnection()) {
try (Connection newConnection = dataSource.getConnection()) {
assertThat(countConnections(newConnection), greaterThanOrEqualTo(1));
}
}
Expand All @@ -128,7 +128,7 @@ public class ConnectionPoolBehaviorIT extends ConsoleIntegrationTest {
}

for (int i = 0; i < maxPoolSize; i++) {
try (Connection newConnection = ds.getConnection()) {
try (Connection newConnection = dataSource.getConnection()) {
assertThat(countConnections(newConnection), greaterThanOrEqualTo(1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AdvancedDelegationDaoIT extends ConsoleIntegrationTest {

private @Autowired AdvancedDelegationDao delegate;

private @Autowired ComboPooledDataSource ds;
private @Autowired ComboPooledDataSource dataSource;

private @Value("${dataSource.maxPoolSize:10}") int maxConnections;
private @Value("${dataSource.timeout:1000}") int timeoutMillis;
Expand All @@ -77,7 +77,7 @@ public class AdvancedDelegationDaoIT extends ConsoleIntegrationTest {
private boolean exhaustConnectionPool() {
List<Connection> allConnections = IntStream.range(0, maxConnections).mapToObj(i -> {
try {
return ds.getConnection();
return dataSource.getConnection();
} catch (SQLException e) {
return null;
}
Expand All @@ -86,7 +86,7 @@ private boolean exhaustConnectionPool() {
// upgrading the spring version.
assertTrue(allConnections.size() == maxConnections || allConnections.size() == maxConnections - 1);
try {
ds.getConnection();
dataSource.getConnection();
} catch (SQLException expected) {
allConnections.forEach(t -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class PasswordRecoverySurvivesDatabaseRestartIT extends ConsoleIntegratio

public @Rule @Autowired IntegrationTestSupport support;

private @Autowired DataSource ds;
private @Autowired DataSource dataSource;

private @Value("${dataSource.maxPoolSize}") int maxPoolSize;

Expand Down Expand Up @@ -110,7 +110,7 @@ private void forceCloseConnectionsBehindPool() throws SQLException {
List<Connection> consumeAll = new ArrayList<>();
for (int i = 0; i < maxPoolSize; i++) {
try {
Connection connection = ds.getConnection();
Connection connection = dataSource.getConnection();
consumeAll.add(connection);
if (i == 0) {
testConnection = connection;
Expand Down
6 changes: 6 additions & 0 deletions console/src/test/resources/console-it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ dataSource.timeout = 1000
#max time unused connections are kept idle in the pool. Unit is seconds for c3p0.
dataSource.maxIdleTime=60

# PostGreSQL database connection parameters to geonetwork
pgsqlGNHost=${jdbc.host:localhost}
pgsqlGNPort=${pgsqlPort}
pgsqlGNDatabase=georchestra
pgsqlGNUser=georchestra
pgsqlGNPassword=georchestra

# SMTP configuration
smtpHost=localhost
Expand Down
7 changes: 7 additions & 0 deletions console/src/test/resources/console.properties
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ pgsqlDatabase=georchestra
pgsqlUser=georchestra
pgsqlPassword=georchestra

# PostGreSQL database connection parameters to geonetwork
pgsqlGNHost=localhost
pgsqlGNPort=${psql.port}
pgsqlGNDatabase=georchestra
pgsqlGNUser=georchestra
pgsqlGNPassword=georchestra

# SMTP configuration
smtpHost=localhost
smtpPort=25
Expand Down
29 changes: 22 additions & 7 deletions console/src/test/resources/webmvc-config-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<property name="driverClassName" value="org.postgresql.Driver"/>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDb">
<bean id="dataSource" primary="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDb">
<property name="jdbcUrl" value="jdbc:postgresql://${pgsqlHost}:${pgsqlPort}/${pgsqlDatabase}"/>
<property name="user" value="${pgsqlUser}"/>
<property name="password" value="${pgsqlPassword}"/>
Expand All @@ -170,12 +170,27 @@
<property name="testConnectionOnCheckout" value="true"/> <!-- Uses JDBC4's Connection.isValid() -->
</bean>

<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--<property name="driverClassName" value="org.postgresql.Driver"/>-->
<!--<property name="url" value="jdbc:postgresql://${pgsqlHost}:${pgsqlPort}/${pgsqlDatabase}"/>-->
<!--<property name="username" value="${pgsqlUser}"/>-->
<!--<property name="password" value="${pgsqlPassword}"/>-->
<!--</bean>-->
<bean id="waitForDbGeonetwork" class="org.georchestra.commons.WaitForDb">
<property name="url" value="jdbc:postgresql://${pgsqlGNHost}:${pgsqlGNPort}/${pgsqlGNDatabase}"/>
<property name="username" value="${pgsqlGNUser}"/>
<property name="password" value="${pgsqlGNPassword}"/>
<property name="driverClassName" value="org.postgresql.Driver"/>
</bean>

<bean id="dataSourceGeonetwork" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="waitForDbGeonetwork">
<property name="jdbcUrl" value="jdbc:postgresql://${pgsqlGNHost}:${pgsqlGNPort}/${pgsqlGNDatabase}"/>
<property name="user" value="${pgsqlGNUser}"/>
<property name="password" value="${pgsqlGNPassword}"/>
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="initialPoolSize" value="2"/>
<property name="minPoolSize" value="${dataSource.minPoolSize:2}"/>
<property name="maxPoolSize" value="${dataSource.maxPoolSize:10}"/>
<property name="checkoutTimeout" value = "${dataSource.timeout:1000}"/>
<property name="idleConnectionTestPeriod" value="30"/>
<property name="maxIdleTime" value = "${dataSource.maxIdleTime:60}"/>
<property name="acquireIncrement" value="2"/>
<property name="testConnectionOnCheckout" value="true"/> <!-- Uses JDBC4's Connection.isValid() -->
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
Expand Down

0 comments on commit 97c08c6

Please sign in to comment.