2022-03-23 18:38:07 +01:00
|
|
|
package org.gcube.application.geoportal.service.engine.providers.ucd;
|
2022-03-04 18:28:45 +01:00
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.gcube.application.cms.caches.AbstractScopedMap;
|
2022-04-27 19:36:10 +02:00
|
|
|
import org.gcube.application.cms.caches.Engine;
|
|
|
|
import org.gcube.application.cms.caches.ObjectManager;
|
2022-03-04 18:28:45 +01:00
|
|
|
import org.gcube.application.cms.implementations.ImplementationProvider;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
|
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
|
|
|
|
import org.gcube.application.geoportal.common.utils.ContextUtils;
|
|
|
|
import org.gcube.application.geoportal.service.engine.mongo.UCDManagerI;
|
|
|
|
import org.gcube.application.geoportal.service.engine.mongo.UCDMongoManager;
|
|
|
|
import org.gcube.application.geoportal.service.model.internal.faults.RegistrationException;
|
|
|
|
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
import javax.ws.rs.core.Response;
|
2022-05-06 18:03:48 +02:00
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.time.temporal.TemporalAmount;
|
2022-03-04 18:28:45 +01:00
|
|
|
import java.util.Map;
|
2022-04-01 19:11:11 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2022-05-06 18:03:48 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-03-04 18:28:45 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
2022-04-27 19:36:10 +02:00
|
|
|
import static java.lang.Thread.sleep;
|
|
|
|
|
2022-05-06 18:03:48 +02:00
|
|
|
/**
|
|
|
|
* Provider of UCDManagerI (this)
|
|
|
|
*
|
|
|
|
* - nb on get should check / trigger cache refresh TTL
|
|
|
|
* - uses UCDMongoManager for storing
|
|
|
|
* - profile live map engine is discovered
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2022-03-04 18:28:45 +01:00
|
|
|
@Slf4j
|
|
|
|
public class UCDManager extends AbstractScopedMap<UCDManagerI> implements UCDManagerI {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UCDManager() {
|
|
|
|
super("UCD MANAGER");
|
2022-05-06 18:03:48 +02:00
|
|
|
setTTL(Duration.of(1, ChronoUnit.MINUTES));
|
2022-03-04 18:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterable<UseCaseDescriptor> query(QueryRequest queryRequest) throws ConfigurationException {
|
|
|
|
return getMongoManager().query(queryRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteById(String id, boolean force) throws RegistrationException, ConfigurationException {
|
|
|
|
log.warn("Trying to delete {} [force : {}]",id,force);
|
|
|
|
// NB Check for existing ID
|
|
|
|
UseCaseDescriptor found = getById(id);
|
|
|
|
if(found!=null) {
|
|
|
|
// TODO validate DELETE REQUEST
|
|
|
|
// TODO STORE UCD
|
|
|
|
// forceUpdateCache();
|
|
|
|
throw new WebApplicationException("TO IMPLEMENT ", Response.Status.INTERNAL_SERVER_ERROR);
|
|
|
|
} else
|
|
|
|
throw new WebApplicationException("No Matching UCD with ID "+id, Response.Status.NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UseCaseDescriptor put(UseCaseDescriptor desc) throws ConfigurationException, RegistrationException {
|
|
|
|
log.debug("Update {} ",desc.getId());
|
|
|
|
// NB Check for existing ID
|
|
|
|
UseCaseDescriptor found = getById(desc.getId());
|
|
|
|
if(found!=null) {
|
|
|
|
// TODO validate UPDATE
|
|
|
|
// TODO STORE UCD
|
|
|
|
// forceUpdateCache();
|
2022-04-27 19:36:10 +02:00
|
|
|
throw new WebApplicationException("Update Feature is yet TO IMPLEMENT ", Response.Status.INTERNAL_SERVER_ERROR);
|
|
|
|
} else {
|
|
|
|
// create new
|
|
|
|
registerNew(desc);
|
|
|
|
do{
|
|
|
|
log.info("Waiting for backend to update.. ");
|
|
|
|
try{sleep(1000);}catch (Throwable t){}
|
|
|
|
forceUpdateCache();
|
|
|
|
found =getById(desc.getId());
|
|
|
|
} while(found == null);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
2022-03-04 18:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UseCaseDescriptor getById(String id) throws ConfigurationException, RegistrationException {
|
|
|
|
// GET from mongo cache
|
|
|
|
UCDMongoManager mongo=getMongoManager();
|
|
|
|
UseCaseDescriptor toReturn=mongo.getById(id);
|
|
|
|
log.debug("UCD ID : {} from mongo is {} ",id,toReturn);
|
|
|
|
if(toReturn == null) {
|
|
|
|
// IF void try from ProfileEngine
|
|
|
|
toReturn =getLiveMap().get(id);
|
|
|
|
if(toReturn != null ){
|
|
|
|
log.debug("Force update of live map {} from live map ",id);
|
|
|
|
toReturn = mongo.put(toReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-23 18:38:07 +01:00
|
|
|
private ProfileMap getLiveMap() throws ConfigurationException {
|
2022-04-27 19:36:10 +02:00
|
|
|
return ImplementationProvider.get().getProvidedObjectByClass(ProfileMap.class);
|
2022-03-04 18:28:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-04-27 19:36:10 +02:00
|
|
|
private void registerNew(UseCaseDescriptor ucd) throws ConfigurationException {
|
|
|
|
Engine<ProfileMap> engine=ImplementationProvider.get().getEngineByManagedClass(ProfileMap.class);
|
|
|
|
if(engine instanceof ObjectManager){
|
|
|
|
((ObjectManager<UseCaseDescriptor>)engine).insert(ucd);
|
|
|
|
} else throw new ConfigurationException("Profile Map Engine is not Object Manager. Actual implementation is "+engine.getClass());
|
|
|
|
}
|
|
|
|
|
2022-03-04 18:28:45 +01:00
|
|
|
private UCDMongoManager getMongoManager() throws ConfigurationException {
|
|
|
|
return new UCDMongoManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-10 18:15:10 +01:00
|
|
|
protected UCDManagerI retrieveObject(String context) throws ConfigurationException {
|
2022-03-04 18:28:45 +01:00
|
|
|
forceUpdateCache();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-01 19:11:11 +02:00
|
|
|
ConcurrentHashMap<String,Boolean> cleanedCaches= new ConcurrentHashMap<>();
|
|
|
|
|
2022-03-04 18:28:45 +01:00
|
|
|
|
|
|
|
private void forceUpdateCache() throws ConfigurationException {
|
2022-03-30 18:39:10 +02:00
|
|
|
log.info("UPDATING PROFILE CACHE..");
|
2022-03-04 18:28:45 +01:00
|
|
|
final UCDMongoManager manager = getMongoManager();
|
2022-04-01 19:11:11 +02:00
|
|
|
manager.deleteAll();
|
2022-03-04 18:28:45 +01:00
|
|
|
final AtomicLong counter= new AtomicLong(0l);
|
2022-03-30 18:39:10 +02:00
|
|
|
ProfileMap liveMap=getLiveMap();
|
|
|
|
log.debug("LiveMap size is {} ",liveMap.size());
|
|
|
|
for (Map.Entry<String, UseCaseDescriptor> entry : liveMap.entrySet()) {
|
2022-03-04 18:28:45 +01:00
|
|
|
UseCaseDescriptor useCaseDescriptor = entry.getValue();
|
|
|
|
try {
|
2022-03-30 18:39:10 +02:00
|
|
|
log.debug("Updateing cache with {} ", useCaseDescriptor.getId());
|
2022-03-04 18:28:45 +01:00
|
|
|
// insert/update into DB
|
|
|
|
manager.put(useCaseDescriptor);
|
|
|
|
} catch (RegistrationException e) {
|
|
|
|
log.warn("Unable to cache UCD {}",entry.getKey(),e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Cached : {} UCDs in {} ",counter.get(),ContextUtils.getCurrentScope());
|
|
|
|
}
|
|
|
|
}
|