You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2021/04/08 12:40:19 UTC

[sling-org-apache-sling-resourceresolver] branch feature/track-map-calls updated: [WIP] Enhance map tracking to also record the original request's path

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

rombert pushed a commit to branch feature/track-map-calls
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/feature/track-map-calls by this push:
     new bc62966  [WIP] Enhance map tracking to also record the original request's path
bc62966 is described below

commit bc629669aef1d1c8a9b620c5f8b5ea33d7962daf
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu Apr 8 14:37:48 2021 +0200

    [WIP] Enhance map tracking to also record the original request's path
---
 .../resourceresolver/impl/mapping/MapTracker.java  | 51 +++++++++++++++++++---
 .../impl/mapping/ResourceMapperImpl.java           |  2 +-
 .../impl/mapping/MapTrackerTest.java               |  6 +--
 3 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapTracker.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapTracker.java
index 253261b..3ec7c4d 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapTracker.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapTracker.java
@@ -19,13 +19,13 @@
 package org.apache.sling.resourceresolver.impl.mapping;
 
 import java.io.PrintWriter;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
-public class MapTracker {
+import javax.servlet.http.HttpServletRequest;
 
-    public static void main(String[] args) {
-    }
+public class MapTracker {
 
     private static final MapTracker INSTANCE = new MapTracker();
 
@@ -33,10 +33,10 @@ public class MapTracker {
         return INSTANCE;
     }
 
-    private final ConcurrentHashMap<String, AtomicInteger> calls = new ConcurrentHashMap<>();
+    private final ConcurrentHashMap<Key, AtomicInteger> calls = new ConcurrentHashMap<>();
 
-    public void trackMapCall(String resourcePath) {
-        calls.computeIfAbsent(resourcePath, path -> new AtomicInteger(0)).incrementAndGet();
+    public void trackMapCall(String resourcePath, HttpServletRequest request) {
+        calls.computeIfAbsent(new Key(resourcePath, request), path -> new AtomicInteger(0)).incrementAndGet();
     }
 
     public void dump(PrintWriter pw) {
@@ -45,11 +45,48 @@ public class MapTracker {
         calls.entrySet()
             .stream()
             .sorted( (first, second) -> Integer.compare(second.getValue().get(), first.getValue().get()) )
-            .forEachOrdered( entry -> pw.printf("%10d\t%s%n", entry.getValue().get(), entry.getKey()));
+            .forEachOrdered( entry -> pw.printf("%10d\t%s\t%s%n", entry.getValue().get(), entry.getKey().getResourcePath(), entry.getKey().getRequestPath()));
         pw.println("--- END ---");
     }
 
     public void clear() {
         calls.clear();
     }
+
+    static class Key {
+        private final String resourcePath;
+        private final String requestPath;
+
+        public Key(String resourcePath, HttpServletRequest request) {
+            this.resourcePath = resourcePath;
+            this.requestPath = request != null ? request.getRequestURI() : null;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(requestPath, resourcePath);
+        }
+
+        public String getRequestPath() {
+            return requestPath;
+        }
+
+        public String getResourcePath() {
+            return resourcePath;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            Key other = (Key) obj;
+            return Objects.equals(requestPath, other.requestPath) && Objects.equals(resourcePath, other.resourcePath);
+        }
+
+
+    }
 }
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
index ed83a1d..54dae52 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
@@ -86,7 +86,7 @@ public class ResourceMapperImpl implements ResourceMapper {
     public Collection<String> getAllMappings(String resourcePath, HttpServletRequest request) {
         
         resolver.checkClosed();
-        MapTracker.get().trackMapCall(resourcePath);
+        MapTracker.get().trackMapCall(resourcePath, request);
         
         // A note on the usage of the 'mappings' variable and the order of the results
         //
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapTrackerTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapTrackerTest.java
index 7e41443..11c8a22 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapTrackerTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapTrackerTest.java
@@ -36,13 +36,13 @@ public class MapTrackerTest {
         mt.clear();
 
         for (int i = 0; i < 10; i++)
-            mt.trackMapCall("/content.html");
+            mt.trackMapCall("/content.html", null);
 
         for (int i = 0; i < 2; i++)
-            mt.trackMapCall("/content/foo.html");
+            mt.trackMapCall("/content/foo.html", null);
 
         for (int i = 0; i < 5; i++)
-            mt.trackMapCall("/content/bar.html");
+            mt.trackMapCall("/content/bar.html", null);
 
         StringWriter out = new StringWriter();
         mt.dump(new PrintWriter(out));