1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Common Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* $Id: ExitHookManager.java,v 1.1.1.1 2004/05/09 16:57:58 vlad_r Exp $
*/
package com.vladium.util.exit;
import java.util.HashMap;
import java.util.Map;
import com.vladium.util.IJREVersion;
import com.vladium.util.Property;
import com.vladium.emma.IAppConstants;
// ----------------------------------------------------------------------------
/**
* @author Vlad Roubtsov, (C) 2003
*/
public
abstract class ExitHookManager implements IJREVersion
{
// public: ................................................................
// TOTO: handle thread groups as well?
public abstract boolean addExitHook (Runnable runnable);
public abstract boolean removeExitHook (Runnable runnable);
public static synchronized ExitHookManager getSingleton ()
{
if (s_singleton == null)
{
s_singleton = new JRE13ExitHookManager ();
}
return s_singleton;
}
// protected: .............................................................
protected ExitHookManager () {}
// package: ...............................................................
// private: ...............................................................
private static final class JRE13ExitHookManager extends ExitHookManager
{
public synchronized boolean addExitHook (final Runnable runnable)
{
if ((runnable != null) && ! m_exitThreadMap.containsKey (runnable))
{
final Thread exitThread = new Thread (runnable, IAppConstants.APP_NAME + " shutdown handler thread");
try
{
Runtime.getRuntime ().addShutdownHook (exitThread);
m_exitThreadMap.put (runnable, exitThread); // TODO: use identity here
return true;
}
catch (Exception e)
{
System.out.println ("exception caught while adding a shutdown hook:");
e.printStackTrace (System.out);
}
}
return false;
}
public synchronized boolean removeExitHook (final Runnable runnable)
{
if (runnable != null)
{
final Thread exitThread = (Thread) m_exitThreadMap.get (runnable); // TODO: use identity here
if (exitThread != null)
{
try
{
Runtime.getRuntime ().removeShutdownHook (exitThread);
m_exitThreadMap.remove (runnable);
return true;
}
catch (Exception e)
{
System.out.println ("exception caught while removing a shutdown hook:");
e.printStackTrace (System.out);
}
}
}
return false;
}
JRE13ExitHookManager ()
{
m_exitThreadMap = new HashMap ();
}
private final Map /* Runnable->Thread */ m_exitThreadMap;
} // end of nested class
private static ExitHookManager s_singleton;
} // end of class
// ----------------------------------------------------------------------------
|