gcube-cms-suite/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/MongoClientProvider.java

71 lines
2.6 KiB
Java
Raw Normal View History

2021-09-20 16:47:35 +02:00
package org.gcube.application.geoportal.service.engine.providers;
import lombok.extern.slf4j.Slf4j;
2022-02-18 14:41:41 +01:00
import org.gcube.application.cms.caches.AbstractScopedMap;
import org.gcube.application.cms.implementations.ISInterface;
2021-10-06 11:26:35 +02:00
import org.gcube.application.geoportal.service.ServiceConstants;
import org.gcube.application.cms.implementations.ImplementationProvider;
2021-10-14 17:00:08 +02:00
import org.gcube.application.geoportal.service.model.internal.db.Mongo;
2022-02-25 11:44:56 +01:00
import org.gcube.application.geoportal.common.model.configuration.MongoConnection;
2022-02-04 17:45:47 +01:00
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
2022-02-25 11:44:56 +01:00
import org.gcube.common.resources.gcore.ServiceEndpoint;
import java.util.List;
import java.util.Map;
2021-09-20 16:47:35 +02:00
@Slf4j
2022-02-18 14:41:41 +01:00
public class MongoClientProvider extends AbstractScopedMap<Mongo> {
2021-09-20 16:47:35 +02:00
public MongoClientProvider() {
super("MongoClient cache");
2021-10-06 11:26:35 +02:00
//NO TTL = Map by context
2021-09-20 16:47:35 +02:00
// setTTL(Duration.of(10, ChronoUnit.MINUTES));
}
@Override
2022-03-10 18:15:10 +01:00
protected Mongo retrieveObject(String context) throws ConfigurationException {
2022-02-25 11:44:56 +01:00
MongoConnection conn=performQueryForMongoDB(ImplementationProvider.get().
2022-03-04 18:28:45 +01:00
getEngineByManagedClass(ISInterface.class),
2022-02-25 11:44:56 +01:00
ServiceConstants.SE_GNA_DB_CATEGORY,
ServiceConstants.MONGO_SE_PLATFORM,
ServiceConstants.SE_GNA_DB_FLAG,
ServiceConstants.MONGO_SE_GNA_FLAG);
2021-10-06 11:26:35 +02:00
2021-09-20 16:47:35 +02:00
log.debug("Connecting to "+conn);
2021-10-14 17:00:08 +02:00
return new Mongo(conn);
2021-09-20 16:47:35 +02:00
}
@Override
2021-10-14 17:00:08 +02:00
protected void dispose(Mongo toDispose) {
2021-09-20 16:47:35 +02:00
toDispose.close();
}
2022-02-25 11:44:56 +01:00
private static MongoConnection performQueryForMongoDB(ISInterface is,String category, String platform,String flagName, String flagValue) throws ConfigurationException {
List<ServiceEndpoint.AccessPoint> found=is.performGetAP(category,platform,flagName, flagValue);
if(found.size()>1) {
throw new ConfigurationException("Multiple SE found ["+found.size()+"] for platform : "+platform+" flag : "+flagValue);
}else if (found.isEmpty()){
throw new ConfigurationException("No SE found for platform : "+platform+" flag : "+flagValue);
}
ServiceEndpoint.AccessPoint point=found.get(0);
MongoConnection toReturn=new MongoConnection();
for(ServiceEndpoint.Property prop:point.properties()) {
switch(prop.name()) {
case "host" : {
toReturn.getHosts().add(prop.value());
break;}
}
}
toReturn.getHosts().add(point.address());
Map<String, ServiceEndpoint.Property> props=point.propertyMap();
toReturn.setDatabase(props.get("database").value());
toReturn.setPassword(is.decryptString(point.password()));
toReturn.setPort(Integer.parseInt(props.get("port").value()));
toReturn.setUser(point.username());
return toReturn;
}
2021-09-20 16:47:35 +02:00
}