001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package java.lang;
026:
027: import java.util.*;
028:
029: /*
030: * Class to track and run user level shutdown hooks registered through
031: * <tt>{@link Runtime#addShutdownHook Runtime.addShutdownHook}</tt>.
032: *
033: * @see java.lang.Runtime#addShutdownHook
034: * @see java.lang.Runtime#removeShutdownHook
035: */
036:
037: class ApplicationShutdownHooks implements Runnable {
038: private static ApplicationShutdownHooks instance = null;
039:
040: /* The set of registered hooks */
041: private static IdentityHashMap<Thread, Thread> hooks = new IdentityHashMap<Thread, Thread>();
042:
043: static synchronized ApplicationShutdownHooks hook() {
044: if (instance == null)
045: instance = new ApplicationShutdownHooks();
046:
047: return instance;
048: }
049:
050: private ApplicationShutdownHooks() {
051: }
052:
053: /* Add a new shutdown hook. Checks the shutdown state and the hook itself,
054: * but does not do any security checks.
055: */
056: static synchronized void add(Thread hook) {
057: if (hooks == null)
058: throw new IllegalStateException("Shutdown in progress");
059:
060: if (hook.isAlive())
061: throw new IllegalArgumentException("Hook already running");
062:
063: if (hooks.containsKey(hook))
064: throw new IllegalArgumentException(
065: "Hook previously registered");
066:
067: hooks.put(hook, hook);
068: }
069:
070: /* Remove a previously-registered hook. Like the add method, this method
071: * does not do any security checks.
072: */
073: static synchronized boolean remove(Thread hook) {
074: if (hooks == null)
075: throw new IllegalStateException("Shutdown in progress");
076:
077: if (hook == null)
078: throw new NullPointerException();
079:
080: return hooks.remove(hook) != null;
081: }
082:
083: /* Iterates over all application hooks creating a new thread for each
084: * to run in. Hooks are run concurrently and this method waits for
085: * them to finish.
086: */
087: public void run() {
088: Collection<Thread> threads;
089: synchronized (ApplicationShutdownHooks.class) {
090: threads = hooks.keySet();
091: hooks = null;
092: }
093:
094: for (Thread hook : threads) {
095: hook.start();
096: }
097: for (Thread hook : threads) {
098: try {
099: hook.join();
100: } catch (InterruptedException x) {
101: }
102: }
103: }
104: }
|