[java-shib-shared] branch main updated: JSSH-5 ServiceableComponent should implement AutoClose

Rod Widdowson rdw at steadingsoftware.com
Sat Nov 26 13:30:12 UTC 2022


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=f613c1f2e19baccb8546b43ad311510346b00588

The following commit(s) were added to refs/heads/main by this push:
     new f613c1f2 JSSH-5 ServiceableComponent should implement AutoClose
f613c1f2 is described below

commit f613c1f2e19baccb8546b43ad311510346b00588
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Nov 26 13:13:58 2022 +0000

    JSSH-5 ServiceableComponent should implement AutoClose
    
    https://shibboleth.atlassian.net/browse/JSSH-5
    
    ClassBasedServiceStrategy now deals with arbitrary types and encapsulates
    the object into a SpringServiceableComponent.
    
    Clean up the implenmentation of  ReloadableSpringService and its test
    for readability.
---
 .../spring/service/ClassBasedServiceStrategy.java  | 23 ++++++---------
 .../spring/service/ReloadableSpringService.java    | 22 +++++++-------
 .../service/ReloadableSpringServiceTest.java       | 34 ++++++++++------------
 .../spring/service/TestServiceableComponent.java   |  9 ++----
 4 files changed, 37 insertions(+), 51 deletions(-)

diff --git a/shib-service/src/main/java/net/shibboleth/shared/spring/service/ClassBasedServiceStrategy.java b/shib-service/src/main/java/net/shibboleth/shared/spring/service/ClassBasedServiceStrategy.java
index 6bdafa17..395d3127 100644
--- a/shib-service/src/main/java/net/shibboleth/shared/spring/service/ClassBasedServiceStrategy.java
+++ b/shib-service/src/main/java/net/shibboleth/shared/spring/service/ClassBasedServiceStrategy.java
@@ -28,24 +28,17 @@ import org.springframework.context.ApplicationContext;
 import net.shibboleth.shared.annotation.ParameterName;
 import net.shibboleth.shared.logic.Constraint;
 import net.shibboleth.shared.service.ServiceException;
+import net.shibboleth.shared.spring.service.impl.SpringServiceableComponent;
 
 /**
  * Strategy to create {@link AbstractServiceableComponent}s from the {@link ApplicationContext}.
  * 
- * @param <T> the service type to look for; defaults to {@link AbstractServiceableComponent}
+ * @param <T> the service type to look for
  */
- at SuppressWarnings("rawtypes")
 public class ClassBasedServiceStrategy<T> implements Function<ApplicationContext, AbstractServiceableComponent<T>> {
 
     /** The class we are looking for. */
-    @Nonnull private final Class<AbstractServiceableComponent> serviceClaz;
-
-    /**
-     * Constructor.
-     */
-    public ClassBasedServiceStrategy() {
-        serviceClaz = AbstractServiceableComponent.class;
-    }
+    @Nonnull private final Class<T> serviceClaz;
 
     /**
      * Constructor.
@@ -53,7 +46,7 @@ public class ClassBasedServiceStrategy<T> implements Function<ApplicationContext
      * @param serviceableClaz what to look for.
      */
     public ClassBasedServiceStrategy(
-            @ParameterName(name="serviceableClaz") final Class<AbstractServiceableComponent> serviceableClaz) {
+            @ParameterName(name="serviceableClaz") final Class<T> serviceableClaz) {
         serviceClaz = Constraint.isNotNull(serviceableClaz, "Serviceable Class cannot be null");
     }
 
@@ -64,7 +57,7 @@ public class ClassBasedServiceStrategy<T> implements Function<ApplicationContext
             throw new ServiceException("Input ApplicationContext was null");
         }
         
-        final Collection<AbstractServiceableComponent> components = appContext.getBeansOfType(serviceClaz).values();
+        final Collection<T> components = appContext.getBeansOfType(serviceClaz).values();
 
         if (components.size() == 0) {
             throw new ServiceException("Reload did not produce any bean of type " + serviceClaz.getName());
@@ -72,8 +65,10 @@ public class ClassBasedServiceStrategy<T> implements Function<ApplicationContext
         if (components.size() > 1) {
             throw new ServiceException("Reload produced " + components.size() + " ServiceableComponents");
         }
-
-        return components.iterator().next();
+        final T value = components.iterator().next();
+        final SpringServiceableComponent<T> result = new  SpringServiceableComponent<>(value);
+        result.setApplicationContext(appContext);
+        return result;
     }
     
 }
\ No newline at end of file
diff --git a/shib-service/src/main/java/net/shibboleth/shared/spring/service/ReloadableSpringService.java b/shib-service/src/main/java/net/shibboleth/shared/spring/service/ReloadableSpringService.java
index de28840f..3c044fb5 100644
--- a/shib-service/src/main/java/net/shibboleth/shared/spring/service/ReloadableSpringService.java
+++ b/shib-service/src/main/java/net/shibboleth/shared/spring/service/ReloadableSpringService.java
@@ -115,7 +115,7 @@ public class ReloadableSpringService<T> extends AbstractReloadableService<T> imp
      * @param claz The interface being implemented.
      */
     public ReloadableSpringService(@Nonnull @ParameterName(name="claz") final Class<T> claz) {
-        this(claz, new ClassBasedServiceStrategy<T>());
+        this(claz, new ClassBasedServiceStrategy<>(claz));
     }
 
     /**
@@ -379,27 +379,27 @@ public class ReloadableSpringService<T> extends AbstractReloadableService<T> imp
 
         log.debug("{} New Application Context created for service '{}'", getLogPrefix(), getId());
 
-        final AbstractServiceableComponent<T> service;
+        final AbstractServiceableComponent<T> component;
         try {
-            service = serviceStrategy.apply(appContext);
+            component = serviceStrategy.apply(appContext);
         } catch (final Exception e) {
             appContext.close();
             throw new ServiceException("Failed to load " + getServiceConfigurations(), e);
         }
 
-        service.pinComponent();
+        component.pinComponent();
 
         // Now check it's the right type before we continue.
-        final T theComponent = service.getComponent();
+        final T theObject = component.getComponent();
 
-        log.debug("{} Testing that {} is a superclass of {}", getLogPrefix(), theComponent.getClass(), theClaz);
+        log.debug("{} Testing that {} is a superclass of {}", getLogPrefix(), theObject.getClass(), theClaz);
 
-        if (!theClaz.isAssignableFrom(theComponent.getClass())) {
+        if (!theClaz.isAssignableFrom(theObject.getClass())) {
             //
             // tear it down
             //
-            service.unpinComponent();
-            service.unloadComponent();
+            component.unpinComponent();
+            component.unloadComponent();
             throw new ServiceException("Class was not the same or a superclass of configured class");
         }
 
@@ -414,8 +414,8 @@ public class ReloadableSpringService<T> extends AbstractReloadableService<T> imp
         final AbstractServiceableComponent<T> oldComponent;
         synchronized (this) {
             oldComponent = cachedComponent;
-            cachedComponent = service;
-            service.unpinComponent();
+            cachedComponent = component;
+            component.unpinComponent();
         }
         
         log.info("{} Completed reload and swapped in latest configuration for service '{}'", getLogPrefix(), getId());
diff --git a/shib-service/src/test/java/net/shibboleth/shared/spring/service/ReloadableSpringServiceTest.java b/shib-service/src/test/java/net/shibboleth/shared/spring/service/ReloadableSpringServiceTest.java
index 591406f2..daa58856 100644
--- a/shib-service/src/test/java/net/shibboleth/shared/spring/service/ReloadableSpringServiceTest.java
+++ b/shib-service/src/test/java/net/shibboleth/shared/spring/service/ReloadableSpringServiceTest.java
@@ -97,7 +97,7 @@ public class ReloadableSpringServiceTest {
         final TestServiceableComponent component = serviceableComponent.getComponent();
 
         Assert.assertEquals("One", component.getTheValue());
-        Assert.assertFalse(component.getComponent().isDestroyed());
+        Assert.assertFalse(component.isDestroyed());
 
         serviceableComponent.unpinComponent();
         overwriteFileWith("net/shibboleth/shared/spring/service/ServiceableBean2.xml");
@@ -137,10 +137,11 @@ public class ReloadableSpringServiceTest {
 
         service.start();
 
-        ServiceableComponent<TestServiceableComponent> serviceableComponent = service.getServiceableComponent();
-        assert(serviceableComponent != null);
-        final TestServiceableComponent component = serviceableComponent.getComponent();
-        
+        final TestServiceableComponent component;
+        try (final ServiceableComponent<TestServiceableComponent> serviceableComponent = service.getServiceableComponent()) {
+            assert(serviceableComponent != null);
+            component = serviceableComponent.getComponent();
+        }
         final Instant x = service.getLastReloadAttemptInstant();
         Assert.assertEquals(x,  service.getLastSuccessfulReloadInstant());
 
@@ -160,29 +161,25 @@ public class ReloadableSpringServiceTest {
         long count = 70;
         TestServiceableComponent component2 = null;
         while (count > 0) {
-            serviceableComponent = service.getServiceableComponent();
-            assert(serviceableComponent != null);
-            component2 = serviceableComponent.getComponent();
-            if ("Two".equals(component2.getTheValue())) {
-                component2.unpinComponent();
-                break;
+            try (ServiceableComponent<TestServiceableComponent> serviceableComponent = service.getServiceableComponent()) {
+                assert(serviceableComponent != null);
+                component2 = serviceableComponent.getComponent();
+                if ("Two".equals(component2.getTheValue())) {
+                    break;
+                }
             }
-            component2.unpinComponent();
             component2 = null;
             Thread.sleep(RELOAD_DELAY.toMillis());
             count--;
         }
         Assert.assertNotNull(component2, "After 7 second initial component has still not got new value");
-
-        component.unpinComponent();
-
+    
         count = 70;
         while (count > 0 && !component.isDestroyed()) {
             Thread.sleep(RELOAD_DELAY.toMillis());
             count--;
         }
         Assert.assertTrue(component.isDestroyed(), "After 7 second initial component has still not be destroyed");
-
         service.stop();
         deleteFile();
     }
@@ -214,7 +211,6 @@ public class ReloadableSpringServiceTest {
         service.stop();
         deleteFile();
     }
-
     @Test public void testNotFailFast() throws IOException, InterruptedException {
         final ReloadableSpringService<TestServiceableComponent> service =
                 new ReloadableSpringService<>(TestServiceableComponent.class);
@@ -232,7 +228,7 @@ public class ReloadableSpringServiceTest {
         overwriteFileWith("net/shibboleth/shared/spring/service/ServiceableBean2.xml");
 
         long count = 700;
-        ServiceableComponent<TestServiceableComponent> serviceableComponent = service.getServiceableComponent();
+        AbstractServiceableComponent<TestServiceableComponent> serviceableComponent = service.getServiceableComponent();
         while (count > 0 && null == serviceableComponent) {
             Thread.sleep(RELOAD_DELAY.toMillis());
             count--;
@@ -244,7 +240,7 @@ public class ReloadableSpringServiceTest {
         Assert.assertEquals(component.getTheValue(), "Two");
 
         Assert.assertFalse(component.isDestroyed());
-        component.unpinComponent();
+        serviceableComponent.unpinComponent();
         service.stop();
 
         count = 70;
diff --git a/shib-service/src/test/java/net/shibboleth/shared/spring/service/TestServiceableComponent.java b/shib-service/src/test/java/net/shibboleth/shared/spring/service/TestServiceableComponent.java
index ba37895a..bae3a936 100644
--- a/shib-service/src/test/java/net/shibboleth/shared/spring/service/TestServiceableComponent.java
+++ b/shib-service/src/test/java/net/shibboleth/shared/spring/service/TestServiceableComponent.java
@@ -17,18 +17,13 @@
 
 package net.shibboleth.shared.spring.service;
 
-import javax.annotation.Nonnull;
+import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent;
 
 @SuppressWarnings("javadoc")
-public class TestServiceableComponent extends AbstractServiceableComponent<TestServiceableComponent> {
+public class TestServiceableComponent extends AbstractIdentifiableInitializableComponent {
 
     private String theValue;
     
-    /** {@inheritDoc} */
-    @Override
-    @Nonnull public TestServiceableComponent getComponent() {
-        return this;
-    }
 
     /**
      * @return Returns the theValue.

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list