From 4a6fa1eb5ea882be554706ffea95e17af1caac1c Mon Sep 17 00:00:00 2001
From: "Brian S. O'Neill" <bronee@gmail.com>
Date: Thu, 22 May 2008 15:21:36 +0000
Subject: Don't autorun shutdown hook if shutdown is in progress.

---
 .../carbonado/capability/ShutdownCapability.java   |  2 +-
 .../amazon/carbonado/spi/AbstractRepository.java   | 57 +++++++++++-----------
 2 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/amazon/carbonado/capability/ShutdownCapability.java b/src/main/java/com/amazon/carbonado/capability/ShutdownCapability.java
index 4940757..d488e3b 100644
--- a/src/main/java/com/amazon/carbonado/capability/ShutdownCapability.java
+++ b/src/main/java/com/amazon/carbonado/capability/ShutdownCapability.java
@@ -33,7 +33,7 @@ public interface ShutdownCapability extends Capability {
 
     /**
      * Request to enable or disable the automatic shutdown hook. Repository may
-     * ignore this request.
+     * ignore this request if shutdown is in progress.
      *
      * @throws SecurityException if caller does not have permission
      */
diff --git a/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java b/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java
index 563ae9e..e73a8c9 100644
--- a/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java
+++ b/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java
@@ -141,31 +141,27 @@ public abstract class AbstractRepository<Txn>
     }
 
     // Required by ShutdownCapability.
-    public boolean isAutoShutdownEnabled() {
+    public synchronized boolean isAutoShutdownEnabled() {
         return mShutdownHook != null;
     }
 
     // Required by ShutdownCapability.
-    public void setAutoShutdownEnabled(boolean enabled) {
-        if (mShutdownHook == null) {
-            if (enabled) {
-                mShutdownHook = new ShutdownHook(this);
-                try {
-                    Runtime.getRuntime().addShutdownHook(mShutdownHook);
-                } catch (IllegalStateException e) {
-                    // Shutdown in progress, so immediately run hook.
-                    mShutdownHook.run();
+    public synchronized void setAutoShutdownEnabled(boolean enabled) {
+        try {
+            if (mShutdownHook == null) {
+                if (enabled) {
+                    ShutdownHook hook = new ShutdownHook(this);
+                    Runtime.getRuntime().addShutdownHook(hook);
+                    mShutdownHook = hook;
                 }
-            }
-        } else {
-            if (!enabled) {
-                try {
+            } else {
+                if (!enabled) {
                     Runtime.getRuntime().removeShutdownHook(mShutdownHook);
-                } catch (IllegalStateException e) {
-                    // Shutdown in progress, hook is running.
+                    mShutdownHook = null;
                 }
-                mShutdownHook = null;
             }
+        } catch (IllegalStateException e) {
+            // Shutdown is in progress so make no changes.
         }
     }
 
@@ -178,23 +174,26 @@ public abstract class AbstractRepository<Txn>
         if (!mHasShutdown) {
             // Since this repository is being closed before system shutdown,
             // remove shutdown hook and run it now.
-            ShutdownHook hook = mShutdownHook;
-            if (hook != null) {
-                try {
-                    Runtime.getRuntime().removeShutdownHook(hook);
-                } catch (IllegalStateException e) {
-                    // Shutdown in progress, hook is running.
-                    hook = null;
+            ShutdownHook hook;
+            synchronized (this) {
+                hook = mShutdownHook;
+                if (hook == null) {
+                    // If hook is null, auto-shutdown was disabled. Make a new
+                    // instance to use, but don't register it.
+                    hook = new ShutdownHook(this);
+                } else {
+                    try {
+                        Runtime.getRuntime().removeShutdownHook(hook);
+                        mShutdownHook = null;
+                    } catch (IllegalStateException e) {
+                        // Shutdown in progress, hook is running.
+                        hook = null;
+                    }
                 }
-            } else {
-                // If hook is null, auto-shutdown was disabled. Make a new
-                // instance to use, but don't register it.
-                hook = new ShutdownHook(this);
             }
             if (hook != null) {
                 hook.run(suspendThreads);
             }
-            mHasShutdown = true;
         }
     }
 
-- 
cgit v1.2.3