1 package org.columba.core.shutdown; 19 20 import java.awt.Component ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.logging.Logger ; 25 26 import javax.swing.JFrame ; 27 import javax.swing.JOptionPane ; 28 29 import org.columba.api.shutdown.IShutdownManager; 30 import org.columba.core.backgroundtask.BackgroundTaskManager; 31 import org.columba.core.command.Command; 32 import org.columba.core.command.CommandProcessor; 33 import org.columba.core.command.TaskManager; 34 import org.columba.core.logging.Logging; 35 import org.columba.core.main.ColumbaServer; 36 import org.columba.core.resourceloader.GlobalResourceLoader; 37 38 73 public class ShutdownManager implements IShutdownManager { 74 75 private static final Logger LOG = Logger 76 .getLogger("org.columba.core.shutdown"); 77 78 protected static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 79 80 83 private static IShutdownManager instance; 84 85 89 private boolean shutdownHook = false; 90 91 94 protected final Thread shutdownThread; 95 96 99 protected List list = new LinkedList (); 100 101 private boolean shuttingDown; 102 103 107 protected ShutdownManager() { 108 shutdownThread = new Thread (new Runnable () { 109 110 public void run() { 111 BackgroundTaskManager.getInstance().stop(); 114 115 CommandProcessor.getInstance().stop(); 116 117 while (!isShutdownHook() 118 && (TaskManager.getInstance().count() > 0)) { 119 Object [] options = { 121 GlobalResourceLoader.getString(RESOURCE_PATH, 122 "session", "tasks_wait"), 123 GlobalResourceLoader.getString(RESOURCE_PATH, 124 "session", "tasks_exit") }; 125 int n = JOptionPane.showOptionDialog(null, 126 GlobalResourceLoader.getString(RESOURCE_PATH, 127 "session", "tasks_msg"), 128 GlobalResourceLoader.getString(RESOURCE_PATH, 129 "session", "tasks_title"), 130 JOptionPane.YES_NO_OPTION, 131 JOptionPane.QUESTION_MESSAGE, null, options, 132 options[0]); 133 134 if (n == 0) { 135 for (int i = 0; i < 10; i++) { 138 try { 139 Thread.sleep(1000); 140 } catch (InterruptedException ie) { 141 } 142 } 143 } else { 144 break; 147 } 148 } 149 150 ShutdownDialog dialog = (ShutdownDialog) openShutdownDialog(); 151 152 Iterator iterator = list.iterator(); 153 Runnable plugin; 154 155 while (iterator.hasNext()) { 156 plugin = (Runnable ) iterator.next(); 157 158 try { 159 plugin.run(); 160 } catch (Exception e) { 161 LOG.severe(e.getMessage()); 162 163 } 165 } 166 167 if (dialog != null) 170 dialog.close(); 171 } 172 }, "ShutdownManager"); 173 setShutdownHook(true); 174 175 shuttingDown = false; 176 } 177 178 183 public void register(Runnable plugin) { 184 list.add(0, plugin); 185 } 186 187 191 public synchronized boolean isShutdownHook() { 192 return shutdownHook; 193 } 194 195 199 protected synchronized void setShutdownHook(boolean b) { 200 if (shutdownHook == b) { 201 return; 202 } 203 204 if (b) { 205 Runtime.getRuntime().addShutdownHook(shutdownThread); 206 } else { 207 Runtime.getRuntime().removeShutdownHook(shutdownThread); 208 } 209 210 shutdownHook = b; 211 } 212 213 218 public synchronized void shutdown(final int status) { 219 if (!shuttingDown) { 220 setShutdownHook(false); 221 new Thread (new Runnable () { 222 223 public void run() { 224 shutdownThread.run(); 225 System.exit(status); 226 } 227 }, "ShutdownManager").start(); 228 229 shuttingDown = true; 230 } 231 } 232 233 236 protected Component openShutdownDialog() { 237 JFrame dialog = null; 238 try { 239 dialog = new ShutdownDialog(); 240 } catch (Exception e) { 241 if (Logging.DEBUG) 242 e.printStackTrace(); 243 } 244 return dialog; 245 } 246 247 250 public static synchronized IShutdownManager getInstance() { 251 if (instance == null) { 252 instance = new ShutdownManager(); 253 } 254 255 return instance; 256 } 257 } 258 | Popular Tags |