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.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tracer.git
commit 871b6f39088a734b99168795f8295ac5dec3ac01 Author: Chetan Mehrotra <[email protected]> AuthorDate: Sat Oct 8 09:29:53 2016 +0000 SLING-6118 - Handle empty tracer config header gracefully git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/tracer@1763881 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/tracer/internal/JSONRecording.java | 16 +------ .../apache/sling/tracer/internal/LogTracer.java | 5 +++ .../org/apache/sling/tracer/internal/Util.java | 47 +++++++++++++++++++++ .../org/apache/sling/tracer/internal/UtilTest.java | 49 ++++++++++++++++++++++ 4 files changed, 103 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java index 21d99d4..2941caf 100644 --- a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java +++ b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java @@ -53,6 +53,8 @@ import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.sling.tracer.internal.Util.nullSafeString; +import static org.apache.sling.tracer.internal.Util.nullSafeTrim; class JSONRecording implements Recording, Comparable<JSONRecording> { private static final String[] QUERY_API_PKGS = { @@ -389,18 +391,4 @@ class JSONRecording implements Recording, Comparable<JSONRecording> { } } - private static String nullSafeTrim(String s){ - if(s == null){ - return ""; - } - return s.trim(); - } - - private static String nullSafeString(Object o){ - if (o != null){ - return o.toString(); - } - return null; - } - } diff --git a/src/main/java/org/apache/sling/tracer/internal/LogTracer.java b/src/main/java/org/apache/sling/tracer/internal/LogTracer.java index 59c647c..c95d7fd 100644 --- a/src/main/java/org/apache/sling/tracer/internal/LogTracer.java +++ b/src/main/java/org/apache/sling/tracer/internal/LogTracer.java @@ -58,6 +58,8 @@ import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; import org.slf4j.LoggerFactory; import org.slf4j.Marker; +import static org.apache.sling.tracer.internal.Util.trimToNull; + /** * Tracer provides support for enabling the logs for specific category at specific level and * only for specific request. It provides a very fine level of control via config provided @@ -224,6 +226,9 @@ public class LogTracer { TracerContext getTracerContext(String tracerSetNames, String tracerConfig, Recording recording) { //No config or tracer set name provided. So tracing not required + tracerConfig = trimToNull(tracerConfig); + tracerSetNames = trimToNull(tracerSetNames); + if (tracerSetNames == null && tracerConfig == null) { return null; } diff --git a/src/main/java/org/apache/sling/tracer/internal/Util.java b/src/main/java/org/apache/sling/tracer/internal/Util.java new file mode 100644 index 0000000..617881e --- /dev/null +++ b/src/main/java/org/apache/sling/tracer/internal/Util.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sling.tracer.internal; + +class Util { + static String trimToNull(String s) { + if (s == null) { + return null; + } + s = s.trim(); + if (s.isEmpty()) { + return null; + } + return s; + } + + static String nullSafeString(Object o){ + if (o != null){ + return o.toString(); + } + return null; + } + + static String nullSafeTrim(String s){ + if(s == null){ + return ""; + } + return s.trim(); + } +} diff --git a/src/test/java/org/apache/sling/tracer/internal/UtilTest.java b/src/test/java/org/apache/sling/tracer/internal/UtilTest.java new file mode 100644 index 0000000..8dd878e --- /dev/null +++ b/src/test/java/org/apache/sling/tracer/internal/UtilTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sling.tracer.internal; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class UtilTest { + + @Test + public void trimToNull() throws Exception{ + assertEquals(null, Util.trimToNull(null)); + assertEquals(null, Util.trimToNull("")); + assertEquals("foo", Util.trimToNull("foo")); + assertEquals("foo", Util.trimToNull(" foo")); + } + + @Test + public void nullSafeString() throws Exception{ + assertEquals(null, Util.nullSafeString(null)); + assertEquals("foo", Util.nullSafeString("foo")); + assertEquals("1", Util.nullSafeString(1)); + } + + @Test + public void nullSafeTrim() throws Exception{ + assertEquals("", Util.nullSafeTrim(null)); + assertEquals("foo", Util.nullSafeTrim(" foo")); + } + +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
