File: ExitHookManager.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,000 kB
  • ctags: 3,667
  • sloc: java: 23,109; xml: 414; makefile: 22
file content (252 lines) | stat: -rw-r--r-- 8,074 bytes parent folder | download | duplicates (2)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* 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 sun.misc.Signal;
import sun.misc.SignalHandler;

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)
        {
            if (JRE_1_3_PLUS)
            {
                s_singleton = new JRE13ExitHookManager ();
            }
            else if (JRE_SUN_SIGNAL_COMPATIBLE)
            {
                s_singleton = new SunJREExitHookManager ();
            }
            else
            {
                throw new UnsupportedOperationException ("no shutdown hook manager available [JVM: " + Property.getSystemFingerprint () + "]");
            }
        }
        
        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 final class SunJREExitHookManager extends ExitHookManager
    {
        public synchronized boolean addExitHook (final Runnable runnable)
        {
            if ((runnable != null) && ! m_signalHandlerMap.containsKey (runnable))
            {
                final INTSignalHandler handler = new INTSignalHandler (runnable);
                
                try
                {
                    handler.register ();
                    m_signalHandlerMap.put (runnable, handler); // TODO: use identity here
                    
                    return true;
                }
                catch (Throwable t)
                {
                    System.out.println ("exception caught while adding a shutdown hook:");
                    t.printStackTrace (System.out);
                }
            }
            
            return false;
        }
        
        public synchronized boolean removeExitHook (final Runnable runnable)
        {
            if (runnable != null)
            {
                final INTSignalHandler handler = (INTSignalHandler) m_signalHandlerMap.get (runnable);  // TODO: use identity here
                if (handler != null)
                {
                    try
                    {
                        handler.unregister ();
                        m_signalHandlerMap.remove (runnable);
                        
                        return true;
                    }
                    catch (Exception e)
                    {
                        System.out.println ("exception caught while removing a shutdown hook:");
                        e.printStackTrace (System.out);
                    }
                }
            }
            
            return false;
        }
        
        SunJREExitHookManager ()
        {
            m_signalHandlerMap = new HashMap ();
        }
        
        
        private final Map /* Runnable->INTSignalHandler */ m_signalHandlerMap;
        
    } // end of nested class
    
    
    private static final class INTSignalHandler implements SignalHandler
    {
        public synchronized void handle (final Signal signal)
        {
            if (m_runnable != null)
            {
                try
                {
                    m_runnable.run ();
                }
                catch (Throwable ignore) {}
            }
            m_runnable = null;
            
            if ((m_previous != null) && (m_previous != SIG_DFL) && (m_previous != SIG_IGN))
            {
                try
                {
                    // this does not work:
                    //Signal.handle (signal, m_previous);
                    //Signal.raise (signal);
                    
                    m_previous.handle (signal);
                }
                catch (Throwable ignore) {}
            }
            else
            {
                System.exit (0);
            }
        }
        
        INTSignalHandler (final Runnable runnable)
        {
            m_runnable = runnable;
        }
       
        synchronized void register ()
        {
            m_previous = Signal.handle (new Signal ("INT"), this);
        }
        
        synchronized void unregister ()
        {
//            if (m_previous != null)
//            {
//                Signal.handle (new Signal ("INT"), m_previous);
//                m_previous = null;
//            }

            m_runnable = null;
        }


        private Runnable m_runnable;
        private SignalHandler m_previous;
        
    } // end of nested class
    
    
    private static ExitHookManager s_singleton;
    
} // end of class
// ----------------------------------------------------------------------------