You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ch...@apache.org on 2018/01/03 11:10:19 UTC

[sling-org-apache-sling-tracer] branch master updated: SLING-7348 - Switch to OSGi annotation for Tracer bundle

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 15ba544  SLING-7348 - Switch to OSGi annotation for Tracer bundle
15ba544 is described below

commit 15ba5447285f5f97690c4fd1e96a42a469915c6b
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Wed Jan 3 16:39:41 2018 +0530

    SLING-7348 - Switch to OSGi annotation for Tracer bundle
---
 pom.xml                                            |  21 ++---
 .../sling/tracer/internal/Configuration.java       |  71 +++++++++++++++
 .../apache/sling/tracer/internal/LogTracer.java    | 101 ++++-----------------
 .../sling/tracer/internal/TracerLogServlet.java    |   9 --
 .../sling/tracer/internal/LogTracerTest.java       |   8 +-
 .../tracer/internal/TracerLogServletTest.java      |  14 ++-
 6 files changed, 110 insertions(+), 114 deletions(-)

diff --git a/pom.xml b/pom.xml
index b4be5ea..73c835b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@
   <parent>
     <groupId>org.apache.sling</groupId>
     <artifactId>sling</artifactId>
-    <version>26</version>
+    <version>32</version>
   </parent>
 
   <artifactId>org.apache.sling.tracer</artifactId>
@@ -65,10 +65,6 @@
           </instructions>
         </configuration>
       </plugin>
-      <plugin>
-        <groupId>org.apache.felix</groupId>
-        <artifactId>maven-scr-plugin</artifactId>
-      </plugin>
     </plugins>
   </build>
 
@@ -89,6 +85,7 @@
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
     </dependency>
     <dependency>
       <groupId>org.apache.sling</groupId>
@@ -102,15 +99,15 @@
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.core</artifactId>
-      <version>4.3.1</version>
-      <scope>provided</scope>
+      <artifactId>osgi.annotation</artifactId>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.compendium</artifactId>
-      <version>4.3.1</version>
-      <scope>provided</scope>
+      <artifactId>osgi.cmpn</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>osgi.core</artifactId>
     </dependency>
     <dependency>
         <groupId>org.apache.felix</groupId>
@@ -155,7 +152,7 @@
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
-      <version>1.3.0</version>
+      <version>2.3.4</version>
       <scope>test</scope>
     </dependency>
     <dependency>
diff --git a/src/main/java/org/apache/sling/tracer/internal/Configuration.java b/src/main/java/org/apache/sling/tracer/internal/Configuration.java
new file mode 100644
index 0000000..c2fb956
--- /dev/null
+++ b/src/main/java/org/apache/sling/tracer/internal/Configuration.java
@@ -0,0 +1,71 @@
+/*
+ * 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.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(name = "Apache Sling Log Tracer",
+        description =  "Provides support for enabling log for specific loggers on per request basis. " +
+                "Refer to http://sling.apache.org/documentation/bundles/log-tracers.html for " +
+                "more details")
+public @interface Configuration {
+
+    @AttributeDefinition(
+            name = "Tracer Sets",
+            description = "Default list of tracer sets configured. Tracer Set config confirms " +
+                    "to following format. <set name> : <logger name>;level=<level name>, other loggers")
+    String[] tracerSets() default {
+            "oak-query : org.apache.jackrabbit.oak.query.QueryEngineImpl;level=debug",
+            "oak-writes : org.apache.jackrabbit.oak.jcr.operations.writes;level=trace"
+    };
+
+    @AttributeDefinition(
+            name = "Enabled",
+            description = "Enable the Tracer")
+    boolean enabled();
+
+    @AttributeDefinition(
+            name = "Recording Servlet Enabled",
+            description = "Enable the Tracer Servlet. This servlet is required for the tracer recording feature " +
+                    "to work and provides access to the json dump of the recording performed")
+    boolean servletEnabled();
+
+    @AttributeDefinition(
+            name = "Recording Cache Size",
+            description = "Recording cache size in MB which would be used to temporary cache the recording data")
+    int recordingCacheSizeInMB() default 50;
+
+    @AttributeDefinition(
+            name = "Recording Cache Duration",
+            description = "Time in seconds upto which the recording data would be held in memory before expiry")
+    long recordingCacheDurationInSecs() default 60 * 15;
+
+    @AttributeDefinition(
+            name = "Compress Recording",
+            description = "Enable compression for recoding held in memory")
+    boolean recordingCompressionEnabled() default true;
+
+    @AttributeDefinition(
+            name = "GZip Response",
+            description = "If enabled the response sent would be compressed")
+    boolean gzipResponse() default true;
+
+}
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 9f38a65..b474a3b 100644
--- a/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
+++ b/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
@@ -43,18 +43,16 @@ import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.turbo.TurboFilter;
 import ch.qos.logback.core.spi.FilterReply;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.ConfigurationPolicy;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.PropertyUnbounded;
 import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
+import org.osgi.service.metatype.annotations.Designate;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Marker;
 
@@ -69,14 +67,8 @@ import static org.apache.sling.tracer.internal.Util.trimToNull;
  * For such parts enabling the log at global level would flood the logs and create lots of noise.
  * Using Tracer one can enable log for that request which is required to be probed
  */
-@Component(
-        label = "Apache Sling Log Tracer",
-        description = "Provides support for enabling log for specific loggers on per request basis. " +
-                "Refer to http://sling.apache.org/documentation/bundles/log-tracers.html for " +
-                "more details",
-        policy = ConfigurationPolicy.REQUIRE,
-        metatype = true
-)
+@Component(configurationPolicy = ConfigurationPolicy.REQUIRE)
+@Designate(ocd = Configuration.class)
 public class LogTracer {
     /**
      * Request parameter name having comma separated value to determine list of tracers to
@@ -95,59 +87,7 @@ public class LogTracer {
 
     public static final String HEADER_TRACER = "Sling-Tracers";
 
-    @Property(label = "Tracer Sets",
-            description = "Default list of tracer sets configured. Tracer Set config confirms " +
-                    "to following format. <set name> : <logger name>;level=<level name>, other loggers",
-            unbounded = PropertyUnbounded.ARRAY,
-            value = {
-                    "oak-query : org.apache.jackrabbit.oak.query.QueryEngineImpl;level=debug",
-                    "oak-writes : org.apache.jackrabbit.oak.jcr.operations.writes;level=trace"
-            }
-    )
-    private static final String PROP_TRACER_SETS = "tracerSets";
-
-    private static final boolean PROP_TRACER_ENABLED_DEFAULT = false;
-    @Property(label = "Enabled",
-            description = "Enable the Tracer",
-            boolValue = PROP_TRACER_ENABLED_DEFAULT
-    )
-    private static final String PROP_TRACER_ENABLED = "enabled";
-
-    private static final boolean PROP_TRACER_SERVLET_ENABLED_DEFAULT = false;
-    @Property(label = "Recording Servlet Enabled",
-            description = "Enable the Tracer Servlet. This servlet is required for the tracer recording feature " +
-                    "to work and provides access to the json dump of the recording performed",
-            boolValue = PROP_TRACER_SERVLET_ENABLED_DEFAULT
-    )
-    private static final String PROP_TRACER_SERVLET_ENABLED = "servletEnabled";
-
-    static final int PROP_TRACER_SERVLET_CACHE_SIZE_DEFAULT = 50;
-    @Property(label = "Recording Cache Size",
-            description = "Recording cache size in MB which would be used to temporary cache the recording data",
-            intValue = PROP_TRACER_SERVLET_CACHE_SIZE_DEFAULT
-    )
-    private static final String PROP_TRACER_SERVLET_CACHE_SIZE = "recordingCacheSizeInMB";
-
-    static final long PROP_TRACER_SERVLET_CACHE_DURATION_DEFAULT = 60 * 15;
-    @Property(label = "Recording Cache Duration",
-            description = "Time in seconds upto which the recording data would be held in memory before expiry",
-            longValue = PROP_TRACER_SERVLET_CACHE_DURATION_DEFAULT
-    )
-    private static final String PROP_TRACER_SERVLET_CACHE_DURATION = "recordingCacheDurationInSecs";
-
-    static final boolean PROP_TRACER_SERVLET_COMPRESS_DEFAULT = true;
-    @Property(label = "Compress Recording",
-            description = "Enable compression for recoding held in memory",
-            boolValue = PROP_TRACER_SERVLET_COMPRESS_DEFAULT
-    )
-    private static final String PROP_TRACER_SERVLET_COMPRESS = "recordingCompressionEnabled";
-
-    static final boolean PROP_TRACER_SERVLET_GZIP_RESPONSE_DEFAULT = true;
-    @Property(label = "GZip Response",
-            description = "If enabled the response sent would be compressed",
-            boolValue = PROP_TRACER_SERVLET_GZIP_RESPONSE_DEFAULT
-    )
-    private static final String PROP_TRACER_SERVLET_GZIP_RESPONSE = "gzipResponse";
+
 
     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(LogTracer.class);
 
@@ -172,24 +112,19 @@ public class LogTracer {
     private TraceLogRecorder recorder = TraceLogRecorder.DEFAULT;
 
     @Activate
-    private void activate(Map<String, ?> config, BundleContext context) {
+    private void activate(Configuration config, BundleContext context) {
         this.bundleContext = context;
         initializeTracerSet(config);
-        boolean enabled = PropertiesUtil.toBoolean(config.get(PROP_TRACER_ENABLED), PROP_TRACER_ENABLED_DEFAULT);
+        boolean enabled = config.enabled();
         if (enabled) {
             registerFilters(context);
-            boolean servletEnabled = PropertiesUtil.toBoolean(config.get(PROP_TRACER_SERVLET_ENABLED),
-                    PROP_TRACER_SERVLET_ENABLED_DEFAULT);
+            boolean servletEnabled = config.servletEnabled();
 
             if (servletEnabled) {
-                int cacheSize = PropertiesUtil.toInteger(config.get(PROP_TRACER_SERVLET_CACHE_SIZE),
-                        PROP_TRACER_SERVLET_CACHE_SIZE_DEFAULT);
-                long cacheDuration = PropertiesUtil.toLong(config.get(PROP_TRACER_SERVLET_CACHE_DURATION),
-                        PROP_TRACER_SERVLET_CACHE_DURATION_DEFAULT);
-                boolean compressionEnabled = PropertiesUtil.toBoolean(config.get(PROP_TRACER_SERVLET_COMPRESS),
-                        PROP_TRACER_SERVLET_COMPRESS_DEFAULT);
-                boolean gzipResponse = PropertiesUtil.toBoolean(config.get(PROP_TRACER_SERVLET_GZIP_RESPONSE),
-                        PROP_TRACER_SERVLET_GZIP_RESPONSE_DEFAULT);
+                int cacheSize = config.recordingCacheSizeInMB();
+                long cacheDuration = config.recordingCacheDurationInSecs();
+                boolean compressionEnabled = config.recordingCompressionEnabled();
+                boolean gzipResponse = config.gzipResponse();
 
                 this.logServlet = new TracerLogServlet(context, cacheSize, cacheDuration, compressionEnabled, gzipResponse);
                 recorder = logServlet;
@@ -259,10 +194,8 @@ public class LogTracer {
         return new TracerContext(configs.toArray(new TracerConfig[configs.size()]), recording);
     }
 
-    private void initializeTracerSet(Map<String, ?> config) {
-        String[] tracerSetConfigs = PropertiesUtil.toStringArray(config.get(PROP_TRACER_SETS), new String[0]);
-
-        for (String tracerSetConfig : tracerSetConfigs) {
+    private void initializeTracerSet(Configuration config) {
+        for (String tracerSetConfig : config.tracerSets()) {
             TracerSet tc = new TracerSet(tracerSetConfig);
             tracers.put(tc.getName(), tc);
         }
diff --git a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
index da2feaa..fcb99ae 100644
--- a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
+++ b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
@@ -67,15 +67,6 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
 
     private final boolean gzipResponse;
 
-    public TracerLogServlet(BundleContext context){
-        this(context,
-                LogTracer.PROP_TRACER_SERVLET_CACHE_SIZE_DEFAULT,
-                LogTracer.PROP_TRACER_SERVLET_CACHE_DURATION_DEFAULT,
-                LogTracer.PROP_TRACER_SERVLET_COMPRESS_DEFAULT,
-                LogTracer.PROP_TRACER_SERVLET_GZIP_RESPONSE_DEFAULT
-        );
-    }
-
     public TracerLogServlet(BundleContext context, int cacheSizeInMB, long cacheDurationInSecs,
                             boolean compressionEnabled, boolean gzipResponse) {
         super(LABEL, "Sling Tracer", "Sling", null);
diff --git a/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java b/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
index 6961509..41b67c8 100644
--- a/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
+++ b/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
@@ -101,7 +101,7 @@ public class LogTracerTest {
         assertEquals(2, context.getServices(Filter.class, null).length);
         assertNull(context.getService(Servlet.class));
 
-        MockOsgi.deactivate(tracer);
+        MockOsgi.deactivate(tracer, context.bundleContext());
         assertNull(context.getService(Filter.class));
     }
 
@@ -114,10 +114,10 @@ public class LogTracerTest {
 
         TracerLogServlet logServlet = (TracerLogServlet) context.getService(Servlet.class);
         assertEquals(true, logServlet.isCompressRecording());
-        assertEquals(LogTracer.PROP_TRACER_SERVLET_CACHE_SIZE_DEFAULT, logServlet.getCacheSizeInMB());
-        assertEquals(LogTracer.PROP_TRACER_SERVLET_CACHE_DURATION_DEFAULT, logServlet.getCacheDurationInSecs());
+        assertEquals(50, logServlet.getCacheSizeInMB());
+        assertEquals(60*15, logServlet.getCacheDurationInSecs());
 
-        MockOsgi.deactivate(tracer);
+        MockOsgi.deactivate(tracer, context.bundleContext());
         assertNull(context.getService(Filter.class));
         assertNull(context.getService(Servlet.class));
     }
diff --git a/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java b/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
index 988c208..f9d0290 100644
--- a/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
+++ b/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
@@ -68,14 +68,14 @@ public class TracerLogServletTest {
 
     @Test
     public void noRecordingByDefault() throws Exception{
-        TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
+        TracerLogServlet logServlet = newLogServlet();
         assertSame(Recording.NOOP, logServlet.startRecording(request, response));
         assertSame(Recording.NOOP, logServlet.getRecordingForRequest(request));
     }
 
     @Test
     public void recordingWhenRequested() throws Exception{
-        TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
+        TracerLogServlet logServlet = newLogServlet();
         request = new MockSlingHttpServletRequest(context.bundleContext());
 
         Recording recording = logServlet.startRecording(request, response);
@@ -98,7 +98,7 @@ public class TracerLogServletTest {
 
     @Test
     public void jsonRendering() throws Exception{
-        TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
+        TracerLogServlet logServlet = newLogServlet();
         when(request.getMethod()).thenReturn("GET");
         when(request.getHeader(TracerLogServlet.HEADER_TRACER_RECORDING)).thenReturn("true");
 
@@ -123,7 +123,7 @@ public class TracerLogServletTest {
 
     @Test
     public void gzipResponse() throws Exception{
-        TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
+        TracerLogServlet logServlet = newLogServlet();
         when(request.getMethod()).thenReturn("GET");
         when(request.getHeader(TracerLogServlet.HEADER_TRACER_RECORDING)).thenReturn("true");
         when(request.getHeader("Accept-Encoding")).thenReturn("gzip, deflate");
@@ -150,9 +150,13 @@ public class TracerLogServletTest {
         verify(response).setHeader("Content-Encoding" , "gzip");
     }
 
+    private TracerLogServlet newLogServlet() {
+        return new TracerLogServlet(context.bundleContext(), 50, 60*15, true, true);
+    }
+
     @Test
     public void pluginRendering() throws Exception{
-        TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
+        TracerLogServlet logServlet = newLogServlet();
         when(request.getRequestURI()).thenReturn("/system/console/tracer" );
 
         StringWriter sw = new StringWriter();

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