Fixes ##22217

This commit is contained in:
Fabio Sinibaldi 2021-10-14 17:00:08 +02:00
parent dd2c3c64cd
commit 86c1fed263
6 changed files with 59 additions and 34 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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());
}

View File

@ -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<MongoClient>{
public class MongoClientProvider extends AbstractScopedMap<Mongo>{
public MongoClientProvider() {
super("MongoClient cache");
@ -21,32 +17,17 @@ public class MongoClientProvider extends AbstractScopedMap<MongoClient>{
}
@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();
}

View File

@ -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();
}
}

View File

@ -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();
}