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

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


The following commit(s) were added to refs/heads/master by this push:
     new c704ef6  SLING-12059 : Make dependency to metrics optional
c704ef6 is described below

commit c704ef6b2e44a22c812b9fa924f67aef21e9674a
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Sun Oct 1 16:39:05 2023 +0200

    SLING-12059 : Make dependency to metrics optional
---
 bnd.bnd                                            |  1 +
 .../auth/core/impl/SlingAuthenticationMetrics.java | 13 +++++++++---
 .../sling/auth/core/impl/SlingAuthenticator.java   | 23 +++++++++++++---------
 .../core/impl/SlingAuthenticationMetricsTest.java  | 11 +++++++----
 .../auth/core/impl/SlingAuthenticatorOsgiTest.java |  6 ++++--
 .../auth/core/impl/SlingAuthenticatorTest.java     |  3 +--
 6 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/bnd.bnd b/bnd.bnd
index f3533ac..4fb45e6 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -1,6 +1,7 @@
 # SLING-11446 - keep hc related imports optional
 Import-Package: !javax.jcr,\
     org.apache.felix.hc.api;resolution:=optional,\
+    org.apache.sling.commons.metrics;resolution:=optional,\
     org.apache.sling.jcr.api;resolution:=optional,\
     *
 DynamicImport-Package: javax.jcr
diff --git 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetrics.java 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetrics.java
index 17a24bf..1f6619f 100644
--- 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetrics.java
+++ 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetrics.java
@@ -16,12 +16,18 @@
  */
 package org.apache.sling.auth.core.impl;
 
+import java.io.Closeable;
+
 import org.apache.sling.commons.metrics.Meter;
 import org.apache.sling.commons.metrics.MetricsService;
 import org.apache.sling.commons.metrics.Timer;
 import org.jetbrains.annotations.NotNull;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
-class SlingAuthenticationMetrics {
+@Component(service = SlingAuthenticationMetrics.class)
+public class SlingAuthenticationMetrics {
 
     static final String AUTHENTICATE_TIMER_NAME = 
"sling.auth.core.authenticate.timer";
     static final String AUTHENTICATE_SUCCESS_METER_NAME = 
"sling.auth.core.authenticate.success";
@@ -31,14 +37,15 @@ class SlingAuthenticationMetrics {
     private final Meter authenticateSuccess;
     private final Meter authenticateFailed;
 
-    SlingAuthenticationMetrics(@NotNull MetricsService metricsService) {
+    @Activate
+    public SlingAuthenticationMetrics(@Reference @NotNull MetricsService 
metricsService) {
         authenticateTimer = metricsService.timer(AUTHENTICATE_TIMER_NAME);
         authenticateSuccess = 
metricsService.meter(AUTHENTICATE_SUCCESS_METER_NAME);
         authenticateFailed = 
metricsService.meter(AUTHENTICATE_FAILED_METER_NAME);
     }
 
     @NotNull
-    Timer.Context authenticationTimerContext() {
+    Closeable authenticationTimerContext() {
         return authenticateTimer.time();
     }
 
diff --git 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
index 8cdadf0..93e7316 100644
--- a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
+++ b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.auth.core.impl;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
@@ -52,8 +53,6 @@ import 
org.apache.sling.auth.core.spi.AuthenticationHandler.FAILURE_REASON_CODES
 import org.apache.sling.auth.core.spi.AuthenticationInfo;
 import org.apache.sling.auth.core.spi.AuthenticationInfoPostProcessor;
 import org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler;
-import org.apache.sling.commons.metrics.MetricsService;
-import org.apache.sling.commons.metrics.Timer;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
@@ -307,7 +306,8 @@ public class SlingAuthenticator implements Authenticator,
     @Reference(policy=ReferencePolicy.DYNAMIC, cardinality = 
ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
     private volatile EventAdmin eventAdmin; // NOSONAR
 
-    private final SlingAuthenticationMetrics metrics;
+    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality = 
ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY)
+    private volatile SlingAuthenticationMetrics metricsService;
 
     private final ResourceResolverFactory resourceResolverFactory;
 
@@ -320,13 +320,11 @@ public class SlingAuthenticator implements Authenticator,
     // ---------- SCR integration
 
     @Activate
-    public SlingAuthenticator(@Reference(policyOption = 
ReferencePolicyOption.GREEDY) final MetricsService metricsService,
-            @Reference AuthenticationRequirementsManager authReqManager,
+    public SlingAuthenticator(@Reference AuthenticationRequirementsManager 
authReqManager,
             @Reference AuthenticationHandlersManager authHandlerManager,
             @Reference(policyOption = ReferencePolicyOption.GREEDY) final 
ResourceResolverFactory resourceResolverFactory,
             final BundleContext bundleContext,
             final Config config) {
-        this.metrics = new SlingAuthenticationMetrics(metricsService);
         this.resourceResolverFactory = resourceResolverFactory;
 
         this.authenticationRequirementsManager = authReqManager;
@@ -421,8 +419,9 @@ public class SlingAuthenticator implements Authenticator,
             request.removeAttribute(REQUEST_ATTRIBUTE_RESOLVER);
         }
 
-        Timer.Context ctx = metrics.authenticationTimerContext();
         boolean process = false;
+        final SlingAuthenticationMetrics local = this.metricsService;
+        final Closeable ctx = local != null ? 
local.authenticationTimerContext() : null;
         try {
             process = doHandleSecurity(request, response);
             if (process && expectAuthenticationHandler(request)) {
@@ -433,8 +432,14 @@ public class SlingAuthenticator implements Authenticator,
                 process = false;
             }
         } finally {
-            ctx.stop();
-            metrics.authenticateCompleted(process);
+            if (local != null) {
+                try {
+                    ctx.close();
+                } catch (final IOException e) {
+                    // ignore
+                }
+                local.authenticateCompleted(process);
+            }
         }
         return process;
     }
diff --git 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetricsTest.java
 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetricsTest.java
index d184194..7c130c9 100644
--- 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetricsTest.java
+++ 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticationMetricsTest.java
@@ -33,6 +33,9 @@ import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
+import java.io.Closeable;
+import java.io.IOException;
+
 public class SlingAuthenticationMetricsTest {
 
     private Meter successMeter = mock(Meter.class);
@@ -75,12 +78,12 @@ public class SlingAuthenticationMetricsTest {
     }
 
     @Test
-    public void testAuthenticationTimerContext() {
-        Timer.Context timerContext = metrics.authenticationTimerContext();
-        timerContext.stop();
+    public void testAuthenticationTimerContext() throws IOException {
+        Closeable timerContext = metrics.authenticationTimerContext();
+        timerContext.close();
 
         verify(timer).time();
-        verify(ctx).stop();
+        verify(ctx).close();
         verifyNoMoreInteractions(timer, ctx);
         verifyNoInteractions(successMeter, failedMeter);
     }
diff --git 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
index d798d25..d96f031 100644
--- 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
+++ 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
+import java.io.IOException;
 import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -89,6 +90,7 @@ public class SlingAuthenticatorOsgiTest {
 
         context.registerService(ResourceResolverFactory.class, 
resourceResolverFactory);
         context.registerService(MetricsService.class, metricsService);
+        
context.registerInjectActivateService(SlingAuthenticationMetrics.class);
         
context.registerInjectActivateService(AuthenticationRequirementsManager.class);
 
         //register a test auth handler
@@ -101,7 +103,7 @@ public class SlingAuthenticatorOsgiTest {
     }
 
     @Test
-    public void testHandleSecurity() {
+    public void testHandleSecurity() throws IOException {
         HttpServletRequest req = mock(HttpServletRequest.class);
         when(req.getServletPath()).thenReturn("/");
         when(req.getServerName()).thenReturn("localhost");
@@ -113,7 +115,7 @@ public class SlingAuthenticatorOsgiTest {
         authenticator.handleSecurity(req, resp);
 
         verify(timer).time();
-        verify(ctx).stop();
+        verify(ctx).close();
         verify(successMeter).mark();
         verifyNoMoreInteractions(timer, successMeter, ctx);
         verifyNoInteractions((failedMeter));
diff --git 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
index 674f507..f0534b5 100644
--- a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
+++ b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
@@ -39,7 +39,6 @@ import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.auth.core.AuthenticationSupport;
 import org.apache.sling.auth.core.spi.AuthenticationFeedbackHandler;
 import org.apache.sling.auth.core.spi.AuthenticationInfo;
-import org.apache.sling.commons.metrics.MetricsService;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -96,7 +95,7 @@ public class SlingAuthenticatorTest {
                 i += 2;
             }
         }
-        final SlingAuthenticator slingAuthenticator = new 
SlingAuthenticator(Mockito.mock(MetricsService.class), requirements,
+        final SlingAuthenticator slingAuthenticator = new 
SlingAuthenticator(requirements,
             handlers,
             null, Mockito.mock(BundleContext.class), config);
 

Reply via email to