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 2017/11/07 10:24:23 UTC

[sling-org-apache-sling-tracer] 23/30: SLING-5507 - Collect more details around query execution

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

rombert pushed a commit to annotated tag org.apache.sling.tracer-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tracer.git

commit 393c6662b64358b541f51202fb96d0e49cfe9d3f
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Feb 11 04:05:55 2016 +0000

    SLING-5507 - Collect more details around query execution
    
    Simplify the logic for determining the caller
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/tracer@1729759 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/tracer/internal/CallerFinder.java | 29 +++++++++-------------
 .../sling/tracer/internal/CallerFinderTest.java    | 18 ++++++++++++++
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/sling/tracer/internal/CallerFinder.java b/src/main/java/org/apache/sling/tracer/internal/CallerFinder.java
index f08e8e7..5c688c7 100644
--- a/src/main/java/org/apache/sling/tracer/internal/CallerFinder.java
+++ b/src/main/java/org/apache/sling/tracer/internal/CallerFinder.java
@@ -19,15 +19,8 @@
 
 package org.apache.sling.tracer.internal;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
 import javax.annotation.CheckForNull;
 
-import com.google.common.collect.Iterators;
-import com.google.common.collect.PeekingIterator;
-
 /**
  * Utility to find out the real caller by excluding stack elements belonging to
  * API classes. Say for a query it would exclude the call stack which is part of Oak
@@ -36,6 +29,11 @@ import com.google.common.collect.PeekingIterator;
 class CallerFinder {
     private final String[] apiPkgs;
 
+    /**
+     * Array of package names which form the API
+     * @param apiPkgs package names in the order they can appear in caller stack. For e.g.
+     *                Sling API package would always come before Oak api package for query evaluation
+     */
     public CallerFinder(String[] apiPkgs) {
         this.apiPkgs = apiPkgs;
     }
@@ -46,17 +44,13 @@ class CallerFinder {
             return null;
         }
 
-        //Reverse the stack trace so as to start from bottom
-        List<StackTraceElement> stackList = Arrays.asList(stack);
-        Collections.reverse(stackList);
-        PeekingIterator<StackTraceElement> pit = Iterators.peekingIterator(stackList.iterator());
-        while (pit.hasNext()) {
-            StackTraceElement current = pit.next();
+        for (int i = stack.length - 1; i >= 0; i--) {
+            StackTraceElement current = stack[i];
+            if (i > 0) {
+                StackTraceElement next = stack[i - 1];
 
-            //now scan each element and check if the *next* stack element belongs to any
-            //api package. If yes then current stack would be the caller
-            if (pit.hasNext()) {
-                StackTraceElement next = pit.peek();
+                //now scan each element and check if the *next* stack element belongs to any
+                //api package. If yes then current stack would be the caller
                 for (String pkg : apiPkgs) {
                     if (next.getClassName().startsWith(pkg)) {
                         return current;
@@ -64,6 +58,7 @@ class CallerFinder {
                 }
             }
         }
+
         return null;
     }
 }
diff --git a/src/test/java/org/apache/sling/tracer/internal/CallerFinderTest.java b/src/test/java/org/apache/sling/tracer/internal/CallerFinderTest.java
index 51d4ba2..29906ef 100644
--- a/src/test/java/org/apache/sling/tracer/internal/CallerFinderTest.java
+++ b/src/test/java/org/apache/sling/tracer/internal/CallerFinderTest.java
@@ -86,6 +86,24 @@ public class CallerFinderTest {
         assertNull(cf.determineCaller(null));
     }
 
+    @Test
+    public void nullCaller() throws Exception{
+        CallerFinder cf = new CallerFinder(new String[] {"o.a1.s", "o.a1.j.o"});
+        StackTraceElement[] stack = createStack(
+                "o.a.j.o.a",
+                "o.a.j.o.b",
+                "o.a.s.a",
+                "o.a.s.b",
+                "c.a.g.w",
+                "o.e.j",
+                "o.e.j",
+                "o.e.j"
+        );
+
+        StackTraceElement caller = cf.determineCaller(stack);
+        assertNull(caller);
+    }
+
     private static StackTraceElement[] createStack(String ... stack){
         StackTraceElement[] result = new StackTraceElement[stack.length];
         for (int i = 0; i < stack.length; i++) {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.