KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > application > InitApplicationServlet


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.application;
21
22 import org.apache.log4j.*;
23 import org.openi.util.FileChangeWatcher;
24 import org.openi.xml.BeanStorage;
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30
31 /**
32  * @author Uddhab Pant <br>
33  * @version $Revision: 1.7 $ $Date: 2006/04/12 00:39:11 $ <br>
34  *
35  * Startup servlet responsible for initialization of application config object and
36  * launch of config file change task when the servlet is initialized.
37  * Also responsible for update of application config object.
38  *
39  */

40 public class InitApplicationServlet extends HttpServlet {
41     private static Logger logger = LogManager.getLogger(InitApplicationServlet.class);
42     private String JavaDoc appConfigPath = "";
43    
44     public void init() throws ServletException {
45         //As there is no way to get context path in this method, using a hack.
46
//get real path of application which is something like '/apache/Tomcat 5.5/webapps/mo/
47
String JavaDoc appPath = this.getServletConfig().getServletContext()
48                              .getRealPath("/");
49
50         //removing last character i.e. '/'
51
appPath = appPath.substring(0, appPath.length() - 1);
52
53         //as application.xml file is always located in 'app_name-projects/WEB-INF/'
54
//concatenating '-projects/WEB-INF/application.xml' in appPath
55
appConfigPath = appPath + "-projects/WEB-INF/application.xml";
56
57         //Restore config from file
58
restoreApplication();
59
60         //launch config file change thread.
61
watchConfigFileChange();
62         
63         //setup our SOAPConnectionFactory if Sql2005Compatiblilty is true
64
if(!Application.getInstance().isSql2005Compatiblilty()) {
65             System.setProperty("javax.xml.soap.SOAPConnectionFactory","com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnectionFactory");
66         } else {
67             System.setProperty("javax.xml.soap.SOAPConnectionFactory","org.openi.soap.client.HttpSOAPConnectionFactory");
68         }
69         //setup project xsl to convert old project to new format
70
String JavaDoc xsl = this.getServletConfig().getServletContext().getRealPath(
71                 "/")
72                 + "WEB-INF/project/project.xsl";
73         Application.getInstance().setProjectXsl(xsl);
74     }
75
76     //Process the HTTP Get request
77
public void doGet(HttpServletRequest request, HttpServletResponse response)
78         throws ServletException, IOException {
79     }
80
81     //Process the HTTP Post request
82
public void doPost(HttpServletRequest request, HttpServletResponse response)
83         throws ServletException, IOException {
84     }
85
86     //Clean up resources
87
public void destroy() {
88         //remove soap factory
89
System.setProperty("javax.xml.soap.SOAPConnectionFactory", null);
90     }
91
92     /**
93      * Restores application config from file to object
94      */

95     private void restoreApplication() {
96         logger.debug("Trying to restore application :" + appConfigPath);
97
98         try {
99             //restore config file, initialize application config object and store in ServletContext.
100
BeanStorage storage = new BeanStorage();
101             storage.restoreBeanFromFile(appConfigPath, Application.getInstance());
102             logger.info("Restored application :" + appConfigPath);
103
104             // set application object in servlet context.
105
this.getServletContext()
106                 .setAttribute("application", Application.getInstance());
107         } catch (Exception JavaDoc e) {
108             logger.error("Error while restoring application:" + appConfigPath, e);
109         }
110     }
111
112     /**
113      * Launches config file change thread.
114      */

115     private void watchConfigFileChange() {
116         // monitor config file
117
TimerTask task = new FileChangeWatcher(new File(appConfigPath)) {
118                 protected void onChange(File file) {
119                     logger.info(
120                         "Application config file has been changed. Reloading...");
121                     restoreApplication();
122                     logger.info("Application config reloaded");
123                 }
124             };
125
126         // repeat the check every 5 second
127
Timer timer = new Timer();
128         timer.schedule(task, new Date(), 5000);
129     }
130 }
131
Popular Tags