From 3f7ddbf313aaabbfba4650cb2036cb41e51a9bde Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 18 Apr 2013 11:37:28 +0000
Subject: [PATCH] Fix OPENDJ-838: Add ConnectionFactory.close() method to facilitate resource cleanup after application exit

---
 opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java |   44 +++++++++++++++++++++++++-------------------
 1 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index 847ad6d..f89e2b0 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2012 ForgeRock AS
+ *      Portions copyright 2011-2013 ForgeRock AS
  */
 
 package com.forgerock.opendj.util;
@@ -48,6 +48,7 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -86,9 +87,30 @@
     // UTC TimeZone is assumed to never change over JVM lifetime
     private static final TimeZone TIME_ZONE_UTC_OBJ = TimeZone.getTimeZone(TIME_ZONE_UTC);
 
-    private static ScheduledExecutorService defaultScheduler = null;
+    /**
+     * The default scheduler which should be used when the application does not
+     * provide one.
+     */
+    public static final ReferenceCountedObject<ScheduledExecutorService> DEFAULT_SCHEDULER =
+            new ReferenceCountedObject<ScheduledExecutorService>() {
 
-    private static final Object DEFAULT_SCHEDULER_LOCK = new Object();
+                @Override
+                protected ScheduledExecutorService newInstance() {
+                    final ThreadFactory factory =
+                            newThreadFactory(null, "OpenDJ LDAP SDK Default Scheduler", true);
+                    return Executors.newSingleThreadScheduledExecutor(factory);
+                }
+
+                @Override
+                protected void destroyInstance(ScheduledExecutorService instance) {
+                    instance.shutdown();
+                    try {
+                        instance.awaitTermination(5, TimeUnit.SECONDS);
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                    }
+                }
+            };
 
     /**
      * Retrieves a string representation of the provided byte in hexadecimal.
@@ -1395,22 +1417,6 @@
     }
 
     /**
-     * Returns the default scheduler which should be used by the SDK.
-     *
-     * @return The default scheduler.
-     */
-    public static ScheduledExecutorService getDefaultScheduler() {
-        synchronized (DEFAULT_SCHEDULER_LOCK) {
-            if (defaultScheduler == null) {
-                final ThreadFactory factory =
-                        newThreadFactory(null, "OpenDJ SDK Default Scheduler", true);
-                defaultScheduler = Executors.newSingleThreadScheduledExecutor(factory);
-            }
-        }
-        return defaultScheduler;
-    }
-
-    /**
      * Retrieves the best human-readable message for the provided exception. For
      * exceptions defined in the OpenDJ project, it will attempt to use the
      * message (combining it with the message ID if available). For some

--
Gitblit v1.10.0