KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > plugin > JGossipStartUp


1 /*
2  * $$Id: JGossipStartUp.java,v 1.4 2005/06/07 12:32:36 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 19.05.2004
27  *
28  */

29 package org.jresearch.gossip.plugin;
30
31 import java.io.IOException JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 import javax.naming.InitialContext JavaDoc;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.sql.DataSource JavaDoc;
42
43 import org.apache.log.Logger;
44 import org.apache.struts.action.ActionServlet;
45 import org.apache.struts.action.PlugIn;
46 import org.apache.struts.config.ModuleConfig;
47 import org.jresearch.gossip.IConst;
48 import org.jresearch.gossip.am.StrutsConfigurationHelperAction;
49 import org.jresearch.gossip.configuration.Configurator;
50 import org.jresearch.gossip.dao.ForumDAO;
51 import org.jresearch.gossip.dao.UserDAO;
52 import org.jresearch.gossip.exception.SystemException;
53 import org.jresearch.gossip.log.avalon.JGossipLog;
54 import org.jresearch.gossip.mail.MailProcessor;
55 import org.jresearch.gossip.mail.MailQueue;
56 import org.jresearch.gossip.scheduler.Scheduler;
57 import org.jresearch.gossip.tags.userstatus.Ranks;
58 import org.jresearch.gossip.util.MessageProcessor;
59 import org.quartz.SchedulerException;
60
61 /**
62  * @author Bel
63  *
64  */

65 public class JGossipStartUp implements PlugIn, IConst {
66
67     private Scheduler scheduler = null;
68
69     /**
70      *
71      *
72      * @see org.apache.struts.action.PlugIn#destroy()
73      */

74     public void destroy() {
75         try {
76             scheduler.shutdown();
77         } catch (SchedulerException e) {
78             try {
79                 JGossipLog.getInstance().getAppLogger().error(
80                         "Failed to shutdown a cheduler instance", e);
81             } catch (SystemException e1) {
82                 e1.printStackTrace();
83             }
84         }
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet,
91      * org.apache.struts.config.ModuleConfig)
92      */

93     public void init(ActionServlet servlet, ModuleConfig moduleconfig)
94             throws ServletException JavaDoc {
95         Logger log = null;
96         try {
97             log = JGossipLog.getInstance().getAppLogger();
98         } catch (SystemException e) {
99             throw new ServletException JavaDoc(e);
100         }
101         if (log.isInfoEnabled()) {
102             log.info("init is started");
103         }
104         ServletContext JavaDoc app = servlet.getServletContext();
105         // start check for avaible translations for application messages
106
Locale JavaDoc[] list = Locale.getAvailableLocales();
107         ArrayList JavaDoc avaibleTranslations = new ArrayList JavaDoc();
108         for (int i = 0; i < list.length; i++) {
109             ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
110                     "org/jresearch/gossip/resources/lang/lang", list[i]);
111             String JavaDoc curr = bundle.getLocale().toString();
112             if (i == 0 || !avaibleTranslations.contains(curr)) {
113                 avaibleTranslations.add(curr);
114             }
115         }
116         if (log.isDebugEnabled()) {
117             log
118                     .debug("avaible translations :"
119                             + avaibleTranslations.toString());
120         }
121         app.setAttribute(IConst.CONTEXT.AVAIBLE_TRANSLATIONS,
122                 avaibleTranslations);
123         // end
124

125         app.setAttribute(IConst.CONTEXT.LAST_UPDATE_DATE, new Date JavaDoc());
126         app.setAttribute(IConst.CONTEXT.MAIL_QUEUE, new MailQueue(
127                 new MailProcessor()));
128         DataSource JavaDoc ds;
129         // Setting Up Datasource
130
try {
131             ds = setupDataSource();
132             ForumDAO dao = ForumDAO.getInstance();
133             dao.setDataSource(ds);
134             UserDAO udao = UserDAO.getInstance();
135             udao.setDataSource(ds);
136         } catch (IOException JavaDoc e) {
137             throw new ServletException JavaDoc(e);
138         } catch (Exception JavaDoc e) {
139             throw new ServletException JavaDoc(e);
140         }
141         // Setting up MessageProcessor
142
MessageProcessor.setEmoticonsMap(ResourceBundle
143                 .getBundle("org/jresearch/gossip/resources/emoticon"));
144         // Load configuration parameters
145
Configurator conf = Configurator.getInstance();
146         conf.setDataSource(ds);
147         try {
148             conf.load(app);
149         } catch (SQLException JavaDoc e) {
150             if (log.isErrorEnabled()) {
151                 log.error("jGossip Configurator is not loaded");
152             }
153             throw new ServletException JavaDoc(e);
154         } catch (IOException JavaDoc e) {
155             if (log.isErrorEnabled()) {
156                 log.error("jGossip Configurator is not loaded");
157             }
158             throw new ServletException JavaDoc(e);
159         } catch (SystemException e) {
160             throw new ServletException JavaDoc(e);
161         }
162         // Get the available struts configuration action mappings
163
try {
164             StrutsConfigurationHelperAction.retrieveStrutsActionMapping(app);
165         } catch (SystemException e) {
166             throw new ServletException JavaDoc(e);
167         }
168         try {
169             // Load Post counted Ranks
170
Ranks.getInstance().load();
171         } catch (SystemException e) {
172             throw new ServletException JavaDoc(e);
173         }
174         try {
175             // Instantiate a scheduler here and load Jobs settings
176
scheduler = initScheduler();
177         } catch (java.security.AccessControlException JavaDoc e) {
178             // TODO
179
} catch (IOException JavaDoc e) {
180             throw new ServletException JavaDoc(e);
181         } catch (SchedulerException e) {
182             throw new ServletException JavaDoc(e);
183         }
184     }
185
186     // Create DataSource and store it in application context
187
private DataSource JavaDoc setupDataSource() throws IOException JavaDoc, Exception JavaDoc {
188         InitialContext JavaDoc ic = new InitialContext JavaDoc();
189         DataSource JavaDoc ds = (DataSource JavaDoc) ic.lookup("jgossip_db");
190         return ds;
191     }
192
193     /**
194      *
195      *
196      * @return
197      * @throws SchedulerException
198      */

199     private Scheduler initScheduler() throws SchedulerException, IOException JavaDoc {
200         Scheduler sched = new Scheduler();
201         sched.start();
202         return sched;
203     }
204 }
Popular Tags