001 /**
002 *
003 * $Revision: 13250 $ $Date: 2008-03-06 12:38:13 +0100 $
004 *
005 * This file is part of ** M y C o R e **
006 * Visit our homepage at http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, normally in the file license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 *
023 **/
024 package org.mycore.common.events;
025
026 import java.beans.Introspector;
027 import java.util.Collections;
028 import java.util.HashSet;
029 import java.util.Iterator;
030 import java.util.Set;
031
032 import org.apache.log4j.LogManager;
033 import org.apache.log4j.Logger;
034
035 import org.mycore.common.MCRConfiguration;
036 import org.mycore.common.MCRSessionMgr;
037
038 /**
039 * is a wrapper for shutdown hooks.
040 *
041 * When used inside a web application this shutdown hook is bound to the
042 * ServletContext. If not this hook is bound to the Java Runtime.
043 *
044 * Every <code>Closeable</code> that is added via <code>addCloseable()</code>
045 * will be closed at shutdown time. Do not forget to remove any closeable via
046 * <code>removeCloseable()</code> to remove any instances.
047 *
048 * For registering this hook for a web application see <code>MCRServletContextListener</code>
049 *
050 * @author Thomas Scheffler (yagee)
051 *
052 * @see org.mycore.common.events.MCRShutdownThread
053 * @see org.mycore.common.events.MCRServletContextListener
054 * @since 1.3
055 */
056 public class MCRShutdownHandler {
057
058 /**
059 * Object is cleanly closeable via <code>close()</code>-call.
060 *
061 * @author Thomas Scheffler (yagee)
062 */
063 public static interface Closeable {
064 /**
065 * cleanly closes this object that implements <code>Closeable</code>.
066 *
067 * You can provide some functionality to close open files and sockets or
068 * so.
069 */
070 public void close();
071 }
072
073 private static MCRShutdownHandler SINGLETON = new MCRShutdownHandler();
074
075 private static Logger LOGGER = Logger.getLogger(MCRShutdownHandler.class);
076
077 private static final Set<Closeable> requests = Collections.synchronizedSet(new HashSet<Closeable>());
078
079 private static final String system = MCRConfiguration.instance().getString("MCR.CommandLineInterface.SystemName", "MyCoRe") + ":";
080
081 private static boolean shuttingDown = false;
082
083 boolean isWebAppRunning;
084
085 private MCRShutdownHandler() {
086 isWebAppRunning = false;
087 }
088
089 private void init() {
090 if (!isWebAppRunning) {
091 MCRShutdownThread.getInstance();
092 }
093 }
094
095 public static MCRShutdownHandler getInstance() {
096 return SINGLETON;
097 }
098
099 public void addCloseable(MCRShutdownHandler.Closeable c) {
100 init();
101 requests.add(c);
102 }
103
104 public void removeCloseable(MCRShutdownHandler.Closeable c) {
105 if (!shuttingDown)
106 requests.remove(c);
107 }
108
109 void shutDown() {
110 System.out.println(system+" Shutting down system, please wait...\n");
111 LOGGER.debug("requests: " + requests.toString());
112 synchronized (requests) {
113 shuttingDown = true;
114 for (Iterator<Closeable> it = requests.iterator(); it.hasNext();) {
115 MCRShutdownHandler.Closeable c = it.next();
116 LOGGER.debug("Closing: " + c.toString());
117 c.close();
118 it.remove();
119 }
120 }
121 System.out.println(system+" closing any remaining MCRSession instances, please wait...\n");
122 MCRSessionMgr.close();
123 System.out.println(system + " Goodbye, and remember: \"Alles wird gut.\"\n");
124 LogManager.shutdown();
125 //may be needed in webapp to release file handles correctly.
126 Introspector.flushCaches();
127 }
128
129 }