From 595507aa2320be206c85a3069c91e70060c9b162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl?= <14930982+geertsmichael@users.noreply.github.com> Date: Sat, 29 Jun 2019 02:42:53 +0200 Subject: [PATCH] Expose OnDeployExecutor Interface and store exception message on result node (#1967) * Expose OnDeployExecutor Interface and store exception message on result node --- CHANGELOG.md | 6 ++-- .../commons/ondeploy/OnDeployExecutor.java | 36 +++++++++++++++++++ .../ondeploy/OnDeployScriptProvider.java | 2 +- .../ondeploy/impl/OnDeployExecutorImpl.java | 21 ++++++----- ...ecutor.java => OnDeployExecutorMBean.java} | 2 +- .../acs/commons/ondeploy/package-info.java | 2 +- .../ondeploy/scripts/OnDeployScript.java | 2 +- .../ondeploy/scripts/package-info.java | 2 +- .../impl/OnDeployExecutorImplTest.java | 9 ++++- 9 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployExecutor.java rename bundle/src/main/java/com/adobe/acs/commons/ondeploy/impl/{OnDeployExecutor.java => OnDeployExecutorMBean.java} (97%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 147d38a981..cef1f6411b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com) ## Unreleased ### Added -- 1927 - HTTP cache: added cookie exclusion -- 1905 - HTTP cache: added response header exclusion +- #1967 - Expose On-Deploy Script Executor for external script triggering +- #1967 - Write On-Deploy Script exception statement to the result node +- #1927 - HTTP cache: added cookie exclusion +- #1905 - HTTP cache: added response header exclusion ### Changed - #1945 - Added support for jcr:content creation and update to the Data Importer diff --git a/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployExecutor.java b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployExecutor.java new file mode 100644 index 0000000000..8087a0f111 --- /dev/null +++ b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployExecutor.java @@ -0,0 +1,36 @@ +/* + * #%L + * ACS AEM Commons Bundle + * %% + * Copyright (C) 2016 Adobe + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.adobe.acs.commons.ondeploy; + +/** + * OnDeploy Provider to execute scripts. + */ +public interface OnDeployExecutor { + + /** + * Execute scripts + * + * @param scriptName className of the script + * @param force true to run if the script ran already. + * @return true if executed, false if failed. + */ + boolean executeScript(String scriptName, boolean force); + +} diff --git a/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployScriptProvider.java b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployScriptProvider.java index fb77dc5443..a4f6e39bca 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployScriptProvider.java +++ b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/OnDeployScriptProvider.java @@ -25,7 +25,7 @@ import java.util.List; /** - * Provider interface to supply a list of scripts to be run by the OnDeployExecutor. + * Provider interface to supply a list of scripts to be run by the OnDeployExecutorMBean. */ @ConsumerType public interface OnDeployScriptProvider { diff --git a/bundle/src/main/java/com/adobe/acs/commons/ondeploy/impl/OnDeployExecutorImpl.java b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/impl/OnDeployExecutorImpl.java index 6afe670a09..014a36cdca 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/ondeploy/impl/OnDeployExecutorImpl.java +++ b/bundle/src/main/java/com/adobe/acs/commons/ondeploy/impl/OnDeployExecutorImpl.java @@ -19,10 +19,12 @@ */ package com.adobe.acs.commons.ondeploy.impl; +import com.adobe.acs.commons.ondeploy.OnDeployExecutor; import com.adobe.acs.commons.ondeploy.OnDeployScriptProvider; import com.adobe.acs.commons.ondeploy.scripts.OnDeployScript; import com.adobe.granite.jmx.annotation.AnnotatedStandardMBean; import com.day.cq.commons.jcr.JcrConstants; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; import org.apache.felix.scr.annotations.Properties; @@ -50,12 +52,12 @@ import javax.management.openmbean.SimpleType; import javax.management.openmbean.TabularDataSupport; import javax.management.openmbean.TabularType; -import java.util.Calendar; import java.util.Collections; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Date; +import java.util.Calendar; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; @@ -80,12 +82,13 @@ metatype = false, policy = ConfigurationPolicy.REQUIRE) @Properties({ @Property(label = "MBean Name", name = "jmx.objectname", value = "com.adobe.acs.commons:type=On-Deploy Scripts", propertyPrivate = true) }) -@Service(value = DynamicMBean.class) -public class OnDeployExecutorImpl extends AnnotatedStandardMBean implements OnDeployExecutor { +@Service(value = {DynamicMBean.class, OnDeployExecutorMBean.class, OnDeployExecutor.class}) +public class OnDeployExecutorImpl extends AnnotatedStandardMBean implements OnDeployExecutorMBean, OnDeployExecutor { static final String SCRIPT_STATUS_JCR_FOLDER = "/var/acs-commons/on-deploy-scripts-status"; private static final String SCRIPT_DATE_END = "endDate"; private static final String SCRIPT_DATE_START = "startDate"; + private static final String SCRIPT_OUTPUT = "output"; private static final String SCRIPT_STATUS = "status"; private static final String SCRIPT_STATUS_FAIL = "fail"; private static final String SCRIPT_STATUS_RUNNING = "running"; @@ -123,7 +126,7 @@ public class OnDeployExecutorImpl extends AnnotatedStandardMBean implements OnDe } public OnDeployExecutorImpl() throws NotCompliantMBeanException { - super(OnDeployExecutor.class); + super(OnDeployExecutorMBean.class); } protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) { @@ -197,12 +200,12 @@ protected boolean runScript(ResourceResolver resourceResolver, OnDeployScript sc try { script.execute(resourceResolver); logger.info("On-deploy script completed successfully: {}", statusResource.getPath()); - trackScriptEnd(statusResource, SCRIPT_STATUS_SUCCESS); + trackScriptEnd(statusResource, SCRIPT_STATUS_SUCCESS, ""); return true; } catch (Exception e) { String errMsg = "On-deploy script failed: " + statusResource.getPath(); logger.error(errMsg, e); - trackScriptEnd(statusResource, SCRIPT_STATUS_FAIL); + trackScriptEnd(statusResource, SCRIPT_STATUS_FAIL, ExceptionUtils.getStackTrace(e.getCause())); throw new OnDeployEarlyTerminationException(new RuntimeException(errMsg)); } } else if (!status.equals(SCRIPT_STATUS_SUCCESS)) { @@ -226,11 +229,12 @@ protected void runScripts(ResourceResolver resourceResolver, List