KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > BlogunityManager


1 /*
2  * $Id: BlogunityManager.java,v 1.14 2005/01/17 21:36:31 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity;
27
28 import java.io.File JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletContextEvent JavaDoc;
34 import javax.servlet.ServletContextListener JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.quartz.JobDetail;
39 import org.quartz.Scheduler;
40 import org.quartz.SchedulerException;
41 import org.quartz.SimpleTrigger;
42 import org.quartz.impl.StdSchedulerFactory;
43
44 import com.j2biz.blogunity.dao.SystemConfigurationDAO;
45 import com.j2biz.blogunity.exception.BlogunityException;
46 import com.j2biz.blogunity.exception.BlogunityRuntimeException;
47 import com.j2biz.blogunity.i18n.I18N;
48 import com.j2biz.blogunity.i18n.I18NStatusFactory;
49 import com.j2biz.blogunity.jobs.RefererAndVisitedPagesRemover;
50 import com.j2biz.blogunity.pojo.SystemConfiguration;
51 import com.j2biz.blogunity.util.HibernateUtil;
52
53 /**
54  *
55  * @web.listener
56  * @author michelson
57  * @version $$
58  * @since 0.1
59  */

60 public class BlogunityManager implements ServletContextListener JavaDoc {
61     /**
62      * Logger for this class
63      */

64     private static final Log log = LogFactory.getLog(BlogunityManager.class);
65
66     private static SystemConfiguration blogunityConfiguration = null;
67
68     // private static BlogunityRenderEngine renderEngine;
69

70     private static ServletContext JavaDoc servletContext;
71
72     private static String JavaDoc host;
73
74     private static String JavaDoc base;
75
76     private static String JavaDoc contextPath;
77
78     private static Scheduler scheduler;
79
80     private static Date JavaDoc startupTime;
81
82     public BlogunityManager() {
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
89      */

90     public void contextInitialized(ServletContextEvent JavaDoc e) {
91         if (log.isDebugEnabled()) {
92             log.debug("contextInitialized(ServletContextEvent) - start");
93         }
94
95         try {
96
97             startupTime = new Date JavaDoc();
98
99             servletContext = e.getServletContext();
100
101             initConfiguration();
102
103             // create data-directory for blogs-content
104
if (log.isDebugEnabled()) {
105                 log.debug("Check, if the data-directory for the blog's content exists. "
106                         + "If not, create new one...");
107             }
108             String JavaDoc dataDir = blogunityConfiguration.getDataDir();
109             File JavaDoc _dataDir = new File JavaDoc(dataDir);
110             if (!_dataDir.exists()) {
111
112                 boolean result = _dataDir.mkdir();
113                 if (!result) {
114
115                 throw new BlogunityRuntimeException(I18NStatusFactory.create(
116                         I18N.ERRORS.CREATE_DIRECTORY, new String JavaDoc[]{_dataDir.getAbsolutePath()})); }
117
118             } else {
119                 if (!_dataDir.isDirectory() || !_dataDir.canRead() || !_dataDir.canWrite()) {
120
121                 throw new BlogunityRuntimeException(I18NStatusFactory.create(
122                         I18N.ERRORS.READ_DIRECTORY, new String JavaDoc[]{_dataDir.getAbsolutePath()})); }
123
124             }
125
126             // if not exists, create directories for blogs- and users-data
127
File JavaDoc _blogsDataDir = new File JavaDoc(_dataDir, "blogs");
128             if (!_blogsDataDir.exists()) {
129                 boolean result = _blogsDataDir.mkdir();
130                 if (!result) {
131
132                 throw new BlogunityRuntimeException(I18NStatusFactory
133                         .create(I18N.ERRORS.CREATE_DIRECTORY, new String JavaDoc[]{_blogsDataDir
134                                 .getAbsolutePath()})); }
135             }
136             
137             File JavaDoc _usersDataDir = new File JavaDoc(_dataDir, "users");
138             if (!_usersDataDir.exists()) {
139                 boolean result = _usersDataDir.mkdir();
140                 if (!result) {
141
142                 throw new BlogunityRuntimeException(I18NStatusFactory
143                         .create(I18N.ERRORS.CREATE_DIRECTORY, new String JavaDoc[]{_usersDataDir
144                                 .getAbsolutePath()})); }
145             }
146             
147
148             // create temp-directory
149
if (log.isDebugEnabled()) {
150                 log.debug("Check, if the temp-directory exists. " + "If not, create new one...");
151             }
152             String JavaDoc tempDir = blogunityConfiguration.getTempDir();
153             File JavaDoc _tempDir = new File JavaDoc(tempDir);
154             if (!_tempDir.exists()) {
155
156                 boolean result = _tempDir.mkdir();
157                 if (!result) {
158
159                 throw new BlogunityRuntimeException(I18NStatusFactory.create(
160                         I18N.ERRORS.CREATE_DIRECTORY, new String JavaDoc[]{_tempDir.getAbsolutePath()})); }
161
162             } else {
163                 if (!_tempDir.isDirectory() || !_tempDir.canRead() || !_tempDir.canWrite()) { throw new BlogunityRuntimeException(
164                         I18NStatusFactory.create(I18N.ERRORS.READ_DIRECTORY, new String JavaDoc[]{_tempDir
165                                 .getAbsolutePath()})); }
166             }
167
168             try {
169                 // initialize scheduler
170
scheduler = StdSchedulerFactory.getDefaultScheduler();
171                 scheduler.start();
172
173                 scheduleJobs();
174
175             } catch (SchedulerException e1) {
176
177                 throw new BlogunityRuntimeException(I18NStatusFactory.createUnknown(e1));
178             }
179
180             if (log.isInfoEnabled()) {
181                 log.info("Blogunity started at " + startupTime);
182             }
183
184         } catch (BlogunityException e2) {
185             if (log.isErrorEnabled()) {
186                 log.error("Error loading configuration!", e2);
187             }
188
189             throw new BlogunityRuntimeException(I18NStatusFactory.create(
190                     I18N.ERRORS.LOADING_CONFIGURATION_FAILED, e2));
191         }
192
193         if (log.isDebugEnabled()) {
194             log.debug("contextInitialized(ServletContextEvent) - end");
195         }
196     }
197
198     /**
199      * @throws BlogException
200      *
201      */

202     private void initConfiguration() throws BlogunityException {
203         SystemConfigurationDAO dao = new SystemConfigurationDAO();
204         blogunityConfiguration = dao.getSystemConfiguration();
205
206     }
207
208     private void scheduleJobs() throws SchedulerException {
209
210         // 1. Referer- and VisitedPages-Remover
211
JobDetail jobDetail = new JobDetail("RefererAndVisitedPagesRemover",
212                 Scheduler.DEFAULT_GROUP, RefererAndVisitedPagesRemover.class);
213
214         Date JavaDoc d = new Date JavaDoc();
215         Calendar JavaDoc calendar = Calendar.getInstance();
216         calendar.setTime(d);
217         calendar.add(Calendar.DATE, 1);
218         calendar.set(Calendar.HOUR, 0);
219         calendar.set(Calendar.MINUTE, 0);
220         calendar.set(Calendar.SECOND, 1);
221
222         if (log.isDebugEnabled()) {
223             log.debug("!!!>>>>> Execute RefererAndVisitedPagesRemover for the first time at "
224                     + calendar.getTime());
225         }
226
227         // execute job every 24 hours
228
int millis = 24 * 60 * 60 * 1000;
229         SimpleTrigger trigger = new SimpleTrigger("RefererAndVisitedPagesTrigger",
230                 Scheduler.DEFAULT_GROUP, calendar.getTime(), null,
231                 SimpleTrigger.REPEAT_INDEFINITELY, millis);
232
233         scheduler.scheduleJob(jobDetail, trigger);
234
235     }
236
237     /**
238      * @return Returns the blogunityConfiguration.
239      */

240     public static SystemConfiguration getSystemConfiguration() {
241         return blogunityConfiguration;
242     }
243
244     /*
245      * (non-Javadoc)
246      *
247      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
248      */

249     public void contextDestroyed(ServletContextEvent JavaDoc e) {
250         if (log.isDebugEnabled()) {
251             log.debug("contextDestroyed(ServletContextEvent) - start");
252         }
253
254         // HibernateUtil.disconnectSession();
255
HibernateUtil.closeSession();
256
257         try {
258             //destroy scheduler
259
if (scheduler != null) scheduler.shutdown();
260         } catch (SchedulerException e1) {
261             log.error("Error shutting down scheduler!", e1);
262         }
263
264         if (log.isDebugEnabled()) {
265             log.debug("contextDestroyed(ServletContextEvent) - end");
266         }
267     }
268
269     // public static String getRenderEngineClass() {
270
// return blogunityConfiguration.getRenderEngineClass();
271
//
272
// }
273

274     /**
275      * @return Returns the servletContext.
276      */

277     public static ServletContext JavaDoc getServletContext() {
278         return servletContext;
279     }
280
281     /**
282      * @return Returns the contextPath.
283      */

284     public static String JavaDoc getContextPath() {
285         return contextPath;
286     }
287
288     /**
289      * @param contextPath
290      * The contextPath to set.
291      */

292     public static void setContextPath(String JavaDoc contextPath) {
293         BlogunityManager.contextPath = contextPath;
294     }
295
296     /**
297      * Delegetion to ServletContext's-method getRealPath(). Returns absolute
298      * filesystem path for the requested resource.
299      *
300      * @param path
301      * @return
302      */

303     public static String JavaDoc getRealPath(String JavaDoc path) {
304         return servletContext.getRealPath(path);
305     }
306
307     public static Scheduler getScheduler() {
308         return scheduler;
309     }
310
311     /**
312      * Returns the startup time of the current bloguntiy instance.
313      *
314      * @return
315      */

316     public static Date JavaDoc getStartupTime() {
317         return startupTime;
318     }
319
320     /**
321      * Sets the host of the current installation.
322      *
323      * @param host
324      */

325     public static void setHost(String JavaDoc host) {
326         BlogunityManager.host = host;
327     }
328
329     /**
330      * Returns the virtual host of the current installation. ( e.g.
331      * http://www.mysite.com)
332      *
333      * @return host.
334      */

335     public static String JavaDoc getHost() {
336         return host;
337     }
338
339     /**
340      * Returns base, that represents host-path followed by the name of the
341      * webapp-context. E.g. if host=http://www.mysite.com and
342      * context=/blogunity, than the returned value of the base will be:
343      * base=http://www.mysite.com/blogunity
344      *
345      *
346      * @return
347      */

348     public static String JavaDoc getBase() {
349         if (base == null) {
350             base = getHost() + getContextPath();
351             if (base.endsWith("/")) base = base.substring(0, base.length() - 1);
352         }
353         return base;
354     }
355 }
Popular Tags