001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/service/impl/MvnCoreLifeCycleServiceImplDefault.java,v 1.16 2008/01/21 07:07:54 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.16 $
005: * $Date: 2008/01/21 07:07:54 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Dung Bui
032: */
033: package net.myvietnam.mvncore.service.impl;
034:
035: import javax.servlet.ServletContextEvent;
036:
037: import net.myvietnam.mvncore.db.DBUtils;
038: import net.myvietnam.mvncore.service.MvnCoreLifeCycleService;
039: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
040: import net.myvietnam.mvncore.util.AssertionUtil;
041: import net.myvietnam.mvncore.util.TimerUtil;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: import com.whirlycott.cache.CacheException;
047: import com.whirlycott.cache.CacheManager;
048:
049: public class MvnCoreLifeCycleServiceImplDefault implements
050: MvnCoreLifeCycleService {
051:
052: private static Log log = LogFactory
053: .getLog(MvnCoreLifeCycleServiceImplDefault.class);
054:
055: private static boolean called;
056:
057: private static int count;
058:
059: public MvnCoreLifeCycleServiceImplDefault() {
060: count++;
061: AssertionUtil.doAssert(count == 1,
062: "Assertion: Must have only one instance.");
063: }
064:
065: public static boolean isCalled() {
066: return called;
067: }
068:
069: public void contextInitialized(ServletContextEvent event) {
070:
071: called = true;
072:
073: // NOTE: the below code is commented out, but the idea is to automatically detect the context path
074: // ServletContext context = event.getServletContext();
075: // String contextPath = null;
076: // String realpath = context.getRealPath("/WEB-INF");
077: // ParamUtil.setContextPath(contextPath);
078: // log.info("ParamUtil.setContextPath called with contextPath = " + contextPath);
079:
080: MvnCoreServiceFactory.getMvnCoreService()
081: .getEnvironmentService().overloadEnvironment();
082:
083: }
084:
085: public void contextDestroyed(ServletContextEvent event) {
086:
087: // cancel the TimerUtil
088: log.debug("About to cancel the TimerUtil");
089: TimerUtil.getInstance().cancel();
090:
091: try {
092: log.debug("About to shutdown the Whirly CacheManager");
093: CacheManager.getInstance().shutdown();
094: } catch (CacheException e) {
095: // ignore this
096: }
097:
098: // This code will release all connections currently pooled.
099: // The next call to #getConnection will recreate the pool.
100: DBUtils.closeAllConnections();
101:
102: // Note that we could consider to sleep 5 seconds to make sure all the background tasks is finished
103:
104: }
105: }
|