Author: chetanm
Date: Thu Jan  7 11:31:58 2016
New Revision: 1723518

URL: http://svn.apache.org/viewvc?rev=1723518&view=rev
Log:
SLING-4080 - API to capture/measure application-level metrics

Allow adapting MetricService to MetricRegistry

Modified:
    
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
    
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
    
sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java

Modified: 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsService.java?rev=1723518&r1=1723517&r2=1723518&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
 (original)
+++ 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsService.java
 Thu Jan  7 11:31:58 2016
@@ -50,6 +50,11 @@ public interface MetricsService {
         public Meter meter(String name) {
             return NoopMetric.INSTANCE;
         }
+
+        @Override
+        public <A> A adaptTo(Class<A> type) {
+            return null;
+        }
     };
 
     /**
@@ -87,4 +92,15 @@ public interface MetricsService {
      * @return a new {@link Meter}
      */
     Meter meter(String name);
+
+    /**
+     * Adapts the service to the specified type. This can be used to
+     * get instance to underlying {@code MetricRegistry}
+     *
+     * @param <A> The type to which this metric is to be adapted.
+     * @param type Class object for the type to which this metric is to be 
adapted.
+     * @return The object, of the specified type, to which this metric has 
been adapted
+     * or null if this metric cannot be adapted to the specified type.
+     */
+    <A> A adaptTo(Class<A> type);
 }

Modified: 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java?rev=1723518&r1=1723517&r2=1723518&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
 (original)
+++ 
sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
 Thu Jan  7 11:31:58 2016
@@ -115,6 +115,15 @@ public class MetricsServiceImpl implemen
     }
 
     @SuppressWarnings("unchecked")
+    @Override
+    public <A> A adaptTo(Class<A> type) {
+        if (type == MetricRegistry.class){
+            return (A) registry;
+        }
+        return null;
+    }
+
+    @SuppressWarnings("unchecked")
     private <T extends Metric> T getOrAdd(String name, MetricBuilder<T> 
builder) {
         final Metric metric = metrics.get(name);
         if (builder.isInstance(metric)) {

Modified: 
sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java?rev=1723518&r1=1723517&r2=1723518&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
 (original)
+++ 
sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
 Thu Jan  7 11:31:58 2016
@@ -59,6 +59,8 @@ public class MetricServiceTest {
         assertNotNull(context.getService(MetricRegistry.class));
         assertNotNull(context.getService(MetricsService.class));
 
+        assertNotNull(service.adaptTo(MetricRegistry.class));
+
         MockOsgi.deactivate(service);
 
         assertNull(context.getService(MetricRegistry.class));


Reply via email to