From 0322042d7302c414ec38cdca1e0e61f1b11ed430 Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Fri, 29 Apr 2022 15:31:17 +0200 Subject: [PATCH] Configurable Multiple handler management --- .../cms/plugins/PluginManagerInterface.java | 4 +- .../implementations/Default3PhaseManager.java | 25 ++++++--- .../SimpleLifeCycleManager.java | 52 +++++++++++++------ .../cms/commons/model/Serialization.java | 6 +++ .../engine/providers/PluginManager.java | 12 +++++ .../cms/sdi/plugins/SDIIndexerPlugin.java | 1 + 6 files changed, 75 insertions(+), 25 deletions(-) diff --git a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/PluginManagerInterface.java b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/PluginManagerInterface.java index 1a0c2f4..d923303 100644 --- a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/PluginManagerInterface.java +++ b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/PluginManagerInterface.java @@ -3,8 +3,10 @@ package org.gcube.application.cms.plugins; import org.gcube.application.cms.plugins.Plugin; import org.gcube.application.geoportal.common.model.rest.ConfigurationException; +import java.util.Map; + public interface PluginManagerInterface { public Plugin getById(String pluginID) throws ConfigurationException; - + public Map getByType(String type) throws ConfigurationException; } diff --git a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/Default3PhaseManager.java b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/Default3PhaseManager.java index 8eb629b..bec4c41 100644 --- a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/Default3PhaseManager.java +++ b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/Default3PhaseManager.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.bson.Document; import org.gcube.application.cms.plugins.IndexerPluginInterface; import org.gcube.application.cms.plugins.LifecycleManager; +import org.gcube.application.cms.plugins.MaterializationPlugin; import org.gcube.application.cms.plugins.faults.*; import org.gcube.application.cms.plugins.implementations.executions.GuardedStepExecution; import org.gcube.application.cms.plugins.reports.*; @@ -48,17 +49,23 @@ public class Default3PhaseManager extends SimpleLifeCycleManager implements Life @Override protected EventExecutionReport onDeleteDocument(EventExecutionReport report) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException { report = super.onDeleteDocument(report); - return deIndex(report,getIndexer(),getInternalIndexParams(report.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(report.getTheRequest())) + report= deIndex(report,indexer,getInternalIndexParams(report.getTheRequest())); + return report; } @Override protected EventExecutionReport onDeleteFileSet(EventExecutionReport theReport) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException { theReport = super.onDeleteFileSet(theReport); String phase = theReport.getTheRequest().getDocument().getLifecycleInformation().getPhase(); + Document parameters = null; if(phase.equals(Phases.PENDING_APPROVAL)) - return index(theReport,getIndexer(),getInternalIndexParams(theReport.getTheRequest())); + parameters =getInternalIndexParams(theReport.getTheRequest()); if(phase.equals(Phases.PUBLISHED)) - return index(theReport,getIndexer(),getPublicIndexParams(theReport.getTheRequest())); + parameters = getPublicIndexParams(theReport.getTheRequest()); + if(parameters!= null) + for(IndexerPluginInterface indexer : getIndexer(theReport.getTheRequest())) + theReport = index(theReport,indexer,getPublicIndexParams(theReport.getTheRequest())); return theReport; } @@ -69,11 +76,13 @@ public class Default3PhaseManager extends SimpleLifeCycleManager implements Life @Override protected StepExecutionReport run() throws Exception { // Materialize - theReport = materializeDocument(theReport,getMaterializer(),getMaterializationParameters(theReport.getTheRequest())); + + for(MaterializationPlugin mat : getMaterializer(theReport.getTheRequest())) + theReport = materializeDocument(theReport,mat,getMaterializationParameters(theReport.getTheRequest())); if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)){ // Index - theReport = index(theReport,getIndexer(), - getInternalIndexParams(theReport.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(theReport.getTheRequest())) + theReport = index(theReport,indexer,getInternalIndexParams(theReport.getTheRequest())); // setPhase if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) theReport.getToSetLifecycleInformation().setPhase(Phases.PENDING_APPROVAL); @@ -87,8 +96,8 @@ public class Default3PhaseManager extends SimpleLifeCycleManager implements Life @Override protected StepExecutionReport run() throws Exception { // Index - theReport = index(theReport,getIndexer(), - getPublicIndexParams(theReport.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(theReport.getTheRequest())) + theReport = index(theReport,indexer,getPublicIndexParams(theReport.getTheRequest())); // setPhase if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) theReport.getToSetLifecycleInformation().setPhase(Phases.PUBLISHED); diff --git a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/SimpleLifeCycleManager.java b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/SimpleLifeCycleManager.java index d356940..b772746 100644 --- a/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/SimpleLifeCycleManager.java +++ b/cms-plugin-framework/src/main/java/org/gcube/application/cms/plugins/implementations/SimpleLifeCycleManager.java @@ -15,12 +15,17 @@ import org.gcube.application.geoportal.common.model.configuration.Configuration; import org.gcube.application.geoportal.common.model.configuration.Index; import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; +import org.gcube.application.geoportal.common.model.plugins.IndexerPluginDescriptor; +import org.gcube.application.geoportal.common.model.plugins.MaterializerPluginDescriptor; import org.gcube.application.geoportal.common.model.plugins.OperationDescriptor; import org.gcube.application.geoportal.common.model.rest.ConfigurationException; +import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; +import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; import org.gcube.application.geoportal.common.utils.Files; import java.util.ArrayList; import java.util.Collections; +import java.util.List; @Slf4j public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements LifecycleManager { @@ -85,17 +90,18 @@ public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements setStep(new GuardedStepExecution(Steps.PUBLISH) { @Override protected StepExecutionReport run() throws Exception { - if(!theReport.getTheRequest().getDocument().getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE)) - throw new StepException("Document is not in "+LifecycleInformation.CommonPhases.DRAFT_PHASE+" phase"); + //Check Performed by Guarded Step Execution +// if(!theReport.getTheRequest().getDocument().getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE)) +// throw new StepException("Document is not in "+LifecycleInformation.CommonPhases.DRAFT_PHASE+" phase"); // Materialize - theReport = materializeDocument(theReport, getMaterializer(), - getMaterializationParameters(theReport.getTheRequest())); + for(MaterializationPlugin mat : getMaterializer(theReport.getTheRequest())) + theReport = materializeDocument(theReport, mat, getMaterializationParameters(theReport.getTheRequest())); if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)){ // Index - theReport = index(theReport,getIndexer(), - getPublicIndexParams(theReport.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(theReport.getTheRequest())) + theReport = index(theReport,indexer,getPublicIndexParams(theReport.getTheRequest())); // setPhase if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) theReport.getToSetLifecycleInformation().setPhase("PUBLISHED"); @@ -133,12 +139,14 @@ public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements JSONPathWrapper wrapper = new JSONPathWrapper(report.getTheRequest().getDocument().getTheDocument().toJson()); for (String s : wrapper.getMatchingPaths("..*[?(@." + RegisteredFileSet.PAYLOADS + ")]")){ log.info("Requesting dematerialization for {} ",s); - report = deMaterialize(report,getMaterializer(),new Document("fileSetPath",s)); + for(MaterializationPlugin mat : getMaterializer(report.getTheRequest())) + report = deMaterialize(report,mat,new Document("fileSetPath",s)); if(!report.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) break; } if(report.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) { - report = deIndex(report,getIndexer(),getPublicIndexParams(report.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(report.getTheRequest())) + report = deIndex(report,indexer,getPublicIndexParams(report.getTheRequest())); } return report; } @@ -147,11 +155,13 @@ public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements protected EventExecutionReport onDeleteFileSet(EventExecutionReport theReport) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException { // dematerialize selected blockNonDraft(theReport); - deMaterialize(theReport,getMaterializer(), - theReport.getTheRequest().getCallParameters()); + for(MaterializationPlugin mat : getMaterializer(theReport.getTheRequest())) + deMaterialize(theReport,mat, + theReport.getTheRequest().getCallParameters()); // de index - if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) - deIndex(theReport,getIndexer(),getPublicIndexParams(theReport.getTheRequest())); + for(IndexerPluginInterface indexer : getIndexer(theReport.getTheRequest())) + if(theReport.getToSetLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK)) + deIndex(theReport,indexer,getPublicIndexParams(theReport.getTheRequest())); return theReport; } @@ -162,12 +172,22 @@ public class SimpleLifeCycleManager extends AbstractLifeCycleManager implements } - protected IndexerPluginInterface getIndexer() throws ConfigurationException { - return (IndexerPluginInterface) pluginManager.getById("SDI-Indexer-Plugin"); + protected List getIndexer(BaseRequest request) throws ConfigurationException { + ArrayList toReturn=new ArrayList<>(); + UseCaseDescriptor desc = request.getUseCaseDescriptor(); + List indexers = desc.getHandlersMapByType().get(IndexerPluginDescriptor.INDEXER); + for (HandlerDeclaration handlerDeclaration : indexers) + toReturn.add((IndexerPluginInterface) pluginManager.getById(handlerDeclaration.getId())); + return toReturn; } - protected MaterializationPlugin getMaterializer() throws ConfigurationException { - return (MaterializationPlugin) pluginManager.getById("SDI-Default-Materializer"); + protected List getMaterializer(BaseRequest request) throws ConfigurationException { + ArrayList toReturn=new ArrayList<>(); + UseCaseDescriptor desc = request.getUseCaseDescriptor(); + List materializers = desc.getHandlersMapByType().get(MaterializerPluginDescriptor.MATERIALIZER); + for (HandlerDeclaration handlerDeclaration : materializers) + toReturn.add((MaterializationPlugin) pluginManager.getById(handlerDeclaration.getId())); + return toReturn; } protected T deIndex(T report, IndexerPluginInterface indexer, Document parameters) throws InvalidPluginRequestException { diff --git a/cms-test-commons/src/test/java/org/gcube/application/cms/commons/model/Serialization.java b/cms-test-commons/src/test/java/org/gcube/application/cms/commons/model/Serialization.java index 1f50d09..c1b0b06 100644 --- a/cms-test-commons/src/test/java/org/gcube/application/cms/commons/model/Serialization.java +++ b/cms-test-commons/src/test/java/org/gcube/application/cms/commons/model/Serialization.java @@ -1,6 +1,12 @@ package org.gcube.application.cms.commons.model; +import org.junit.Test; + public class Serialization { + @Test + public void testSerializations(){ + + } } diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/PluginManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/PluginManager.java index 0768fea..300088b 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/PluginManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/PluginManager.java @@ -11,6 +11,7 @@ import org.gcube.application.geoportal.common.utils.ContextUtils; import org.gcube.application.geoportal.common.model.rest.ConfigurationException; import java.util.HashMap; +import java.util.Map; @Slf4j public class PluginManager extends AbstractScopedMap implements PluginManagerInterface { @@ -128,4 +129,15 @@ public class PluginManager extends AbstractScopedMap im log.warn("Invalid report provided by {} ",p.getDescriptor().getId(),e); } } + + + @Override + public Map getByType(String type) throws ConfigurationException { + HashMap toReturn = new HashMap<>(); + getObject().forEach((s, plugin) -> { + if (plugin!=null&&plugin.getDescriptor().getType().equals(type)) + toReturn.put(s,plugin); + }); + return toReturn; + } } diff --git a/sdi-plugins/src/main/java/org/gcube/application/cms/sdi/plugins/SDIIndexerPlugin.java b/sdi-plugins/src/main/java/org/gcube/application/cms/sdi/plugins/SDIIndexerPlugin.java index a29c2b5..d90cc76 100644 --- a/sdi-plugins/src/main/java/org/gcube/application/cms/sdi/plugins/SDIIndexerPlugin.java +++ b/sdi-plugins/src/main/java/org/gcube/application/cms/sdi/plugins/SDIIndexerPlugin.java @@ -133,6 +133,7 @@ public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPlugin log.debug("Using user defined spatial reference " + reference); + GeoJsonObject object = Serialization.convert(reference.getGeoJson(), GeoJsonObject.class); GCubeSDILayer.BBOX bbox = GCubeSDILayer.BBOX.fromGeoJSON(object.getBbox());