2020-11-19 17:38:56 +01:00
|
|
|
package org.gcube.application.geoportal.service.engine.cache;
|
|
|
|
|
|
|
|
import org.gcube.application.geoportal.model.fault.ConfigurationException;
|
|
|
|
import org.gcube.application.geoportal.service.engine.AbstractScopedMap;
|
|
|
|
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
|
|
|
|
import org.gcube.application.geoportal.service.model.internal.db.MongoConnection;
|
|
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
|
|
|
import com.mongodb.MongoClientOptions;
|
|
|
|
import com.mongodb.MongoCredential;
|
|
|
|
import com.mongodb.ServerAddress;
|
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
2020-11-19 17:38:56 +01:00
|
|
|
public class MongoClientProvider extends AbstractScopedMap<MongoClient>{
|
|
|
|
|
|
|
|
public MongoClientProvider() {
|
|
|
|
super("MongoClient cache");
|
|
|
|
|
|
|
|
// setTTL(Duration.of(10,ChronoUnit.MINUTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected MongoClient retrieveObject() throws ConfigurationException {
|
|
|
|
MongoConnection conn=ImplementationProvider.get().getMongoConnectionProvider().getObject();
|
2020-12-17 18:27:45 +01:00
|
|
|
log.debug("Connecting to "+conn);
|
|
|
|
|
2020-11-19 17:38:56 +01:00
|
|
|
MongoCredential credential = MongoCredential.createCredential(conn.getUser(), conn.getDatabase(),
|
|
|
|
conn.getPassword().toCharArray());
|
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
|
|
|
|
|
2020-11-19 17:38:56 +01:00
|
|
|
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
|
|
|
|
|
|
|
|
return new MongoClient(new ServerAddress(conn.getHosts().get(0),conn.getPort()),
|
|
|
|
credential,
|
|
|
|
options);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void dispose(MongoClient toDispose) {
|
|
|
|
toDispose.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
}
|