This is an automated email from the ASF dual-hosted git repository. reschke pushed a commit to branch SLING-12759 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
commit a49b9ce64bcdf741a18cc8d71b2a1ffa6f03acc6 Author: Julian Reschke <[email protected]> AuthorDate: Wed Apr 23 17:05:09 2025 +0100 SLING-12759: push 'non-optimized' alias handling into AliasHandler --- .../impl/ResourceResolverImpl.java | 65 ++++++---------------- .../impl/mapping/AliasHandler.java | 43 ++++++++++++-- 2 files changed, 57 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java index 16806623..8d0fb260 100644 --- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java +++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java @@ -927,55 +927,26 @@ public class ResourceResolverImpl extends SlingAdaptable implements ResourceReso // we do not have a child with the exact name, so we look for // a child, whose alias matches the childName - if (factory.getMapEntries().isOptimizeAliasResolutionEnabled()) { - final String parentPath = parent.getPath(); - logger.debug( - "getChildInternal: Optimize Alias Resolution is Enabled, looking up {} in {}", - childName, - parentPath); - - // optimized alias resolution: aliases are cached by MapEntries - final Optional<String> aliasedResourceName = - factory.getMapEntries().getAliasMap(parentPath).entrySet().stream() - .filter(e -> e.getValue().contains(childName)) - .findFirst() - .map(Map.Entry::getKey); - if (aliasedResourceName.isPresent()) { - // we know that MapEntries already has checked for valid aliases - final String aliasPath = parent.getPath() + '/' + aliasedResourceName.get(); - final Resource aliasedChild = - getAbsoluteResourceInternal(parent, ResourceUtil.normalize(aliasPath), EMPTY_PARAMETERS, true); - logger.debug("getChildInternal: Found Resource {} with alias {} to use", aliasedChild, childName); - return aliasedChild; - } + final String parentPath = parent.getPath(); + logger.debug("getChildInternal: looking up {} in {}", childName, parentPath); + + final Optional<String> aliasedResourceName = factory.getMapEntries().getAliasMap(parentPath).entrySet().stream() + .filter(e -> e.getValue().contains(childName)) + .findFirst() + .map(Map.Entry::getKey); + + if (aliasedResourceName.isPresent()) { + // we know that MapEntries already has checked for valid aliases + final String aliasPath = parent.getPath() + '/' + aliasedResourceName.get(); + final Resource aliasedChild = + getAbsoluteResourceInternal(parent, ResourceUtil.normalize(aliasPath), EMPTY_PARAMETERS, true); + logger.debug("getChildInternal: Found Resource {} with alias {} to use", aliasedChild, childName); + return aliasedChild; } else { - if (this.factory.isOptimizeAliasResolutionEnabled()) { - this.factory.getMapEntries().logDisableAliasOptimization(); - } - logger.debug("getChildInternal: Optimize Alias Resolution is Disabled"); - final Iterator<Resource> children = listChildren(parent); - while (children.hasNext()) { - child = children.next(); - if (!child.getPath().endsWith(JCR_CONTENT_LEAF)) { - final String[] aliases = ResourceResolverControl.getProperty(child, PROP_ALIAS, String[].class); - if (aliases != null) { - for (final String alias : aliases) { - if (childName.equals(alias)) { - logger.debug( - "getChildInternal: Found Resource {} with alias {} to use", child, childName); - final Resource aliasedChild = getAbsoluteResourceInternal( - parent, ResourceUtil.normalize(child.getPath()), EMPTY_PARAMETERS, true); - return aliasedChild; - } - } - } - } - } + // no match for the childName found + logger.debug("getChildInternal: Resource {} has no child {}", parent, childName); + return null; } - - // no match for the childName found - logger.debug("getChildInternal: Resource {} has no child {}", parent, childName); - return null; } /** diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java index 6451d439..7dae9e74 100644 --- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java +++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java @@ -21,6 +21,7 @@ package org.apache.sling.resourceresolver.impl.mapping; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -54,6 +55,8 @@ class AliasHandler { private static final String JCR_CONTENT_SUFFIX = "/" + JCR_CONTENT; + private static final String SERVICE_USER = "mapping"; + private MapConfigurationProvider factory; private final ReentrantLock initializing; @@ -67,10 +70,13 @@ class AliasHandler { private final Runnable sendChangeEvent; /** - * The key of the map is the parent path, while the value is a map with the resource name as key and the actual aliases as values + * The key of the map is the parent path, while the value is a map with the + * resource name as key and the actual aliases as values */ Map<String, Map<String, Collection<String>>> aliasMapsMap; + boolean mapIsInitialized = false; + final AtomicLong aliasResourcesOnStartup; final AtomicLong detectedConflictingAliases; final AtomicLong detectedInvalidAliases; @@ -130,13 +136,17 @@ class AliasHandler { } else if (!conflictingAliases.isEmpty()) { log.warn("There are {} conflicting aliases: {}", conflictingAliases.size(), conflictingAliases); } + if (invalidAliases.size() >= MAX_REPORT_DEFUNCT_ALIASES) { log.warn("There are {} invalid aliases; excerpt: {}", invalidAliases.size(), invalidAliases); } else if (!invalidAliases.isEmpty()) { log.warn("There are {} invalid aliases: {}", invalidAliases.size(), invalidAliases); } + + mapIsInitialized = true; } catch (final Exception e) { + this.aliasMapsMap = new ConcurrentHashMap<>(); logDisableAliasOptimization(e); // disable optimize alias resolution @@ -286,8 +296,33 @@ class AliasHandler { } public @NotNull Map<String, Collection<String>> getAliasMap(final String parentPath) { - Map<String, Collection<String>> aliasMapForParent = aliasMapsMap.get(parentPath); - return aliasMapForParent != null ? aliasMapForParent : Collections.emptyMap(); + if (mapIsInitialized) { + Map<String, Collection<String>> aliasMapForParent = aliasMapsMap.get(parentPath); + return aliasMapForParent != null ? aliasMapForParent : Collections.emptyMap(); + } else { + return getAliasMapFromRepo(parentPath); + } + } + + private @NotNull Map<String, Collection<String>> getAliasMapFromRepo(final String parentPath) { + try (final ResourceResolver resolver = + factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo(SERVICE_USER))) { + + Resource parent = resolver.getResource(parentPath); + if (parent != null) { + Map<String, Map<String, Collection<String>>> localMap = new HashMap<>(); + for (Resource child : parent.getChildren()) { + loadAlias(child, localMap, new ArrayList<>(), new ArrayList<>()); + } + Map<String, Collection<String>> aliasMapForParent = localMap.get(parentPath); + return aliasMapForParent != null ? aliasMapForParent : Collections.emptyMap(); + } else { + return Collections.emptyMap(); + } + } catch (LoginException ex) { + log.error("Could not obtain resolver", ex); + return Collections.emptyMap(); + } } /** @@ -300,7 +335,7 @@ class AliasHandler { final Map<String, Map<String, Collection<String>>> map = new ConcurrentHashMap<>(); try (final ResourceResolver resolver = - factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"))) { + factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo(SERVICE_USER))) { final String baseQueryString = generateAliasQuery(); Iterator<Resource> it;
