Search in sources :

Example 6 with Progress

use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.

the class ScriptManagerImpl method evaluate.

@Override
public Progress evaluate(String scriptContent, Mode mode, Map<String, String> customDefinitions, ResourceResolver resolver) throws RepositoryException, PersistenceException {
    Script script = scriptFinder.find(ScriptManager.FILE_FOR_EVALUATION, false, resolver);
    if (script != null) {
        scriptStorage.remove(script, resolver);
    }
    InputStream stream = new ByteArrayInputStream(scriptContent.getBytes(StandardCharsets.UTF_8));
    script = scriptStorage.save(FILE_FOR_EVALUATION, stream, true, resolver);
    Progress progress = process(script, mode, customDefinitions, resolver);
    scriptStorage.remove(script, resolver);
    return progress;
}
Also used : ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) Script(com.cognifide.cq.cqsm.api.scripts.Script) Progress(com.cognifide.cq.cqsm.api.logger.Progress) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 7 with Progress

use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.

the class ScriptRunServlet method doPost.

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
    ResourceResolver resolver = request.getResourceResolver();
    final String searchPath = request.getParameter("file");
    final String modeName = request.getParameter("mode");
    if (StringUtils.isEmpty(searchPath)) {
        ServletUtils.writeMessage(response, "error", "Please set the script file name: -d \"file=[name]\"");
        return;
    }
    if (StringUtils.isEmpty(modeName)) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        ServletUtils.writeMessage(response, "error", "Running mode not specified.");
        return;
    }
    final Script script = scriptFinder.find(searchPath, resolver);
    if (script == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        ServletUtils.writeMessage(response, "error", String.format("Script not found: %s", searchPath));
        return;
    }
    try {
        final Mode mode = Mode.fromString(modeName, Mode.DRY_RUN);
        final Progress progressLogger = scriptManager.process(script, mode, resolver);
        if (progressLogger.isSuccess()) {
            ServletUtils.writeJson(response, ProgressHelper.toJson(progressLogger.getEntries()));
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            ServletUtils.writeJson(response, ProgressHelper.toJson(progressLogger.getLastError()));
        }
    } catch (RepositoryException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ServletUtils.writeMessage(response, "error", String.format("Script cannot be executed because of" + " repository error: %s", e.getMessage()));
    }
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) Progress(com.cognifide.cq.cqsm.api.logger.Progress) Mode(com.cognifide.cq.cqsm.api.executors.Mode) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) RepositoryException(javax.jcr.RepositoryException)

Example 8 with Progress

use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.

the class ScriptValidationServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final String content = request.getParameter("content");
    if (StringUtils.isEmpty(content)) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        ServletUtils.writeMessage(response, "error", "Script content is required");
        return;
    }
    try {
        final Progress progress = scriptManager.evaluate(content, Mode.VALIDATION, request.getResourceResolver());
        if (progress.isSuccess()) {
            ServletUtils.writeMessage(response, "success", "Script passes validation");
        } else {
            final String message = progress.getLastError().getLastMessageText();
            final Map<String, Object> context = new HashMap<>();
            if (message != null) {
                context.put("error", message);
            }
            ServletUtils.writeMessage(response, "error", "Script does not pass validation", context);
        }
    } catch (RepositoryException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ServletUtils.writeMessage(response, "error", String.format("Script' cannot be validated because of " + "repository error: %s", e.getMessage()));
    }
}
Also used : Progress(com.cognifide.cq.cqsm.api.logger.Progress) HashMap(java.util.HashMap) RepositoryException(javax.jcr.RepositoryException)

Example 9 with Progress

use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.

the class StartupExecutor method runScript.

private void runScript(ResourceResolver resolver, Script script) throws PersistenceException {
    final String scriptPath = script.getPath();
    try {
        scriptManager.process(script, Mode.VALIDATION, resolver);
        if (script.isValid()) {
            final Progress progress = scriptManager.process(script, Mode.AUTOMATIC_RUN, resolver);
            logStatus(scriptPath, progress.isSuccess());
        } else {
            LOG.warn("Startup executor cannot execute script which is not valid: {}", scriptPath);
        }
    } catch (RepositoryException e) {
        LOG.error("Script cannot be processed because of repository error: {}", e);
    }
}
Also used : Progress(com.cognifide.cq.cqsm.api.logger.Progress) RepositoryException(javax.jcr.RepositoryException)

Example 10 with Progress

use of com.cognifide.cq.cqsm.api.logger.Progress in project APM by Cognifide.

the class ScriptRunnerJobConsumer method process.

@Override
public JobResult process(final Job job) {
    LOG.info("Script runner job consumer started");
    final String id = job.getId();
    final Mode mode = getMode(job);
    final String userId = getUserId(job);
    return SlingHelper.resolveDefault(resolverFactory, userId, new ResolveCallback<JobResult>() {

        @Override
        public JobResult resolve(ResourceResolver resolver) {
            JobResult result = JobResult.FAILED;
            final Script script = getScript(job, resolver);
            if (script != null && mode != null) {
                try {
                    final Progress progressLogger = scriptManager.process(script, mode, resolver);
                    jobResultsCache.put(id, progressLogger);
                    result = JobResult.OK;
                } catch (RepositoryException | PersistenceException e) {
                    LOG.error("Script manager failed to process script", e);
                    result = JobResult.FAILED;
                }
            }
            return result;
        }
    }, JobResult.FAILED);
}
Also used : Script(com.cognifide.cq.cqsm.api.scripts.Script) Progress(com.cognifide.cq.cqsm.api.logger.Progress) Mode(com.cognifide.cq.cqsm.api.executors.Mode) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Aggregations

Progress (com.cognifide.cq.cqsm.api.logger.Progress)11 RepositoryException (javax.jcr.RepositoryException)6 Script (com.cognifide.cq.cqsm.api.scripts.Script)4 Mode (com.cognifide.cq.cqsm.api.executors.Mode)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ExecutionException (com.cognifide.cq.cqsm.api.exceptions.ExecutionException)2 ProgressImpl (com.cognifide.cq.cqsm.core.progress.ProgressImpl)2 ActionDescriptor (com.cognifide.cq.cqsm.api.actions.ActionDescriptor)1 ActionResult (com.cognifide.cq.cqsm.api.actions.ActionResult)1 Context (com.cognifide.cq.cqsm.api.executors.Context)1 InstanceDetails (com.cognifide.cq.cqsm.api.history.InstanceDetails)1 ModifiableScript (com.cognifide.cq.cqsm.api.scripts.ModifiableScript)1 ActionExecutor (com.cognifide.cq.cqsm.core.actions.executor.ActionExecutor)1 ScriptImpl (com.cognifide.cq.cqsm.core.scripts.ScriptImpl)1 SessionSavingPolicy (com.cognifide.cq.cqsm.core.sessions.SessionSavingPolicy)1 OperateCallback (com.cognifide.cq.cqsm.core.utils.sling.OperateCallback)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1