This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 81a4e883 SLING-12696: alias refactoring - move alias related code into 
single place (#157)
81a4e883 is described below

commit 81a4e88356f6fcfdd0e6a15ba0936bb9d5081f04
Author: Julian Reschke <[email protected]>
AuthorDate: Thu Mar 6 18:26:39 2025 +0100

    SLING-12696: alias refactoring - move alias related code into single place 
(#157)
---
 .../resourceresolver/impl/mapping/MapEntries.java  | 726 +++++++++++----------
 1 file changed, 364 insertions(+), 362 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 5e5b59af..3be2e3ef 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -197,66 +197,6 @@ public class MapEntries implements
         return bundleContext.registerService(ResourceChangeListener.class, 
this, props);
     }
 
-    /**
-     * Actual initializer. Guards itself against concurrent use by using a
-     * ReentrantLock. Does nothing if the resource resolver has already been
-     * null-ed.
-     * @return true if the optimizedAliasResolution is enabled, false otherwise
-     */
-    protected boolean initializeAliases() {
-
-        this.initializing.lock();
-        try {
-            final ResourceResolver resolver = this.resolver;
-            final MapConfigurationProvider factory = this.factory;
-            if (resolver == null || factory == null) {
-                return this.factory.isOptimizeAliasResolutionEnabled();
-            }
-
-            List<String> conflictingAliases = new ArrayList<>();
-            List<String> invalidAliases = new ArrayList<>();
-
-            boolean isOptimizeAliasResolutionEnabled = 
this.factory.isOptimizeAliasResolutionEnabled();
-
-            //optimization made in SLING-2521
-            if (isOptimizeAliasResolutionEnabled) {
-                try {
-                    final Map<String, Map<String, Collection<String>>> 
loadedMap = this.loadAliases(resolver, conflictingAliases, invalidAliases);
-                    this.aliasMapsMap = loadedMap;
-
-                    // warn if there are more than a few defunct aliases
-                    if (conflictingAliases.size() >= 
MAX_REPORT_DEFUNCT_ALIASES) {
-                        log.warn("There are {} conflicting aliases; excerpt: 
{}",  conflictingAliases.size(), conflictingAliases);
-                    } 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);
-                    }
-                } catch (final Exception e) {
-
-                    logDisableAliasOptimization(e);
-
-                    // disable optimize alias resolution
-                    isOptimizeAliasResolutionEnabled = false;
-                }
-            }
-
-            doUpdateConfiguration();
-
-            sendChangeEvent();
-
-            return isOptimizeAliasResolutionEnabled;
-
-        } finally {
-
-            this.initializing.unlock();
-
-        }
-    }
-
     private boolean addResource(final String path, final AtomicBoolean 
resolverRefreshed) {
         this.initializing.lock();
 
@@ -335,72 +275,6 @@ public class MapEntries implements
         return changed;
     }
 
-    /**
-     * Remove all aliases for the content path
-     * @param contentPath The content path
-     * @param path Optional sub path of the vanity path
-     * @return {@code true} if a change happened
-     */
-    private boolean removeAlias(final String contentPath, final String path, 
final AtomicBoolean resolverRefreshed) {
-        // if path is specified we first need to find out if it is
-        // a direct child of vanity path but not jcr:content, or a jcr:content 
child of a direct child
-        // otherwise we can discard the event
-        boolean handle = true;
-        final String resourcePath;
-        if ( path != null  && path.length() > contentPath.length()) {
-            final String subPath = path.substring(contentPath.length() + 1);
-            final int firstSlash = subPath.indexOf('/');
-            if ( firstSlash == -1 ) {
-                if ( subPath.equals(JCR_CONTENT) ) {
-                    handle = false;
-                }
-                resourcePath = path;
-            } else if ( subPath.lastIndexOf('/') == firstSlash) {
-                if ( subPath.startsWith(JCR_CONTENT_PREFIX) || 
!subPath.endsWith(JCR_CONTENT_SUFFIX) ) {
-                    handle = false;
-                }
-                resourcePath = ResourceUtil.getParent(path);
-            } else {
-                handle = false;
-                resourcePath = null;
-            }
-        }
-        else {
-            resourcePath = contentPath;
-        }
-        if ( !handle ) {
-            return false;
-        }
-
-        this.initializing.lock();
-        try {
-            final Map<String, Collection<String>> aliasMapEntry = 
aliasMapsMap.get(contentPath);
-            if (aliasMapEntry != null) {
-                this.refreshResolverIfNecessary(resolverRefreshed);
-
-                String prefix = contentPath.endsWith("/") ? contentPath : 
contentPath + "/";
-                if (aliasMapEntry.entrySet().removeIf(e -> (prefix + 
e.getKey()).startsWith(resourcePath)) &&  (aliasMapEntry.isEmpty())) {
-                    this.aliasMapsMap.remove(contentPath);
-                }
-
-                Resource containingResource = this.resolver != null ? 
this.resolver.getResource(resourcePath) : null;
-
-                if (containingResource != null) {
-                    if 
(containingResource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) 
{
-                        doAddAlias(containingResource);
-                    }
-                    final Resource child = 
containingResource.getChild(JCR_CONTENT);
-                    if (child != null && 
child.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) {
-                        doAddAlias(child);
-                    }
-                }
-            }
-            return aliasMapEntry != null;
-        } finally {
-            this.initializing.unlock();
-        }
-    }
-
     /**
      * Update the configuration.
      * Does no locking and does not send an event at the end
@@ -419,51 +293,6 @@ public class MapEntries implements
         resolveMapsMap.put(GLOBAL_LIST_KEY, globalResolveMap);
         this.mapMaps = Collections.unmodifiableSet(new 
TreeSet<>(newMapMaps.values()));
     }
-
-    private boolean doAddAlias(final Resource resource) {
-        return loadAlias(resource, this.aliasMapsMap, null, null);
-    }
-
-    /**
-     * Update alias from a resource
-     * @param resource The resource
-     * @return {@code true} if any change
-     */
-    private boolean doUpdateAlias(final Resource resource) {
-
-        // resource containing the alias
-        final Resource containingResource = getResourceToBeAliased(resource);
-
-        if ( containingResource != null ) {
-            final String containingResourceName = containingResource.getName();
-            final String parentPath = 
ResourceUtil.getParent(containingResource.getPath());
-
-            final Map<String, Collection<String>> aliasMapEntry = parentPath 
== null ? null : aliasMapsMap.get(parentPath);
-            if (aliasMapEntry != null) {
-                aliasMapEntry.remove(containingResourceName);
-                if (aliasMapEntry.isEmpty()) {
-                    this.aliasMapsMap.remove(parentPath);
-                }
-            }
-
-            boolean changed = aliasMapEntry != null;
-
-            if ( 
containingResource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS) ) 
{
-                changed |= doAddAlias(containingResource);
-            }
-            final Resource child = containingResource.getChild(JCR_CONTENT);
-            if ( child != null && 
child.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS) ) {
-                changed |= doAddAlias(child);
-            }
-
-            return changed;
-        } else {
-            log.warn("containingResource is null for alias on {}, skipping.", 
resource.getPath());
-        }
-
-        return false;
-    }
-
     /**
      * Cleans up this class.
      */
@@ -550,17 +379,6 @@ public class MapEntries implements
         return this.useOptimizeAliasResolution;
     }
 
-    @Override
-    public @NotNull Map<String, Collection<String>> getAliasMap(final String 
parentPath) {
-        Map<String, Collection<String>> aliasMapForParent = 
aliasMapsMap.get(parentPath);
-        return aliasMapForParent != null ? aliasMapForParent : 
Collections.emptyMap();
-    }
-
-    @Override
-    public Map<String, List<String>> getVanityPathMappings() {
-        return vph.getVanityPathMappings();
-    }
-
     /**
      * Refresh the resource resolver if not already done
      * @param resolverRefreshed Boolean flag containing the state if the 
resolver
@@ -788,51 +606,372 @@ public class MapEntries implements
         }
     }
 
-    /**
-     * Load aliases - Search for all nodes (except under /jcr:system) below
-     * configured alias locations having the sling:alias property
-     */
-    private Map<String, Map<String, Collection<String>>> loadAliases(final 
ResourceResolver resolver,
-            List<String> conflictingAliases, List<String> invalidAliases) {
-
-        final Map<String, Map<String, Collection<String>>> map = new 
ConcurrentHashMap<>();
-        final String baseQueryString = generateAliasQuery();
-
-        Iterator<Resource> it;
-        try {
-            final String queryStringWithSort = baseQueryString + " AND 
FIRST([sling:alias]) >= '%s' ORDER BY FIRST([sling:alias])";
-            it = new PagedQueryIterator("alias", "sling:alias", resolver, 
queryStringWithSort, 2000);
-        } catch (QuerySyntaxException ex) {
-            log.debug("sort with first() not supported, falling back to base 
query", ex);
-            it = queryUnpaged("alias", baseQueryString);
-        } catch (UnsupportedOperationException ex) {
-            log.debug("query failed as unsupported, retrying without 
paging/sorting", ex);
-            it = queryUnpaged("alias", baseQueryString);
-        }
-
-        log.debug("alias initialization - start");
-        long count = 0;
-        long processStart = System.nanoTime();
-        while (it.hasNext()) {
-            count += 1;
-            loadAlias(it.next(), map, conflictingAliases, invalidAliases);
+    private void loadConfiguration(final MapConfigurationProvider factory, 
final List<MapEntry> entries) {
+        // virtual uris
+        final Map<String, String> virtuals = factory.getVirtualURLMap();
+        if (virtuals != null) {
+            for (final Entry<String, String> virtualEntry : 
virtuals.entrySet()) {
+                final String extPath = virtualEntry.getKey();
+                final String intPath = virtualEntry.getValue();
+                if (!extPath.equals(intPath)) {
+                    // this regular expression must match the whole URL !!
+                    final String url = "^" + ANY_SCHEME_HOST + extPath + "$";
+                    final String redirect = intPath;
+                    MapEntry mapEntry = getMapEntry(url, -1, redirect);
+                    if (mapEntry!=null){
+                        entries.add(mapEntry);
+                    }
+                }
+            }
         }
-        long processElapsed = System.nanoTime() - processStart;
-        long resourcePerSecond = (count * TimeUnit.SECONDS.toNanos(1) / 
(processElapsed == 0 ? 1 : processElapsed));
-
-        String diagnostics = "";
-        if (it instanceof PagedQueryIterator) {
-            PagedQueryIterator pit = (PagedQueryIterator)it;
 
-            if (!pit.getWarning().isEmpty()) {
-                log.warn(pit.getWarning());
+        // URL Mappings
+        final Mapping[] mappings = factory.getMappings();
+        if (mappings != null) {
+            final Map<String, List<String>> map = new HashMap<>();
+            for (final Mapping mapping : mappings) {
+                if (mapping.mapsInbound()) {
+                    final String url = mapping.getTo();
+                    final String alias = mapping.getFrom();
+                    if (url.length() > 0) {
+                        List<String> aliasList = map.get(url);
+                        if (aliasList == null) {
+                            aliasList = new ArrayList<>();
+                            map.put(url, aliasList);
+                        }
+                        aliasList.add(alias);
+                    }
+                }
             }
 
-            diagnostics = pit.getStatistics();
-        }
-
-        log.info("alias initialization - completed, processed {} resources 
with sling:alias properties in {}ms (~{} resource/s){}",
-                count, TimeUnit.NANOSECONDS.toMillis(processElapsed), 
resourcePerSecond, diagnostics);
+            for (final Entry<String, List<String>> entry : map.entrySet()) {
+                MapEntry mapEntry = getMapEntry(ANY_SCHEME_HOST + 
entry.getKey(), -1, entry.getValue().toArray(new String[0]));
+                if (mapEntry!=null){
+                    entries.add(mapEntry);
+                }
+            }
+        }
+    }
+
+    private void loadMapConfiguration(final MapConfigurationProvider factory, 
final Map<String, MapEntry> entries) {
+        // URL Mappings
+        final Mapping[] mappings = factory.getMappings();
+        if (mappings != null) {
+            for (int i = mappings.length - 1; i >= 0; i--) {
+                final Mapping mapping = mappings[i];
+                if (mapping.mapsOutbound()) {
+                    final String url = mapping.getTo();
+                    final String alias = mapping.getFrom();
+                    if (!url.equals(alias)) {
+                        addMapEntry(entries, alias, url, -1);
+                    }
+                }
+            }
+        }
+
+        // virtual uris
+        final Map<String, String> virtuals = factory.getVirtualURLMap();
+        if (virtuals != null) {
+            for (final Entry<String, String> virtualEntry : 
virtuals.entrySet()) {
+                final String extPath = virtualEntry.getKey();
+                final String intPath = virtualEntry.getValue();
+                if (!extPath.equals(intPath)) {
+                    // this regular expression must match the whole URL !!
+                    final String path = "^" + intPath + "$";
+                    final String url = extPath;
+                    addMapEntry(entries, path, url, -1);
+                }
+            }
+        }
+    }
+
+    private void addMapEntry(final Map<String, MapEntry> entries, final String 
path, final String url, final int status) {
+        MapEntry entry = entries.get(path);
+        if (entry == null) {
+            entry = getMapEntry(path, status, url);
+        } else {
+            final String[] redir = entry.getRedirect();
+            final String[] newRedir = new String[redir.length + 1];
+            System.arraycopy(redir, 0, newRedir, 0, redir.length);
+            newRedir[redir.length] = url;
+            entry = getMapEntry(entry.getPattern(), entry.getStatus(), 
newRedir);
+        }
+        if (entry!=null){
+            entries.put(path, entry);
+        }
+    }
+
+    private MapEntry getMapEntry(final String url, final int status, final 
String... redirect) {
+        return getMapEntry(url, status, 0, redirect);
+    }
+
+    private MapEntry getMapEntry(final String url, final int status, long 
order,
+            final String... redirect) {
+        try {
+            return new MapEntry(url, status, false, order, redirect);
+        } catch (IllegalArgumentException iae) {
+            // ignore this entry
+            log.debug("ignored entry for {} due to exception", url, iae);
+            return null;
+        }
+    }
+
+
+    private void drainQueue() {
+        final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
+
+        // send the change event only once
+        boolean sendEvent = false;
+
+        // the config needs to be reloaded only once
+        final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
+
+        while (!resourceChangeQueue.isEmpty()) {
+            Map.Entry<String, ResourceChange.ChangeType> entry = 
resourceChangeQueue.remove(0);
+            final ResourceChange.ChangeType type = entry.getValue();
+            final String path = entry.getKey();
+
+            log.trace("drain type={}, path={}", type, path);
+            boolean changed = handleResourceChange(type, path, 
resolverRefreshed, hasReloadedConfig);
+
+            if (changed) {
+                sendEvent = true;
+            }
+        }
+
+        if (sendEvent) {
+            sendChangeEvent();
+        }
+    }
+
+    // Alias handling code
+
+    /**
+     * Actual initializer. Guards itself against concurrent use by using a
+     * ReentrantLock. Does nothing if the resource resolver has already been
+     * null-ed.
+     * @return true if the optimizedAliasResolution is enabled, false otherwise
+     */
+    protected boolean initializeAliases() {
+
+        this.initializing.lock();
+        try {
+            final ResourceResolver resolver = this.resolver;
+            final MapConfigurationProvider factory = this.factory;
+            if (resolver == null || factory == null) {
+                return this.factory.isOptimizeAliasResolutionEnabled();
+            }
+
+            List<String> conflictingAliases = new ArrayList<>();
+            List<String> invalidAliases = new ArrayList<>();
+
+            boolean isOptimizeAliasResolutionEnabled = 
this.factory.isOptimizeAliasResolutionEnabled();
+
+            //optimization made in SLING-2521
+            if (isOptimizeAliasResolutionEnabled) {
+                try {
+                    final Map<String, Map<String, Collection<String>>> 
loadedMap = this.loadAliases(resolver, conflictingAliases, invalidAliases);
+                    this.aliasMapsMap = loadedMap;
+
+                    // warn if there are more than a few defunct aliases
+                    if (conflictingAliases.size() >= 
MAX_REPORT_DEFUNCT_ALIASES) {
+                        log.warn("There are {} conflicting aliases; excerpt: 
{}",  conflictingAliases.size(), conflictingAliases);
+                    } 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);
+                    }
+                } catch (final Exception e) {
+
+                    logDisableAliasOptimization(e);
+
+                    // disable optimize alias resolution
+                    isOptimizeAliasResolutionEnabled = false;
+                }
+            }
+
+            doUpdateConfiguration();
+
+            sendChangeEvent();
+
+            return isOptimizeAliasResolutionEnabled;
+
+        } finally {
+
+            this.initializing.unlock();
+
+        }
+    }
+
+    private boolean doAddAlias(final Resource resource) {
+        return loadAlias(resource, this.aliasMapsMap, null, null);
+    }
+
+    /**
+     * Remove all aliases for the content path
+     * @param contentPath The content path
+     * @param path Optional sub path of the vanity path
+     * @return {@code true} if a change happened
+     */
+    private boolean removeAlias(final String contentPath, final String path, 
final AtomicBoolean resolverRefreshed) {
+        // if path is specified we first need to find out if it is
+        // a direct child of vanity path but not jcr:content, or a jcr:content 
child of a direct child
+        // otherwise we can discard the event
+        boolean handle = true;
+        final String resourcePath;
+        if ( path != null  && path.length() > contentPath.length()) {
+            final String subPath = path.substring(contentPath.length() + 1);
+            final int firstSlash = subPath.indexOf('/');
+            if ( firstSlash == -1 ) {
+                if ( subPath.equals(JCR_CONTENT) ) {
+                    handle = false;
+                }
+                resourcePath = path;
+            } else if ( subPath.lastIndexOf('/') == firstSlash) {
+                if ( subPath.startsWith(JCR_CONTENT_PREFIX) || 
!subPath.endsWith(JCR_CONTENT_SUFFIX) ) {
+                    handle = false;
+                }
+                resourcePath = ResourceUtil.getParent(path);
+            } else {
+                handle = false;
+                resourcePath = null;
+            }
+        }
+        else {
+            resourcePath = contentPath;
+        }
+        if ( !handle ) {
+            return false;
+        }
+
+        this.initializing.lock();
+        try {
+            final Map<String, Collection<String>> aliasMapEntry = 
aliasMapsMap.get(contentPath);
+            if (aliasMapEntry != null) {
+                this.refreshResolverIfNecessary(resolverRefreshed);
+
+                String prefix = contentPath.endsWith("/") ? contentPath : 
contentPath + "/";
+                if (aliasMapEntry.entrySet().removeIf(e -> (prefix + 
e.getKey()).startsWith(resourcePath)) &&  (aliasMapEntry.isEmpty())) {
+                    this.aliasMapsMap.remove(contentPath);
+                }
+
+                Resource containingResource = this.resolver != null ? 
this.resolver.getResource(resourcePath) : null;
+
+                if (containingResource != null) {
+                    if 
(containingResource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) 
{
+                        doAddAlias(containingResource);
+                    }
+                    final Resource child = 
containingResource.getChild(JCR_CONTENT);
+                    if (child != null && 
child.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS)) {
+                        doAddAlias(child);
+                    }
+                }
+            }
+            return aliasMapEntry != null;
+        } finally {
+            this.initializing.unlock();
+        }
+    }
+
+    /**
+     * Update alias from a resource
+     * @param resource The resource
+     * @return {@code true} if any change
+     */
+    private boolean doUpdateAlias(final Resource resource) {
+
+        // resource containing the alias
+        final Resource containingResource = getResourceToBeAliased(resource);
+
+        if ( containingResource != null ) {
+            final String containingResourceName = containingResource.getName();
+            final String parentPath = 
ResourceUtil.getParent(containingResource.getPath());
+
+            final Map<String, Collection<String>> aliasMapEntry = parentPath 
== null ? null : aliasMapsMap.get(parentPath);
+            if (aliasMapEntry != null) {
+                aliasMapEntry.remove(containingResourceName);
+                if (aliasMapEntry.isEmpty()) {
+                    this.aliasMapsMap.remove(parentPath);
+                }
+            }
+
+            boolean changed = aliasMapEntry != null;
+
+            if ( 
containingResource.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS) ) 
{
+                changed |= doAddAlias(containingResource);
+            }
+            final Resource child = containingResource.getChild(JCR_CONTENT);
+            if ( child != null && 
child.getValueMap().containsKey(ResourceResolverImpl.PROP_ALIAS) ) {
+                changed |= doAddAlias(child);
+            }
+
+            return changed;
+        } else {
+            log.warn("containingResource is null for alias on {}, skipping.", 
resource.getPath());
+        }
+
+        return false;
+    }
+
+    @Override
+    public @NotNull Map<String, Collection<String>> getAliasMap(final String 
parentPath) {
+        Map<String, Collection<String>> aliasMapForParent = 
aliasMapsMap.get(parentPath);
+        return aliasMapForParent != null ? aliasMapForParent : 
Collections.emptyMap();
+    }
+
+    @Override
+    public Map<String, List<String>> getVanityPathMappings() {
+        return vph.getVanityPathMappings();
+    }
+
+    /**
+     * Load aliases - Search for all nodes (except under /jcr:system) below
+     * configured alias locations having the sling:alias property
+     */
+    private Map<String, Map<String, Collection<String>>> loadAliases(final 
ResourceResolver resolver,
+                                                                     
List<String> conflictingAliases, List<String> invalidAliases) {
+
+        final Map<String, Map<String, Collection<String>>> map = new 
ConcurrentHashMap<>();
+        final String baseQueryString = generateAliasQuery();
+
+        Iterator<Resource> it;
+        try {
+            final String queryStringWithSort = baseQueryString + " AND 
FIRST([sling:alias]) >= '%s' ORDER BY FIRST([sling:alias])";
+            it = new PagedQueryIterator("alias", "sling:alias", resolver, 
queryStringWithSort, 2000);
+        } catch (QuerySyntaxException ex) {
+            log.debug("sort with first() not supported, falling back to base 
query", ex);
+            it = queryUnpaged("alias", baseQueryString);
+        } catch (UnsupportedOperationException ex) {
+            log.debug("query failed as unsupported, retrying without 
paging/sorting", ex);
+            it = queryUnpaged("alias", baseQueryString);
+        }
+
+        log.debug("alias initialization - start");
+        long count = 0;
+        long processStart = System.nanoTime();
+        while (it.hasNext()) {
+            count += 1;
+            loadAlias(it.next(), map, conflictingAliases, invalidAliases);
+        }
+        long processElapsed = System.nanoTime() - processStart;
+        long resourcePerSecond = (count * TimeUnit.SECONDS.toNanos(1) / 
(processElapsed == 0 ? 1 : processElapsed));
+
+        String diagnostics = "";
+        if (it instanceof PagedQueryIterator) {
+            PagedQueryIterator pit = (PagedQueryIterator)it;
+
+            if (!pit.getWarning().isEmpty()) {
+                log.warn(pit.getWarning());
+            }
+
+            diagnostics = pit.getStatistics();
+        }
+
+        log.info("alias initialization - completed, processed {} resources 
with sling:alias properties in {}ms (~{} resource/s){}",
+                count, TimeUnit.NANOSECONDS.toMillis(processElapsed), 
resourcePerSecond, diagnostics);
 
         this.aliasResourcesOnStartup.set(count);
 
@@ -869,7 +1008,7 @@ public class MapEntries implements
      * Load alias given a resource
      */
     private boolean loadAlias(final Resource resource, Map<String, Map<String, 
Collection<String>>> map,
-            List<String> conflictingAliases, List<String> invalidAliases) {
+                              List<String> conflictingAliases, List<String> 
invalidAliases) {
 
         // resource containing the alias
         final Resource containingResource = getResourceToBeAliased(resource);
@@ -900,7 +1039,7 @@ public class MapEntries implements
      * Load alias given a an alias array, return success flag.
      */
     private boolean loadAliasFromArray(final String[] aliasArray, Map<String, 
Map<String, Collection<String>>> map,
-            List<String> conflictingAliases, List<String> invalidAliases, 
final String resourceName, final String parentPath) {
+                                       List<String> conflictingAliases, 
List<String> invalidAliases, final String resourceName, final String 
parentPath) {
 
         boolean hasAlias = false;
 
@@ -980,100 +1119,6 @@ public class MapEntries implements
         return it;
     }
 
-    private void loadConfiguration(final MapConfigurationProvider factory, 
final List<MapEntry> entries) {
-        // virtual uris
-        final Map<String, String> virtuals = factory.getVirtualURLMap();
-        if (virtuals != null) {
-            for (final Entry<String, String> virtualEntry : 
virtuals.entrySet()) {
-                final String extPath = virtualEntry.getKey();
-                final String intPath = virtualEntry.getValue();
-                if (!extPath.equals(intPath)) {
-                    // this regular expression must match the whole URL !!
-                    final String url = "^" + ANY_SCHEME_HOST + extPath + "$";
-                    final String redirect = intPath;
-                    MapEntry mapEntry = getMapEntry(url, -1, redirect);
-                    if (mapEntry!=null){
-                        entries.add(mapEntry);
-                    }
-                }
-            }
-        }
-
-        // URL Mappings
-        final Mapping[] mappings = factory.getMappings();
-        if (mappings != null) {
-            final Map<String, List<String>> map = new HashMap<>();
-            for (final Mapping mapping : mappings) {
-                if (mapping.mapsInbound()) {
-                    final String url = mapping.getTo();
-                    final String alias = mapping.getFrom();
-                    if (url.length() > 0) {
-                        List<String> aliasList = map.get(url);
-                        if (aliasList == null) {
-                            aliasList = new ArrayList<>();
-                            map.put(url, aliasList);
-                        }
-                        aliasList.add(alias);
-                    }
-                }
-            }
-
-            for (final Entry<String, List<String>> entry : map.entrySet()) {
-                MapEntry mapEntry = getMapEntry(ANY_SCHEME_HOST + 
entry.getKey(), -1, entry.getValue().toArray(new String[0]));
-                if (mapEntry!=null){
-                    entries.add(mapEntry);
-                }
-            }
-        }
-    }
-
-    private void loadMapConfiguration(final MapConfigurationProvider factory, 
final Map<String, MapEntry> entries) {
-        // URL Mappings
-        final Mapping[] mappings = factory.getMappings();
-        if (mappings != null) {
-            for (int i = mappings.length - 1; i >= 0; i--) {
-                final Mapping mapping = mappings[i];
-                if (mapping.mapsOutbound()) {
-                    final String url = mapping.getTo();
-                    final String alias = mapping.getFrom();
-                    if (!url.equals(alias)) {
-                        addMapEntry(entries, alias, url, -1);
-                    }
-                }
-            }
-        }
-
-        // virtual uris
-        final Map<String, String> virtuals = factory.getVirtualURLMap();
-        if (virtuals != null) {
-            for (final Entry<String, String> virtualEntry : 
virtuals.entrySet()) {
-                final String extPath = virtualEntry.getKey();
-                final String intPath = virtualEntry.getValue();
-                if (!extPath.equals(intPath)) {
-                    // this regular expression must match the whole URL !!
-                    final String path = "^" + intPath + "$";
-                    final String url = extPath;
-                    addMapEntry(entries, path, url, -1);
-                }
-            }
-        }
-    }
-
-    private void addMapEntry(final Map<String, MapEntry> entries, final String 
path, final String url, final int status) {
-        MapEntry entry = entries.get(path);
-        if (entry == null) {
-            entry = getMapEntry(path, status, url);
-        } else {
-            final String[] redir = entry.getRedirect();
-            final String[] newRedir = new String[redir.length + 1];
-            System.arraycopy(redir, 0, newRedir, 0, redir.length);
-            newRedir[redir.length] = url;
-            entry = getMapEntry(entry.getPattern(), entry.getStatus(), 
newRedir);
-        }
-        if (entry!=null){
-            entries.put(path, entry);
-        }
-    }
 
     private final AtomicLong lastTimeLogged = new AtomicLong(-1);
 
@@ -1094,47 +1139,4 @@ public class MapEntries implements
             }
         }
     }
-
-    private MapEntry getMapEntry(final String url, final int status, final 
String... redirect) {
-        return getMapEntry(url, status, 0, redirect);
-    }
-
-    private MapEntry getMapEntry(final String url, final int status, long 
order,
-            final String... redirect) {
-        try {
-            return new MapEntry(url, status, false, order, redirect);
-        } catch (IllegalArgumentException iae) {
-            // ignore this entry
-            log.debug("ignored entry for {} due to exception", url, iae);
-            return null;
-        }
-    }
-
-
-    private void drainQueue() {
-        final AtomicBoolean resolverRefreshed = new AtomicBoolean(false);
-
-        // send the change event only once
-        boolean sendEvent = false;
-
-        // the config needs to be reloaded only once
-        final AtomicBoolean hasReloadedConfig = new AtomicBoolean(false);
-
-        while (!resourceChangeQueue.isEmpty()) {
-            Map.Entry<String, ResourceChange.ChangeType> entry = 
resourceChangeQueue.remove(0);
-            final ResourceChange.ChangeType type = entry.getValue();
-            final String path = entry.getKey();
-
-            log.trace("drain type={}, path={}", type, path);
-            boolean changed = handleResourceChange(type, path, 
resolverRefreshed, hasReloadedConfig);
-
-            if (changed) {
-                sendEvent = true;
-            }
-        }
-
-        if (sendEvent) {
-            sendChangeEvent();
-        }
-    }
 }

Reply via email to