From de543af3e3538c4ce9cfca6daf50431cee3a9edc Mon Sep 17 00:00:00 2001 From: Jasper Simon <38686610+jasper-s@users.noreply.github.com> Date: Wed, 14 Aug 2024 15:52:53 +0200 Subject: [PATCH] Creations or updates of EnsureOakIndex configurations get picked up (#3402) * Creations or updates of EnsureOakIndex configurations get picked up * Update EnsureOakIndex OSGi annotations * Update EnsureOakIndexManagerImpl to OSGi annotations --------- Co-authored-by: Jasper Simon Co-authored-by: david g. --- CHANGELOG.md | 4 + .../acs/commons/oak/impl/EnsureOakIndex.java | 130 +++++++++--------- .../oak/impl/EnsureOakIndexManagerImpl.java | 90 ++++++------ 3 files changed, 113 insertions(+), 111 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b648f5c80..82ac959ab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com) - #3390 - Remove usage of commons collections 3 +### Fixed + +- #3402 - EnsureOakIndexManagerImpl does not pick up changes in EnsureOakIndex configurations. + ## 6.6.2 - 2024-06-25 ### Fixed diff --git a/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndex.java b/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndex.java index 5c2be21b1f..cd4d227ac2 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndex.java +++ b/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndex.java @@ -17,81 +17,85 @@ */ package com.adobe.acs.commons.oak.impl; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CopyOnWriteArrayList; - -import javax.jcr.RepositoryException; - +import com.adobe.acs.commons.analysis.jcrchecksum.ChecksumGenerator; +import com.adobe.acs.commons.oak.EnsureOakIndexManager; import org.apache.commons.lang3.StringUtils; -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.ConfigurationPolicy; -import org.apache.felix.scr.annotations.Properties; -import org.apache.felix.scr.annotations.Property; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.resource.ResourceResolverFactory; -import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.commons.scheduler.ScheduleOptions; import org.apache.sling.commons.scheduler.Scheduler; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.osgi.service.component.annotations.ReferencePolicyOption; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.adobe.acs.commons.analysis.jcrchecksum.ChecksumGenerator; -import com.adobe.acs.commons.oak.EnsureOakIndexManager; +import javax.jcr.RepositoryException; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; //@formatter:off @Component( - label = "ACS AEM Commons - Ensure Oak Index", - description = "Component Factory to manage Oak indexes.", - configurationFactory = true, - policy = ConfigurationPolicy.REQUIRE, - metatype = true + configurationPolicy = ConfigurationPolicy.REQUIRE ) -@Properties({ - @Property( - name = "webconsole.configurationFactory.nameHint", - value = "Definitions: {ensure-definitions.path}, Indexes: {oak-indexes.path}", - propertyPrivate = true - ) -}) -@Service +@Designate(ocd = EnsureOakIndex.Config.class, factory = true) //@formatter:on public class EnsureOakIndex implements AppliableEnsureOakIndex { static final Logger log = LoggerFactory.getLogger(EnsureOakIndex.class); //@formatter:off private static final String DEFAULT_ENSURE_DEFINITIONS_PATH = StringUtils.EMPTY; - @Property(label = "Ensure Definitions Path", - description = "The absolute path to the resource containing the " - + "ACS AEM Commons ensure definitions", - value = DEFAULT_ENSURE_DEFINITIONS_PATH) - public static final String PROP_ENSURE_DEFINITIONS_PATH = "ensure-definitions.path"; - - private static final String DEFAULT_OAK_INDEXES_PATH = "/oak:index"; - @Property(label = "Oak Indexes Path", - description = "The absolute path to the oak:index to update; Defaults to [ /oak:index ]", - value = DEFAULT_OAK_INDEXES_PATH) - public static final String PROP_OAK_INDEXES_PATH = "oak-indexes.path"; - private static final boolean DEFAULT_IMMEDIATE = true; - @Property( - label = "Immediate", - description = "Apply the indexes on startup of service. Defaults to [ true ]", - boolValue = DEFAULT_IMMEDIATE - ) + public static final String PROP_ENSURE_DEFINITIONS_PATH = "ensure-definitions.path"; + public static final String PROP_OAK_INDEXES_PATH = "oak-indexes.path"; public static final String PROP_IMMEDIATE = "immediate"; - - private static final String[] DEFAULT_ADDITIONAL_IGNORE_PROPERTIES = new String[]{}; - @Property(label = "Additional ignore properties", - description = "Property names that are to be ignored when determining if an oak index has changed, as well as what properties should be removed/updated.", - cardinality = Integer.MAX_VALUE, - value = {}) public static final String PROP_ADDITIONAL_IGNORE_PROPERTIES = "properties.ignore"; + @ObjectClassDefinition( + name = "ACS AEM Commons - Ensure Oak Index", + description = "Component Factory to manage Oak indexes." + ) + @interface Config { + + @AttributeDefinition( + name = "Ensure Definitions Path", + description = "The absolute path to the resource containing the " + + "ACS AEM Commons ensure definitions" + ) + String ensure$_$definitions_path() default DEFAULT_ENSURE_DEFINITIONS_PATH; + + @AttributeDefinition( + name = "Oak Indexes Path", + description = "The absolute path to the oak:index to update; Defaults to [ /oak:index ]" + ) + String oak$_$indexes_path() default DEFAULT_OAK_INDEXES_PATH; + + @AttributeDefinition( + name = "Immediate", + description = "Apply the indexes on startup of service. Defaults to [ true ]" + ) + boolean immediate() default DEFAULT_IMMEDIATE; + + @AttributeDefinition( + name = "Additional ignore properties", + description = "Property names that are to be ignored when determining if an oak index has changed, " + + "as well as what properties should be removed/updated.", + cardinality = Integer.MAX_VALUE + ) + String[] properties_ignore() default {}; + + String webconsole_configurationFactory_nameHint() default "Definitions: {ensure-definitions.path}, Indexes: {oak-indexes.path}"; + + } + @Reference private ChecksumGenerator checksumGenerator; @@ -101,8 +105,12 @@ public class EnsureOakIndex implements AppliableEnsureOakIndex { @Reference private Scheduler scheduler; - @Reference - private EnsureOakIndexManager indexManager; + @Reference( + cardinality = ReferenceCardinality.OPTIONAL, + policy = ReferencePolicy.DYNAMIC, + policyOption = ReferencePolicyOption.GREEDY + ) + private volatile EnsureOakIndexManager indexManager; private String ensureDefinitionsPath; private String oakIndexesPath; @@ -112,13 +120,11 @@ public class EnsureOakIndex implements AppliableEnsureOakIndex { //@formatter:on @Activate - protected final void activate(Map config) throws RepositoryException { + protected final void activate(Config config) throws RepositoryException { - ensureDefinitionsPath = PropertiesUtil.toString(config.get(PROP_ENSURE_DEFINITIONS_PATH), - DEFAULT_ENSURE_DEFINITIONS_PATH); + ensureDefinitionsPath = config.ensure$_$definitions_path(); - oakIndexesPath = PropertiesUtil.toString(config.get(PROP_OAK_INDEXES_PATH), - DEFAULT_OAK_INDEXES_PATH); + oakIndexesPath = config.oak$_$indexes_path(); if (StringUtils.isBlank(ensureDefinitionsPath)) { throw new IllegalArgumentException("OSGi Configuration Property `" @@ -128,9 +134,9 @@ protected final void activate(Map config) throws RepositoryExcep + PROP_OAK_INDEXES_PATH + "` " + "cannot be blank."); } - this.immediate = PropertiesUtil.toBoolean(config.get(PROP_IMMEDIATE), DEFAULT_IMMEDIATE); + this.immediate = config.immediate(); - String[] ignoredProps = PropertiesUtil.toStringArray(config.get(PROP_ADDITIONAL_IGNORE_PROPERTIES), DEFAULT_ADDITIONAL_IGNORE_PROPERTIES); + String[] ignoredProps = config.properties_ignore(); String[] indexManagerIgnoredProps = getIndexManagerConfiguredIgnoreProperties(); if (ignoredProps.length == 0) { diff --git a/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndexManagerImpl.java b/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndexManagerImpl.java index 0cff72a833..e22225dfe6 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndexManagerImpl.java +++ b/bundle/src/main/java/com/adobe/acs/commons/oak/impl/EnsureOakIndexManagerImpl.java @@ -21,15 +21,16 @@ import com.adobe.acs.commons.util.RequireAem; import com.adobe.granite.jmx.annotation.AnnotatedStandardMBean; import org.apache.commons.lang3.StringUtils; -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Properties; -import org.apache.felix.scr.annotations.Property; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.apache.felix.scr.annotations.ReferencePolicy; -import org.apache.felix.scr.annotations.Service; -import org.apache.sling.commons.osgi.PropertiesUtil; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.FieldOption; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.osgi.service.component.annotations.ReferencePolicyOption; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,39 +59,16 @@ //@formatter:off @Component( - label = "ACS AEM Commons - Ensure Oak Index Manager", - description = "Manage for ensuring oak indexes.", + service = {DynamicMBean.class, EnsureOakIndexManager.class}, immediate = true, - metatype = true + property = { + "felix.webconsole.title=Ensure Oak Index", + "felix.webconsole.label=ensureOakIndex", + "felix.webconsole.category=Sling", + "jmx.objectname=com.adobe.acs.commons.oak:type=Ensure Oak Index" + } ) -@Properties({ - @Property( - name = "webconsole.configurationFactory.nameHint", - value = "Additional Ignore properties: {properties.ignore}", - propertyPrivate = true - ), - @Property( - name = "felix.webconsole.title", - value = "Ensure Oak Index", - propertyPrivate = true - ), - @Property( - name = "felix.webconsole.label", - value = "ensureOakIndex", - propertyPrivate = true - ), - @Property( - name = "felix.webconsole.category", - value = "Sling", - propertyPrivate = true - ), - @Property( - name = "jmx.objectname", - value = "com.adobe.acs.commons.oak:type=Ensure Oak Index", - propertyPrivate = true - ) -}) -@Service(value = {DynamicMBean.class, EnsureOakIndexManager.class}) +@Designate(ocd = EnsureOakIndexManagerImpl.Config.class) //@formatter:on public class EnsureOakIndexManagerImpl extends AnnotatedStandardMBean implements EnsureOakIndexManager, EnsureOakIndexManagerMBean { private static final Logger log = LoggerFactory.getLogger(EnsureOakIndexManagerImpl.class); @@ -98,21 +76,35 @@ public class EnsureOakIndexManagerImpl extends AnnotatedStandardMBean implements //@formatter:off private static final String[] DEFAULT_ADDITIONAL_IGNORE_PROPERTIES = new String[]{}; private String[] additionalIgnoreProperties = DEFAULT_ADDITIONAL_IGNORE_PROPERTIES; - @Property(label = "Additional ignore properties", - description = "Property names that are to be ignored when determining if an oak index has changed, as well as what properties should be removed/updated.", - cardinality = Integer.MAX_VALUE, - value = {}) public static final String PROP_ADDITIONAL_IGNORE_PROPERTIES = "properties.ignore"; + @ObjectClassDefinition( + name = "ACS AEM Commons - Ensure Oak Index Manager", + description = "Manage for ensuring oak indexes." + ) + @interface Config { + + @AttributeDefinition( + name = "Additional ignore properties", + description = "Property names that are to be ignored when determining if an oak index has changed, " + + "as well as what properties should be removed/updated.", + cardinality = Integer.MAX_VALUE + ) + String[] properties_ignore() default {}; + + String webconsole_configurationFactory_nameHint() default "Additional Ignore properties: {properties.ignore}"; + + } // Disable this feature on AEM as a Cloud Service @Reference(target="(distribution=classic)") RequireAem requireAem; @Reference( - cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, - referenceInterface = AppliableEnsureOakIndex.class, - policy = ReferencePolicy.DYNAMIC + cardinality = ReferenceCardinality.MULTIPLE, + policyOption = ReferencePolicyOption.GREEDY, + policy = ReferencePolicy.DYNAMIC, + fieldOption = FieldOption.UPDATE ) // Thread-safe ArrayList to track EnsureIndex service registrations private CopyOnWriteArrayList ensureIndexes = @@ -216,8 +208,8 @@ public final TabularData getEnsureOakIndexes() throws OpenDataException { @Activate - protected void activate(Map config) { - additionalIgnoreProperties = PropertiesUtil.toStringArray(config.get(PROP_ADDITIONAL_IGNORE_PROPERTIES), DEFAULT_ADDITIONAL_IGNORE_PROPERTIES); + protected void activate(Config config) { + additionalIgnoreProperties = config.properties_ignore(); }