From 86c1fed26327361155ac251a848ca2db5c5a1fbe Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Thu, 14 Oct 2021 17:00:08 +0200 Subject: [PATCH] Fixes ##22217 --- geoportal-service/CHANGELOG.md | 1 + .../engine/mongo/ConcessioniMongoManager.java | 6 ++- .../service/engine/mongo/MongoManager.java | 11 ++--- .../engine/providers/MongoClientProvider.java | 29 +++---------- .../service/model/internal/db/Mongo.java | 41 +++++++++++++++++++ .../geoportal/service/MongoTests.java | 5 ++- 6 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/Mongo.java diff --git a/geoportal-service/CHANGELOG.md b/geoportal-service/CHANGELOG.md index 42651a9..b09f888 100644 --- a/geoportal-service/CHANGELOG.md +++ b/geoportal-service/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [v1.0.6] 2021-09-20 Refactored repositories Fixes #22193 +Fixes #22217 ## [v1.0.5-SNAPSHOT] 2021-07-23 Upgrade to gcube-smartgears-bom 2.1.0 diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ConcessioniMongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ConcessioniMongoManager.java index 8cdfc95..07e7c7e 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ConcessioniMongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ConcessioniMongoManager.java @@ -57,7 +57,11 @@ public class ConcessioniMongoManager extends MongoManager{ @Synchronized protected MongoDatabase getDatabase() { if(db==null) { - db=client.getDatabase(DB_NAME); + String toUseDB=super.client.getConnection().getDatabase(); + log.info("Connecting to DB {} ",toUseDB); + + // TODO MAP OF DATABASES? + db=client.getTheClient().getDatabase(toUseDB); } return db; } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java index d2436e1..5927fac 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManager.java @@ -14,6 +14,7 @@ import org.bson.Document; import org.bson.types.ObjectId; import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.service.engine.ImplementationProvider; +import org.gcube.application.geoportal.service.model.internal.db.Mongo; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; import static com.mongodb.client.model.Filters.eq; @@ -23,7 +24,8 @@ import static com.mongodb.client.model.Sorts.descending; @Slf4j public abstract class MongoManager { - protected MongoClient client=null; + + protected Mongo client=null; protected static final String ID="_id"; @@ -36,13 +38,8 @@ public abstract class MongoManager { public MongoManager() throws ConfigurationException { client=ImplementationProvider.get().getMongoClientProvider().getObject(); - log.info("Got Mongo Client at "+client.getConnectPoint()); + log.info("Got Mongo Client at "+client.getConnection()); log.debug("Mongo client is "+client); - - - - // NOT AUTHORIZED -// log.debug("Existing databases "+client.getDatabaseNames()); } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java index 6626389..215e811 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java @@ -1,18 +1,14 @@ package org.gcube.application.geoportal.service.engine.providers; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; import lombok.extern.slf4j.Slf4j; import org.gcube.application.geoportal.service.ServiceConstants; import org.gcube.application.geoportal.service.engine.ImplementationProvider; +import org.gcube.application.geoportal.service.model.internal.db.Mongo; import org.gcube.application.geoportal.service.model.internal.db.MongoConnection; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; -import org.gcube.application.geoportal.service.utils.ISUtils; @Slf4j -public class MongoClientProvider extends AbstractScopedMap{ +public class MongoClientProvider extends AbstractScopedMap{ public MongoClientProvider() { super("MongoClient cache"); @@ -21,32 +17,17 @@ public class MongoClientProvider extends AbstractScopedMap{ } @Override - protected MongoClient retrieveObject() throws ConfigurationException { + protected Mongo retrieveObject() throws ConfigurationException { MongoConnection conn=ImplementationProvider.get(). getIsProvider().getIS().queryForMongoDB( ServiceConstants.MONGO_SE_PLATFORM, ServiceConstants.MONGO_SE_GNA_FLAG); log.debug("Connecting to "+conn); - - MongoCredential credential = MongoCredential.createCredential(conn.getUser(), conn.getDatabase(), - conn.getPassword().toCharArray()); - - MongoClientOptions options = MongoClientOptions.builder(). - threadsAllowedToBlockForConnectionMultiplier(10). - connectionsPerHost(20). - maxConnectionIdleTime(10000). - applicationName("geoportal-service"). - sslEnabled(true). - connectTimeout(30000). - build(); - - return new MongoClient(new ServerAddress(conn.getHosts().get(0),conn.getPort()), - credential, - options); + return new Mongo(conn); } @Override - protected void dispose(MongoClient toDispose) { + protected void dispose(Mongo toDispose) { toDispose.close(); } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/Mongo.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/Mongo.java new file mode 100644 index 0000000..dff03dc --- /dev/null +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/Mongo.java @@ -0,0 +1,41 @@ +package org.gcube.application.geoportal.service.model.internal.db; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import lombok.Getter; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@ToString +public class Mongo { + @Getter + private MongoClient theClient = null; + @Getter + private MongoConnection connection = null; + + public Mongo(MongoConnection conn) { + connection = conn; + MongoCredential credential = MongoCredential.createCredential(conn.getUser(), conn.getDatabase(), + conn.getPassword().toCharArray()); + + MongoClientOptions options = MongoClientOptions.builder(). + threadsAllowedToBlockForConnectionMultiplier(10). + connectionsPerHost(20). + maxConnectionIdleTime(10000). + applicationName("geoportal-service"). + sslEnabled(true). + connectTimeout(30000). + build(); + + theClient = new MongoClient(new ServerAddress(conn.getHosts().get(0), conn.getPort()), + credential, + options); + } + + public void close() { + theClient.close(); + } +} diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java index 56d6af2..0c3a363 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/MongoTests.java @@ -11,6 +11,7 @@ import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.geoportal.service.engine.ImplementationProvider; import org.gcube.application.geoportal.service.engine.mongo.MongoManager; import org.gcube.application.geoportal.service.engine.providers.MongoClientProvider; +import org.gcube.application.geoportal.service.model.internal.db.Mongo; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; import org.junit.BeforeClass; import org.junit.Test; @@ -28,7 +29,7 @@ public class MongoTests { @Override protected MongoDatabase getDatabase() { - return client.getDatabase("gna_dev"); + return client.getTheClient().getDatabase("gna_dev"); } } @@ -37,7 +38,7 @@ public class MongoTests { public static final void init() { ImplementationProvider.get().setMongoClientProvider(new MongoClientProvider() { @Override - public MongoClient getObject() throws ConfigurationException { + public Mongo getObject() throws ConfigurationException { TokenSetter.set("/gcube/devsec/devVRE"); return super.getObject(); }