001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/service/impl/MvnForumLifeCycleServiceImplDefault.java,v 1.17 2007/10/09 11:09:21 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.17 $
005: * $Date: 2007/10/09 11:09:21 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Phong Ta Quoc
039: */
040: package com.mvnforum.service.impl;
041:
042: import java.util.*;
043:
044: import javax.servlet.ServletContext;
045: import javax.servlet.ServletContextEvent;
046:
047: import net.myvietnam.mvncore.MVNCoreConfig;
048: import net.myvietnam.mvncore.db.DBUtils;
049: import net.myvietnam.mvncore.exception.DatabaseException;
050: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
051: import net.myvietnam.mvncore.info.DatabaseInfo;
052: import net.myvietnam.mvncore.service.EnvironmentService;
053: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
054: import net.myvietnam.mvncore.util.*;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058:
059: import com.mvnforum.MVNForumConfig;
060: import com.mvnforum.MVNForumConstant;
061: import com.mvnforum.common.*;
062: import com.mvnforum.db.DAOFactory;
063: import com.mvnforum.db.WatchBean;
064: import com.mvnforum.service.MvnForumLifeCycleService;
065:
066: public class MvnForumLifeCycleServiceImplDefault implements
067: MvnForumLifeCycleService {
068:
069: private static Log log = LogFactory
070: .getLog(MvnForumLifeCycleServiceImplDefault.class);
071:
072: private static boolean called;
073:
074: private static int count;
075:
076: public MvnForumLifeCycleServiceImplDefault() {
077: count++;
078: AssertionUtil.doAssert(count == 1,
079: "Assertion: Must have only one instance.");
080: }
081:
082: public static boolean isCalled() {
083: return called;
084: }
085:
086: public void contextInitialized(ServletContextEvent event) {
087:
088: log.debug("Begin calling contextInitialized()");
089: called = true;
090:
091: EnvironmentService environmentService = MvnCoreServiceFactory
092: .getMvnCoreService().getEnvironmentService();
093: ServletContext context = event.getServletContext();
094:
095: // Check if the servlet is in specs Servlet 2.3 or later
096: if (environmentService.isShouldRun()) {
097: int majorVersion = context.getMajorVersion();
098: int mimorVersion = context.getMinorVersion();
099: if ((majorVersion < 2)
100: || ((majorVersion == 2) && (mimorVersion < 3))) {
101: environmentService
102: .setShouldRun(
103: false,
104: "mvnForum requires Servlet 2.3 or later. Please upgrade your Servlet Container.");
105: return;
106: }
107: }
108:
109: // now check the database
110: try {
111: DatabaseInfo databaseInfo = new DatabaseInfo();
112: if (databaseInfo.getErrorMessage() != null) {
113: log
114: .fatal("Cannot get database connection. Please correct it first.");
115: environmentService.setShouldRun(false,
116: "Check your database configuration. Detail : "
117: + databaseInfo.getErrorMessage());
118: } else if (databaseInfo.getDatabaseUrl().toLowerCase()
119: .startsWith("jdbc:odbc:")) {
120: log
121: .fatal("Does not support JDBC/ODBC driver. Please use other drivers.");
122: environmentService
123: .setShouldRun(false,
124: "Does not support JDBC/ODBC driver. Please use other drivers.");
125: } else if (DAOFactory.getMemberDAO().getNumberOfMembers() == 0) { // check if no member
126: log
127: .fatal("There are no members in database. Please correct it first.");
128: environmentService.setShouldRun(false,
129: "There are no members in database.");
130: }
131: // call this method will print the database type to logger with level INFO
132: DBUtils.getDatabaseType();
133:
134: // now check if Guest user is in database or not
135: try {
136: DAOFactory.getMemberDAO().getMember(
137: MVNForumConstant.MEMBER_ID_OF_GUEST);
138: MVNForumConfig.setGuestUserInDatabase(true);
139: } catch (ObjectNotFoundException ex) {
140: // dont have Guest user in database, just ignore
141: } catch (Exception ex) {
142: log.info("Error occured when get Guest user.", ex);
143: }
144: } catch (DatabaseException dbe) {
145: log
146: .fatal(
147: "Error while access database. Please correct it first.",
148: dbe);
149: environmentService.setShouldRun(false,
150: "Error while access database. Detail : "
151: + dbe.getMessage());
152: }
153:
154: // schedule the WatchSendTask
155: log.debug("Schedule the WatchSendTask");
156: if (MVNForumConfig.getEnableWatch()) {
157: if (environmentService.isShouldRun()) {
158: log.info("Schedule the WatchSendTask for send mail");
159: if (MVNForumConfig.getDefaultWatchOption() == WatchBean.WATCH_OPTION_LIVE) {
160: // The default watch is LIVE, so the timer is called more often (5 minutes)
161: WatchSendTask.getInstance().schedule(
162: DateUtil.MINUTE, DateUtil.MINUTE * 5);
163: } else {
164: // Other options, we only check the watch hourly
165: WatchSendTask.getInstance().schedule(
166: DateUtil.MINUTE, DateUtil.HOUR);
167: }
168: }
169: } else {
170: log
171: .info("Watch is disabled. Do not schedule the WatchSendTask.");
172: }
173:
174: // schedule the DeleteOrphanPmAttachmentTask
175: if (environmentService.isShouldRun()) {
176: log.info("Schedule the DeleteOrphanPmAttachmentTask.");
177: if (MVNForumConfig.getEnableMessageAttachment()) {
178: //Repeated task
179: DeleteOrphanPmAttachmentTask.getInstance().schedule(
180: DateUtil.MINUTE, DateUtil.HOUR);
181: } else {
182: // Try to delete the PmAttachment at least once time
183: DeleteOrphanPmAttachmentTask.getInstance().schedule(
184: DateUtil.MINUTE);
185: }
186: }
187:
188: List timerTaskExtList = MVNCoreConfig.getTimerTaskExtList();
189: Iterator iter = timerTaskExtList.iterator();
190:
191: while (iter.hasNext()) {
192: TimerTaskExt timerTask = (TimerTaskExt) iter.next();
193: timerTask.schedule();
194: }
195: }
196:
197: public void contextDestroyed(ServletContextEvent event) {
198: log.debug("Begin calling contextDestroyed()");
199: }
200:
201: }
|