1 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 40 public class InitApplicationServlet extends HttpServlet { 41 private static Logger logger = LogManager.getLogger(InitApplicationServlet.class); 42 private String appConfigPath = ""; 43 44 public void init() throws ServletException { 45 String appPath = this.getServletConfig().getServletContext() 48 .getRealPath("/"); 49 50 appPath = appPath.substring(0, appPath.length() - 1); 52 53 appConfigPath = appPath + "-projects/WEB-INF/application.xml"; 56 57 restoreApplication(); 59 60 watchConfigFileChange(); 62 63 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 String xsl = this.getServletConfig().getServletContext().getRealPath( 71 "/") 72 + "WEB-INF/project/project.xsl"; 73 Application.getInstance().setProjectXsl(xsl); 74 } 75 76 public void doGet(HttpServletRequest request, HttpServletResponse response) 78 throws ServletException, IOException { 79 } 80 81 public void doPost(HttpServletRequest request, HttpServletResponse response) 83 throws ServletException, IOException { 84 } 85 86 public void destroy() { 88 System.setProperty("javax.xml.soap.SOAPConnectionFactory", null); 90 } 91 92 95 private void restoreApplication() { 96 logger.debug("Trying to restore application :" + appConfigPath); 97 98 try { 99 BeanStorage storage = new BeanStorage(); 101 storage.restoreBeanFromFile(appConfigPath, Application.getInstance()); 102 logger.info("Restored application :" + appConfigPath); 103 104 this.getServletContext() 106 .setAttribute("application", Application.getInstance()); 107 } catch (Exception e) { 108 logger.error("Error while restoring application:" + appConfigPath, e); 109 } 110 } 111 112 115 private void watchConfigFileChange() { 116 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 Timer timer = new Timer(); 128 timer.schedule(task, new Date(), 5000); 129 } 130 } 131 | Popular Tags |