| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
import java.util.WeakHashMap; |
| 5 |
import java.util.logging.Level; |
| 6 |
import java.util.logging.Logger; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles ordered running of cleanup processes at JVM shutdown. |
| 10 |
* This is a singleton class, use {@link #getInstance}. |
| 11 |
* |
| 12 |
* @author Sylvain Lafrasse |
| 13 |
* @author Mark Taylor |
| 14 |
* @since 12 Oct 2012 |
| 15 |
*/ |
| 16 |
public class ShutdownManager { |
| 17 |
|
| 18 |
/** Shutdown sequence for preparatory hooks. */ |
| 19 |
public static final int PREPARE_SEQUENCE = 0; |
| 20 |
|
| 21 |
/** Shutdown sequence value for client hooks. */ |
| 22 |
public static final int CLIENT_SEQUENCE = 100; |
| 23 |
|
| 24 |
/** Shutdown sequence value for hub hooks. */ |
| 25 |
public static final int HUB_SEQUENCE = 200; |
| 26 |
|
| 27 |
private static final ShutdownManager instance_ = new ShutdownManager(); |
| 28 |
private static final Logger logger_ = |
| 29 |
Logger.getLogger( "org.astrogrid.samp" ); |
| 30 |
|
| 31 |
|
| 32 |
/** Maps Objects to Hooks. */ |
| 33 |
private final WeakHashMap hookMap_; |
| 34 |
|
| 35 |
/** |
| 36 |
* Private constructor prevents instantiation. |
| 37 |
*/ |
| 38 |
private ShutdownManager() { |
| 39 |
hookMap_ = new WeakHashMap(); |
| 40 |
try { |
| 41 |
Runtime.getRuntime() |
| 42 |
.addShutdownHook( new Thread( "SAMP Shutdown" ) { |
| 43 |
public void run() { |
| 44 |
doCleanup(); |
| 45 |
} |
| 46 |
} ); |
| 47 |
} |
| 48 |
catch ( SecurityException e ) { |
| 49 |
logger_.log( Level.WARNING, "Can't add shutdown hook: " + e, e ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register a runnable to be run on shutdown with a given key and sequence. |
| 55 |
* Items with a smaller value of <code>iseq</code> |
| 56 |
* are run earlier at shutdown. |
| 57 |
* Suitable sequence values are given by {@link #HUB_SEQUENCE} and |
| 58 |
* {@link #CLIENT_SEQUENCE}. |
| 59 |
* The <code>key</code> is kept in a WeakHashMap, so if it is GC'd, |
| 60 |
* the runnable will never execute. |
| 61 |
* |
| 62 |
* @param key key which can be used to unregister the hook later |
| 63 |
* @param iseq value indicating position in shutdown sequence |
| 64 |
* @param runnable to be run on shutdown |
| 65 |
*/ |
| 66 |
public synchronized void registerHook( Object key, int iseq, |
| 67 |
Runnable runnable ) { |
| 68 |
hookMap_.put( key, new Hook( runnable, iseq ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Unregisters a key earlier registered using {@link #registerHook}. |
| 73 |
* |
| 74 |
* @param key registration key |
| 75 |
*/ |
| 76 |
public synchronized void unregisterHook( Object key ) { |
| 77 |
hookMap_.remove( key ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Invoked on shutdown by runtime. |
| 82 |
*/ |
| 83 |
private void doCleanup() { |
| 84 |
Hook[] hooks; |
| 85 |
synchronized ( this ) { |
| 86 |
hooks = (Hook[]) hookMap_.values().toArray( new Hook[ 0 ] ); |
| 87 |
} |
| 88 |
Arrays.sort( hooks ); |
| 89 |
logger_.info( "SAMP shutdown start" ); |
| 90 |
for ( int ih = 0; ih < hooks.length; ih++ ) { |
| 91 |
try { |
| 92 |
hooks[ ih ].runnable_.run(); |
| 93 |
} |
| 94 |
catch ( RuntimeException e ) { |
| 95 |
forceLog( logger_, Level.WARNING, "Shutdown hook failure: " + e, |
| 96 |
e ); |
| 97 |
} |
| 98 |
} |
| 99 |
logger_.info( "SAMP shutdown end" ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns sole instance of this class. |
| 104 |
* |
| 105 |
* @return instance |
| 106 |
*/ |
| 107 |
public static ShutdownManager getInstance() { |
| 108 |
return instance_; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Writes a log-like message directly to standard error if it has |
| 113 |
* an appropriate level. |
| 114 |
* This method is only intended for use during the shutdown process, |
| 115 |
* when the logging system may be turned off so that normal logging |
| 116 |
* calls may get ignored (this behaviour is not as far as I know |
| 117 |
* documented, but seems reliable in for example Oracle JRE1.5). |
| 118 |
* There may be some good reason for logging services to be withdrawn |
| 119 |
* during shutdown, so it's not clear that using this method is |
| 120 |
* a good idea at all even apart from bypassing the logging system; |
| 121 |
* therefore use it sparingly. |
| 122 |
* |
| 123 |
* @param logger logger |
| 124 |
* @param level level of message to log |
| 125 |
* @param msg text of logging message |
| 126 |
* @param error associated throwable if any; may be null |
| 127 |
*/ |
| 128 |
public static void forceLog( Logger logger, Level level, String msg, |
| 129 |
Throwable error ) { |
| 130 |
if ( logger.isLoggable( level ) ) { |
| 131 |
System.err.println( level + ": " + msg ); |
| 132 |
if ( error != null ) { |
| 133 |
error.printStackTrace( System.err ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Aggregates a runnable and an associated sequence value. |
| 140 |
*/ |
| 141 |
private static class Hook implements Comparable { |
| 142 |
final Runnable runnable_; |
| 143 |
final int iseq_; |
| 144 |
|
| 145 |
/** |
| 146 |
* Constructor. |
| 147 |
* |
| 148 |
* @param runnable runnable |
| 149 |
* @param iseq sequence value |
| 150 |
*/ |
| 151 |
Hook( Runnable runnable, int iseq ) { |
| 152 |
runnable_ = runnable; |
| 153 |
iseq_ = iseq; |
| 154 |
} |
| 155 |
|
| 156 |
public int compareTo( Object other ) { |
| 157 |
return this.iseq_ - ((Hook) other).iseq_; |
| 158 |
} |
| 159 |
} |
| 160 |
} |