Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove custom definitions #438

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> 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<String, String> customDefinitions, ResourceResolver resolver, String executor)
ExecutionResult process(Script script, ExecutionMode mode, ResourceResolver resolver, String executor)
throws RepositoryException, PersistenceException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,10 +41,6 @@ public class ScriptExecutionForm {
@RequestParameter("async")
private boolean async;

@Inject
@RequestParameter("define")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

private Map<String, String> customDefinitions;

public String getScript() {
return script;
}
Expand All @@ -57,8 +52,4 @@ public ExecutionMode getExecutionMode() {
public boolean isAsync() {
return async;
}

public Map<String, String> getCustomDefinitions() {
return customDefinitions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +70,7 @@ public void process(Map<String, Object> 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) {
Expand Down Expand Up @@ -101,14 +100,6 @@ private ExecutionMode getMode(Map<String, Object> properties) {
return result;
}

private Map<String, String> getDefinitions(Map<String, Object> properties) {
Map<String, String> definitions = (Map<String, String>) properties.get(AsyncScriptExecutorImpl.DEFINITIONS);
if (definitions == null) {
definitions = Collections.emptyMap();
}
return definitions;
}

private Script getScript(Map<String, Object> properties, ResourceResolver resolver) {
String scriptSearchPath = (String) properties.get(AsyncScriptExecutorImpl.SCRIPT_PATH);
if (StringUtils.isNotBlank(scriptSearchPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public class ScriptManagerImpl implements ScriptManager {
)
private final Set<DefinitionsProvider> definitionsProviders = new CopyOnWriteArraySet<>();

private Progress execute(Script script, ExecutionMode mode, Map<String, String> customDefinitions,
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");
}
Expand Down Expand Up @@ -139,9 +139,7 @@ private Progress execute(Script script, ExecutionMode mode, Map<String, String>
}, dataSourceInvoker);

try {
Map<String, String> definitions = new HashMap<>();
definitions.putAll(getPredefinedDefinitions());
definitions.putAll(customDefinitions);
Map<String, String> definitions = getPredefinedDefinitions();
scriptRunner.execute(script, progress, definitions);
} catch (RuntimeException e) {
progress.addEntry(Status.ERROR, e.getMessage());
Expand All @@ -153,11 +151,11 @@ private Progress execute(Script script, ExecutionMode mode, Map<String, String>
}

@Override
public Progress process(Script script, ExecutionMode mode, Map<String, String> customDefinitions,
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, customDefinitions, resolver, executor);
progress = execute(script, mode, resolver, executor);
} catch (ExecutionException e) {
progress = new ProgressImpl(executor);
progress.addEntry(Status.ERROR, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> customDefinitions, String executor);
String process(Script script, ExecutionMode executionMode, String executor);

ExecutionStatus checkStatus(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,14 +56,13 @@ public class AsyncScriptExecutorImpl implements AsyncScriptExecutor {
private JobResultsCache jobResultsCache;

@Override
public String process(Script script, ExecutionMode executionMode, Map<String, String> customDefinitions, String executor) {
public String process(Script script, ExecutionMode executionMode, String executor) {
String id = UUID.randomUUID().toString();
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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() {
Expand Down
Loading