View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight;
25  
26  import com.mindtree.techworks.insight.pagination.PageSet;
27  import com.mindtree.techworks.insight.preferences.PreferenceManager;
28  
29  
30  /**
31   * This class manages all actions that need to be performed when the application
32   * is being closed. All possible tasks are defined as constants in this class, 
33   * and the class exposes public methods to register tasks that need to be 
34   * performed on application shutdown.
35   * 
36   * <p>
37   * The class registers a thread which monitors the runtime with a shutdown
38   * hook, and calls a method in this class when the runtime is going down. The
39   * method <code>#shutDown</code> checks for all registered actions requested (if
40   * any) and executes them.
41   * </p>
42   * 
43   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
44   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
45   */
46  public final class ShutdownHookManager {
47  
48  	// -------------------------------------------------------------------------
49  	// Constants
50  	// -------------------------------------------------------------------------
51  
52  	/**
53  	 * Action identifier to call a save on the Preference Manager before the
54  	 * application is closed.
55  	 */
56  	public static final int SHUTDOWN_ACTION_SAVE_PREFERENCES = 1;
57  
58  	/**
59  	 * Action identifier to call a clear on the PageSet, to clear the paginated
60  	 * data before the application is closed.
61  	 */
62  	public static final int SHUTDOWN_ACTION_CLEAR_PAGES = 2;
63  
64  	// -------------------------------------------------------------------------
65  	// Class fields
66  	// -------------------------------------------------------------------------
67  
68  	/**
69  	 * Store of all actions that need to be performed on application shutdown.
70  	 */
71  	protected static int SHUTDOWN_ACTION;
72  
73  	// -------------------------------------------------------------------------
74  	// Public methods
75  	// -------------------------------------------------------------------------
76  
77  	/**
78  	 * Register an action to be performed on shutdown of the application. The
79  	 * calling class can send in any value in the parameter, but only those
80  	 * actions defined in this class will be performed.
81  	 * 
82  	 * @param shutdownAction
83  	 *            The action identifier or a union of those for multiple
84  	 *            actions.
85  	 */
86  	public static void addShutdownAction (int shutdownAction) {
87  
88  		SHUTDOWN_ACTION = SHUTDOWN_ACTION | shutdownAction;
89  	}
90  
91  	// -------------------------------------------------------------------------
92  	// Private methods
93  	// -------------------------------------------------------------------------
94  
95  	/**
96  	 * Method called when the runtime is going down, all actions registered will
97  	 * be checked and performed.
98  	 */
99  	protected static void shutDown () {
100 
101 		// Save Preferences
102 		if ((SHUTDOWN_ACTION & SHUTDOWN_ACTION_SAVE_PREFERENCES) 
103 				== SHUTDOWN_ACTION_SAVE_PREFERENCES) {
104 			PreferenceManager.getInstance ().applicationShuttingDown ();
105 		}
106 
107 		// Clear Pagesets
108 		if ((SHUTDOWN_ACTION & SHUTDOWN_ACTION_CLEAR_PAGES) 
109 				== SHUTDOWN_ACTION_CLEAR_PAGES) {
110 			PageSet.clearAll ();
111 		}
112 	}
113 
114 	/**
115 	 * Registers a shutdown hook for the applicatoin.
116 	 */
117 	static {
118 		/*
119 		 * This will initializes the shutdown hook.A shutdown hook is an
120 		 * initialized thread that has not yet been executed which helps to gain
121 		 * control of the application when it is shutting down (Even on press of
122 		 * CTRL+C at the console.
123 		 */
124 		Runtime.getRuntime ().addShutdownHook (new Thread () {
125 
126 			public void run () {
127 
128 				shutDown ();
129 			}
130 		});
131 	}
132 }