From af1da334965f8379434c4f0cd4c3a03c7b6dc24e Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Thu, 19 Oct 2023 00:18:52 +0200 Subject: [PATCH 1/5] fixed handling of ifModified property --- .../apm/core/scripts/ScriptModel.java | 27 ++++ .../install/launchers/ApmInstallService.java | 123 +++++++++++++----- 2 files changed, 120 insertions(+), 30 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptModel.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptModel.java index 558a5c8d..494988cb 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptModel.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptModel.java @@ -31,10 +31,12 @@ import com.day.cq.commons.jcr.JcrConstants; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; import javax.annotation.PostConstruct; @@ -260,4 +262,29 @@ public static boolean isScript(Resource resource) { private static List getArrayProperty(Resource resource, String name) { return Lists.newArrayList(resource.getValueMap().get(name, new String[]{})); } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof ScriptModel) { + ScriptModel that = (ScriptModel) obj; + return Objects.equals(path, that.path) + && Objects.equals(launchEnabled, that.launchEnabled) + && Objects.equals(launchMode, that.launchMode) + && Objects.equals(launchEnvironment, that.launchEnvironment) + && Arrays.equals(launchRunModes, that.launchRunModes) + && Objects.equals(launchHook, that.launchHook) + && Objects.equals(launchSchedule, that.launchSchedule) + && Objects.equals(checksum, that.checksum) + && Objects.equals(verified, that.verified); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(path); + } } diff --git a/app/aem/install/src/main/java/com/cognifide/apm/install/launchers/ApmInstallService.java b/app/aem/install/src/main/java/com/cognifide/apm/install/launchers/ApmInstallService.java index d0f9e948..f619f60b 100644 --- a/app/aem/install/src/main/java/com/cognifide/apm/install/launchers/ApmInstallService.java +++ b/app/aem/install/src/main/java/com/cognifide/apm/install/launchers/ApmInstallService.java @@ -34,21 +34,30 @@ import com.cognifide.apm.core.utils.sling.SlingHelper; import java.lang.management.ManagementFactory; import java.util.Arrays; +import java.util.Collections; +import java.util.Dictionary; +import java.util.HashSet; +import java.util.Hashtable; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.observation.ResourceChange; +import org.apache.sling.api.resource.observation.ResourceChangeListener; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; import org.osgi.service.metatype.annotations.ObjectClassDefinition; @Component( - service = {ApmInstallService.class, Runnable.class}, immediate = true, property = { Property.DESCRIPTION + "APM Launches configured scripts", @@ -75,46 +84,77 @@ public class ApmInstallService extends AbstractLauncher implements Runnable { @Reference private History history; - private Configuration config; + private List scriptPaths; + + private boolean ifModified; + + private Set> registrations; @Activate - public void activate(Configuration config) { - this.config = config; - process(); + public void activate(Configuration config, BundleContext bundleContext) { + scriptPaths = Arrays.asList(config.scriptPaths()); + ifModified = config.ifModified(); + process(scriptPaths); + if (ifModified) { + registerScripts(bundleContext); + } + } + + @Deactivate + public void deactivate() { + registrations.forEach(ServiceRegistration::unregister); + registrations.clear(); } @Override public void run() { - process(); + process(scriptPaths); } - private void process() { - SlingHelper.operateTraced(resolverProvider, resolver -> { - boolean compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(resolver); - String instanceName = ManagementFactory.getRuntimeMXBean().getName(); - if (!compositeNodeStore || StringUtils.contains(instanceName, AEM_MUTABLE_CONTENT_INSTANCE)) { - processScripts(config, resolver); - } - }); + private void process(List scriptPaths) { + SlingHelper.operateTraced(resolverProvider, resolver -> process(scriptPaths, resolver)); } - private void processScripts(Configuration config, ResourceResolver resolver) throws PersistenceException { + private void process(List scriptPaths, ResourceResolver resolver) throws PersistenceException { + boolean compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(resolver); + String instanceName = ManagementFactory.getRuntimeMXBean().getName(); + if (!compositeNodeStore || StringUtils.contains(instanceName, AEM_MUTABLE_CONTENT_INSTANCE)) { + List