1 /*
  2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.util.*;
 28 import java.util.concurrent.RejectedExecutionException;
 29 
 30 /*
 31  * Class to track and run user level shutdown hooks registered through
 32  * {@link Runtime#addShutdownHook Runtime.addShutdownHook}.
 33  *
 34  * @see java.lang.Runtime#addShutdownHook
 35  * @see java.lang.Runtime#removeShutdownHook
 36  */
 37 
 38 class ApplicationShutdownHooks {
 39     /* The set of registered hooks */
 40     private static IdentityHashMap<Thread, Thread> hooks;
 41     static {
 42         try {
 43             Shutdown.add(1 /* shutdown hook invocation order */,
 44                 false /* not registered if shutdown in progress */,
 45                 new Runnable() {
 46                     public void run() {
 47                         runHooks();
 48                     }
 49                 }
 50             );
 51             hooks = new IdentityHashMap<>();
 52         } catch (IllegalStateException e) {
 53             // application shutdown hooks cannot be added if
 54             // shutdown is in progress.
 55             hooks = null;
 56         }
 57     }
 58 
 59 
 60     private ApplicationShutdownHooks() {}
 61 
 62     /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
 63      * but does not do any security checks.
 64      */
 65     static synchronized void add(Thread hook) {
 66         if(hooks == null)
 67             throw new IllegalStateException("Shutdown in progress");
 68 
 69         if (hook.isAlive())
 70             throw new IllegalArgumentException("Hook already running");
 71 
 72         if (hooks.containsKey(hook))
 73             throw new IllegalArgumentException("Hook previously registered");
 74 
 75         hooks.put(hook, hook);
 76     }
 77 
 78     /* Remove a previously-registered hook.  Like the add method, this method
 79      * does not do any security checks.
 80      */
 81     static synchronized boolean remove(Thread hook) {
 82         if(hooks == null)
 83             throw new IllegalStateException("Shutdown in progress");
 84 
 85         if (hook == null)
 86             throw new NullPointerException();
 87 
 88         return hooks.remove(hook) != null;
 89     }
 90 
 91     /* Iterates over all application hooks creating a new thread for each
 92      * to run in. Hooks are run concurrently and this method waits for
 93      * them to finish.
 94      */
 95     static void runHooks() {
 96         Collection<Thread> threads;
 97         synchronized(ApplicationShutdownHooks.class) {
 98             threads = hooks.keySet();
 99             hooks = null;
100         }
101         for (Thread hook : threads) {
102             try {
103                 hook.start();
104             } catch (IllegalThreadStateException ignore) {
105                 // already started
106             } catch (RejectedExecutionException ignore) {
107                 // scheduler shutdown?
108                 assert hook.isVirtual();
109             }
110         }
111         for (Thread hook : threads) {
112             while (true) {
113                 try {
114                     hook.join();
115                     break;
116                 } catch (InterruptedException ignored) {
117                 }
118             }
119         }
120     }
121 }