From 8aec73df1f4888f7db3051fcf5cf84f5bfce623f Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sun, 17 Mar 2024 18:24:59 +0100 Subject: [PATCH 1/2] remove custom definitions --- .../apm/api/services/ScriptManager.java | 19 +------------------ .../core/endpoints/ScriptExecutionForm.java | 9 --------- .../endpoints/ScriptExecutionServlet.java | 4 ++-- .../core/jobs/ScriptRunnerJobConsumer.java | 11 +---------- .../apm/core/scripts/ScriptManagerImpl.java | 10 ++++------ .../services/async/AsyncScriptExecutor.java | 3 +-- .../async/AsyncScriptExecutorImpl.java | 7 ++----- .../apm/core/ui/models/ConsoleModel.java | 10 ++++++---- 8 files changed, 17 insertions(+), 56 deletions(-) diff --git a/app/aem/api/src/main/java/com/cognifide/apm/api/services/ScriptManager.java b/app/aem/api/src/main/java/com/cognifide/apm/api/services/ScriptManager.java index dc6886de..5f0c6be9 100644 --- a/app/aem/api/src/main/java/com/cognifide/apm/api/services/ScriptManager.java +++ b/app/aem/api/src/main/java/com/cognifide/apm/api/services/ScriptManager.java @@ -20,7 +20,6 @@ package com.cognifide.apm.api.services; import com.cognifide.apm.api.scripts.Script; -import java.util.Collections; import java.util.Map; import javax.jcr.RepositoryException; import org.apache.sling.api.resource.PersistenceException; @@ -39,23 +38,7 @@ default ExecutionResult process(Script script, ExecutionMode mode, ResourceResol /** * Fail-safe execution of script in concrete mode (dry run, automatic execution, validation) */ - default ExecutionResult process(Script script, ExecutionMode mode, Map customDefinitions, ResourceResolver resolver) - throws RepositoryException, PersistenceException { - return process(script, mode, customDefinitions, resolver, resolver.getUserID()); - } - - /** - * Fail-safe execution of script in concrete mode (dry run, automatic execution, validation) - */ - default ExecutionResult process(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) - throws RepositoryException, PersistenceException { - return process(script, mode, Collections.emptyMap(), resolver, executor); - } - - /** - * Fail-safe execution of script in concrete mode (dry run, automatic execution, validation) - */ - ExecutionResult process(Script script, ExecutionMode mode, Map customDefinitions, ResourceResolver resolver, String executor) + ExecutionResult process(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) throws RepositoryException, PersistenceException; /** diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionForm.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionForm.java index ca873f71..b974544c 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionForm.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionForm.java @@ -22,7 +22,6 @@ import com.cognifide.apm.api.services.ExecutionMode; import com.cognifide.apm.core.endpoints.params.RequestParameter; -import java.util.Map; import javax.inject.Inject; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; @@ -42,10 +41,6 @@ public class ScriptExecutionForm { @RequestParameter("async") private boolean async; - @Inject - @RequestParameter("define") - private Map customDefinitions; - public String getScript() { return script; } @@ -57,8 +52,4 @@ public ExecutionMode getExecutionMode() { public boolean isAsync() { return async; } - - public Map getCustomDefinitions() { - return customDefinitions; - } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionServlet.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionServlet.java index 95606657..20b30c98 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionServlet.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptExecutionServlet.java @@ -123,13 +123,13 @@ private ResponseEntity executeScript(ScriptExecutionForm form, ResourceResolver } private ResponseEntity asyncExecute(Script script, ScriptExecutionForm form, String executor) { - String id = asyncScriptExecutor.process(script, form.getExecutionMode(), form.getCustomDefinitions(), executor); + String id = asyncScriptExecutor.process(script, form.getExecutionMode(), executor); return ResponseEntity.ok("Script successfully queued for async execution") .addEntry("id", id); } private ResponseEntity syncExecute(Script script, ScriptExecutionForm form, ResourceResolver resourceResolver, String executor) throws PersistenceException, RepositoryException { - ExecutionResult result = scriptManager.process(script, form.getExecutionMode(), form.getCustomDefinitions(), resourceResolver, executor); + ExecutionResult result = scriptManager.process(script, form.getExecutionMode(), resourceResolver, executor); if (result.isSuccess()) { return ResponseEntity.ok("Script successfully executed") .addEntry("output", result.getEntries()); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/jobs/ScriptRunnerJobConsumer.java b/app/aem/core/src/main/java/com/cognifide/apm/core/jobs/ScriptRunnerJobConsumer.java index 45631526..3e819947 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/jobs/ScriptRunnerJobConsumer.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/jobs/ScriptRunnerJobConsumer.java @@ -29,7 +29,6 @@ import com.cognifide.apm.core.services.ResourceResolverProvider; import com.cognifide.apm.core.services.async.AsyncScriptExecutorImpl; import com.cognifide.apm.core.utils.sling.SlingHelper; -import java.util.Collections; import java.util.Map; import javax.jcr.RepositoryException; import org.apache.commons.lang3.StringUtils; @@ -71,7 +70,7 @@ public void process(Map properties) { Script script = getScript(properties, resolver); if (script != null && mode != null) { try { - ExecutionResult executionResult = scriptManager.process(script, mode, getDefinitions(properties), resolver, userId); + ExecutionResult executionResult = scriptManager.process(script, mode, resolver, userId); String summaryPath = getSummaryPath(resolver, script, mode); jobResultsCache.put(id, ExecutionSummary.finished(executionResult, summaryPath)); } catch (RepositoryException | PersistenceException e) { @@ -101,14 +100,6 @@ private ExecutionMode getMode(Map properties) { return result; } - private Map getDefinitions(Map properties) { - Map definitions = (Map) properties.get(AsyncScriptExecutorImpl.DEFINITIONS); - if (definitions == null) { - definitions = Collections.emptyMap(); - } - return definitions; - } - private Script getScript(Map properties, ResourceResolver resolver) { String scriptSearchPath = (String) properties.get(AsyncScriptExecutorImpl.SCRIPT_PATH); if (StringUtils.isNotBlank(scriptSearchPath)) { diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java index 82cb4ca6..538c7f75 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java @@ -99,7 +99,7 @@ public class ScriptManagerImpl implements ScriptManager { ) private final Set definitionsProviders = new CopyOnWriteArraySet<>(); - private Progress execute(Script script, ExecutionMode mode, Map customDefinitions, + private Progress execute(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) throws ExecutionException, RepositoryException { if (script == null) { throw new ExecutionException("Script is not specified"); @@ -139,9 +139,7 @@ private Progress execute(Script script, ExecutionMode mode, Map }, dataSourceInvoker); try { - Map definitions = new HashMap<>(); - definitions.putAll(getPredefinedDefinitions()); - definitions.putAll(customDefinitions); + Map definitions = getPredefinedDefinitions(); scriptRunner.execute(script, progress, definitions); } catch (RuntimeException e) { progress.addEntry(Status.ERROR, e.getMessage()); @@ -153,11 +151,11 @@ private Progress execute(Script script, ExecutionMode mode, Map } @Override - public Progress process(Script script, ExecutionMode mode, Map customDefinitions, + public Progress process(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) throws RepositoryException, PersistenceException { Progress progress; try { - progress = execute(script, mode, customDefinitions, resolver, executor); + progress = execute(script, mode, resolver, executor); } catch (ExecutionException e) { progress = new ProgressImpl(executor); progress.addEntry(Status.ERROR, e.getMessage()); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutor.java b/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutor.java index 2c15006c..4f6a3b9c 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutor.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutor.java @@ -21,11 +21,10 @@ import com.cognifide.apm.api.scripts.Script; import com.cognifide.apm.api.services.ExecutionMode; -import java.util.Map; public interface AsyncScriptExecutor { - String process(Script script, ExecutionMode executionMode, Map customDefinitions, String executor); + String process(Script script, ExecutionMode executionMode, String executor); ExecutionStatus checkStatus(String id); } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutorImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutorImpl.java index 4f099538..688a3a90 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutorImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/services/async/AsyncScriptExecutorImpl.java @@ -47,8 +47,6 @@ public class AsyncScriptExecutorImpl implements AsyncScriptExecutor { public static final String USER_ID = "userName"; - public static final String DEFINITIONS = "definitions"; - public static final String ID = "id"; @Reference @@ -58,14 +56,13 @@ public class AsyncScriptExecutorImpl implements AsyncScriptExecutor { private JobResultsCache jobResultsCache; @Override - public String process(Script script, ExecutionMode executionMode, Map customDefinitions, String executor) { + public String process(Script script, ExecutionMode executionMode, String executor) { String id = UUID.randomUUID().toString(); Map properties = ImmutableMap.of( ID, id, SCRIPT_PATH, script.getPath(), EXECUTION_MODE, executionMode.toString(), - USER_ID, executor, - DEFINITIONS, customDefinitions + USER_ID, executor ); jobResultsCache.put(id, ExecutionSummary.running()); new Thread(() -> scriptRunnerJobConsumer.process(properties)).start(); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/ui/models/ConsoleModel.java b/app/aem/core/src/main/java/com/cognifide/apm/core/ui/models/ConsoleModel.java index 2a935c23..d91c118e 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/ui/models/ConsoleModel.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/ui/models/ConsoleModel.java @@ -22,6 +22,7 @@ import com.cognifide.apm.api.scripts.Script; import com.cognifide.apm.api.services.ScriptFinder; import java.io.IOException; +import java.io.InputStream; import javax.inject.Inject; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; @@ -34,8 +35,6 @@ @Model(adaptables = SlingHttpServletRequest.class) public final class ConsoleModel { - public static final String PATH_PARAM = "path"; - private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleModel.class); private static final String FILE_NAME_DEFAULT = "filename"; @@ -66,11 +65,14 @@ public ConsoleModel(SlingHttpServletRequest request, @OSGiService ScriptFinder s private String getContentDefault() { try { - return IOUtils.toString(getClass().getResourceAsStream(CONTENT_FILE), CONTENT_FILE_CHARSET); + InputStream inputStream = getClass().getResourceAsStream(CONTENT_FILE); + if (inputStream != null) { + return IOUtils.toString(inputStream, CONTENT_FILE_CHARSET); + } } catch (IOException e) { LOGGER.warn("Cannot read content of default script template.", e); - return ""; } + return ""; } public String getFileName() { From 2ba78733c1f12214558325720d36aaae1cc8941a Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sun, 17 Mar 2024 18:28:54 +0100 Subject: [PATCH 2/2] remove custom definitions --- .../com/cognifide/apm/core/scripts/ScriptManagerImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java index 538c7f75..275dd65a 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptManagerImpl.java @@ -99,8 +99,8 @@ public class ScriptManagerImpl implements ScriptManager { ) private final Set definitionsProviders = new CopyOnWriteArraySet<>(); - private Progress execute(Script script, ExecutionMode mode, - ResourceResolver resolver, String executor) throws ExecutionException, RepositoryException { + private Progress execute(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) + throws ExecutionException, RepositoryException { if (script == null) { throw new ExecutionException("Script is not specified"); } @@ -151,8 +151,8 @@ private Progress execute(Script script, ExecutionMode mode, } @Override - public Progress process(Script script, ExecutionMode mode, - ResourceResolver resolver, String executor) throws RepositoryException, PersistenceException { + public Progress process(Script script, ExecutionMode mode, ResourceResolver resolver, String executor) + throws RepositoryException, PersistenceException { Progress progress; try { progress = execute(script, mode, resolver, executor);