[java-support] branch master updated: OSJ-214: Make sure all internally-created Timers have names

Brent Putman putmanb at georgetown.edu
Wed Aug 16 20:18:38 EDT 2017


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

putmanb pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=92dee797a654c556314837875629465d7739007c

The following commit(s) were added to refs/heads/master by this push:
       new  92dee79   OSJ-214: Make sure all internally-created Timers have names
92dee79 is described below

commit 92dee797a654c556314837875629465d7739007c
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed Aug 16 20:15:11 2017 -0400

    OSJ-214: Make sure all internally-created Timers have names
---
 .../httpclient/FileCachingHttpClientBuilder.java   |  3 +-
 .../support/httpclient/IdleConnectionSweeper.java  |  3 +-
 .../java/support/primitive/TimerSupport.java       | 99 ++++++++++++++++++++++
 .../support/security/BasicKeystoreKeyStrategy.java |  3 +-
 .../support/service/AbstractReloadableService.java |  3 +-
 .../java/support/primitive/TimerSupportTest.java   | 85 +++++++++++++++++++
 6 files changed, 192 insertions(+), 4 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilder.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilder.java
index ca662f8..68e8cdb 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilder.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilder.java
@@ -32,6 +32,7 @@ import net.shibboleth.utilities.java.support.component.DestructableComponent;
 import net.shibboleth.utilities.java.support.component.InitializableComponent;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.primitive.StringSupport;
+import net.shibboleth.utilities.java.support.primitive.TimerSupport;
 
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
@@ -345,7 +346,7 @@ public class FileCachingHttpClientBuilder extends HttpClientBuilder {
 
         /** {@inheritDoc} */
         public void initialize() throws ComponentInitializationException {
-            timer = new Timer(true);
+            timer = new Timer(TimerSupport.getTimerName(this), true);
             maintenanceTask = new StorageMaintenanceTask(storage);
             timer.schedule(maintenanceTask, maintenanceTaskInterval, maintenanceTaskInterval);
             
diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
index 0f81584..b55974a 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
@@ -26,6 +26,7 @@ import javax.annotation.Nonnull;
 import net.shibboleth.utilities.java.support.component.DestroyedComponentException;
 import net.shibboleth.utilities.java.support.component.DestructableComponent;
 import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.primitive.TimerSupport;
 
 import org.apache.http.conn.HttpClientConnectionManager;
 
@@ -59,7 +60,7 @@ public class IdleConnectionSweeper implements DestructableComponent {
      */
     public IdleConnectionSweeper(@Nonnull final HttpClientConnectionManager manager, final long idleTimeout,
             final long sweepInterval) {
-        this(manager, idleTimeout, sweepInterval, new Timer(true));
+        this(manager, idleTimeout, sweepInterval, new Timer(TimerSupport.getTimerName(IdleConnectionSweeper.class.getName(), null), true));
         createdTimer = true;
     }
 
diff --git a/src/main/java/net/shibboleth/utilities/java/support/primitive/TimerSupport.java b/src/main/java/net/shibboleth/utilities/java/support/primitive/TimerSupport.java
new file mode 100644
index 0000000..b27b847
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/primitive/TimerSupport.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.primitive;
+
+import java.util.Timer;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.IdentifiedComponent;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/** Timer utility methods. */
+public final class TimerSupport {
+    
+    /** Constructor. */
+    private TimerSupport() { }
+    
+    /**
+     * Produce the name which should be used for a {@link Timer} owned by the specified object.
+     * 
+     * <p>The base name will constructed as follows:
+     * <ul>
+     *  <li>if target is instance of {@link IdentifiedComponent}, use {@link IdentifiedComponent#getId()} if non-empty</li>
+     *  <li>use {@link #toString()} if non-empty</li>
+     *  <li>use serialized class name</li>
+     * </ul>
+     * </p>
+     * 
+     * @param obj the target object instance to evaluate
+     * @return an appropriate name for a Timer owned by the specified object
+     */
+    @Nonnull @NotEmpty public static String getTimerName(final @Nonnull Object obj) {
+        return getTimerName(obj, null);
+    }
+    
+    /**
+     * Produce the name which should be used for a {@link Timer} owned by the specified object.
+     * 
+     * <p>The base name will constructed as follows:
+     * <ul>
+     *  <li>if target is instance of {@link IdentifiedComponent}, use {@link IdentifiedComponent#getId()} if non-empty</li>
+     *  <li>use {@link #toString()} if non-empty</li>
+     *  <li>use serialized class name</li>
+     * </ul>
+     * </p>
+     * 
+     * @param obj the target object instance to evaluate
+     * @param additionalData additional qualifying data to include in the name
+     * @return an appropriate name for a Timer owned by the specified object
+     */
+    @Nonnull @NotEmpty public static String getTimerName(final @Nonnull Object obj, final @Nullable String additionalData) {
+        Constraint.isNotNull(obj, "Target object for Timer was null");
+        
+        String baseName = null;
+        if (obj instanceof IdentifiedComponent && StringSupport.trimOrNull(((IdentifiedComponent)obj).getId()) != null) {
+            baseName = StringSupport.trimOrNull(((IdentifiedComponent)obj).getId());
+        } else if (StringSupport.trimOrNull(obj.toString()) != null){
+            baseName = StringSupport.trimOrNull(obj.toString());
+        } else {
+            baseName = obj.getClass().getName();
+        }
+        
+        return getTimerName(baseName, additionalData);
+    }
+        
+    /**
+     * Produce the name for a {@link Timer} based on the specified base name.
+     * 
+     * @param baseName the base name of Timer
+     * @param additionalData additional qualifying data to include in the name
+     * @return an appropriate name for a Timer based on the specified base name
+     */
+    @Nonnull @NotEmpty public static String getTimerName(final @Nonnull String baseName, final @Nullable String additionalData) {
+        Constraint.isNotNull(baseName, "Base name for Timer was null");
+        if (additionalData != null) {
+            return String.format("Timer for %s (%s)", baseName, additionalData);
+        } else {
+            return String.format("Timer for %s", baseName);
+        }
+    }
+
+}
diff --git a/src/main/java/net/shibboleth/utilities/java/support/security/BasicKeystoreKeyStrategy.java b/src/main/java/net/shibboleth/utilities/java/support/security/BasicKeystoreKeyStrategy.java
index 21f4dca..c2e8fec 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/security/BasicKeystoreKeyStrategy.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/security/BasicKeystoreKeyStrategy.java
@@ -45,6 +45,7 @@ import net.shibboleth.utilities.java.support.component.ComponentSupport;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
 import net.shibboleth.utilities.java.support.primitive.StringSupport;
+import net.shibboleth.utilities.java.support.primitive.TimerSupport;
 import net.shibboleth.utilities.java.support.resource.Resource;
 
 import org.slf4j.Logger;
@@ -238,7 +239,7 @@ public class BasicKeystoreKeyStrategy extends AbstractInitializableComponent imp
                 }
             };
             if (updateTaskTimer == null) {
-                internalTaskTimer = new Timer(true);
+                internalTaskTimer = new Timer(TimerSupport.getTimerName(this), true);
             } else {
                 internalTaskTimer = updateTaskTimer;
             }
diff --git a/src/main/java/net/shibboleth/utilities/java/support/service/AbstractReloadableService.java b/src/main/java/net/shibboleth/utilities/java/support/service/AbstractReloadableService.java
index 27361e1..9cc9952 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/service/AbstractReloadableService.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/service/AbstractReloadableService.java
@@ -29,6 +29,7 @@ import net.shibboleth.utilities.java.support.component.AbstractIdentifiableIniti
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
 import net.shibboleth.utilities.java.support.component.UnmodifiableComponent;
+import net.shibboleth.utilities.java.support.primitive.TimerSupport;
 
 import org.joda.time.DateTime;
 import org.joda.time.chrono.ISOChronology;
@@ -192,7 +193,7 @@ public abstract class AbstractReloadableService<T> extends AbstractIdentifiableI
         if (reloadCheckDelay > 0) {
             if (null == reloadTaskTimer) {
                 log.debug("{} No reload task timer specified, creating default", getLogPrefix());
-                internalTaskTimer = new Timer("Timer for " + getId(), true);
+                internalTaskTimer = new Timer(TimerSupport.getTimerName(this), true);
             } else {
                 internalTaskTimer = reloadTaskTimer;
             }
diff --git a/src/test/java/net/shibboleth/utilities/java/support/primitive/TimerSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/primitive/TimerSupportTest.java
new file mode 100644
index 0000000..c028127
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/primitive/TimerSupportTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.primitive;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import net.shibboleth.utilities.java.support.component.IdentifiedComponent;
+
+public class TimerSupportTest {
+    
+    @Test
+    public void testBasic() {
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   myComponentID    ", "   myComponentToString   ")), 
+                "Timer for myComponentID");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   ", "   myComponentToString   ")), 
+                "Timer for myComponentToString");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   ", "   ")), 
+                "Timer for " + MockIdentifiedComponent.class.getName());
+        Assert.assertEquals(TimerSupport.getTimerName(new MockObject("   myComponentToString   ")), 
+                "Timer for myComponentToString");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockObject("   ")), 
+                "Timer for " + MockObject.class.getName());
+        
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   myComponentID    ", "   myComponentToString   "), "abc123"), 
+                "Timer for myComponentID (abc123)");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   ", "   myComponentToString   "), "abc123"), 
+                "Timer for myComponentToString (abc123)");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockIdentifiedComponent("   ", "   "), "abc123"), 
+                "Timer for " + MockIdentifiedComponent.class.getName() + " (abc123)");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockObject("   myComponentToString   "), "abc123"), 
+                "Timer for myComponentToString (abc123)");
+        Assert.assertEquals(TimerSupport.getTimerName(new MockObject("   "), "abc123"), 
+                "Timer for " + MockObject.class.getName() + " (abc123)");
+    }
+    
+    private static class MockIdentifiedComponent implements IdentifiedComponent {
+        
+        private String id;
+        private String toString;
+        
+        public MockIdentifiedComponent(String idValue, String toStringValue) {
+            id = idValue;
+            toString = toStringValue;
+        }
+        
+        public String getId() {
+            return id;
+        }
+        
+        public String toString() {
+            return toString;
+        }
+    }
+    
+    private static class MockObject {
+        
+        private String toString;
+        
+        public MockObject(String toStringValue) {
+            toString = toStringValue;
+        }
+        
+        public String toString() {
+            return toString;
+        }
+        
+    }
+
+}

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


More information about the commits mailing list