1 22 package org.jboss.test.web.servlets; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 27 import javax.management.JMException ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletContextEvent ; 32 import javax.servlet.ServletContextListener ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.mx.util.MBeanServerLocator; 36 37 44 public class TextFileServletContextListener implements ServletContextListener 45 { 46 private static final Logger log = Logger.getLogger(TextFileServletContextListener.class); 47 48 private File location = null; 49 50 public void contextInitialized(ServletContextEvent event) 51 { 52 log.debug("ContextInitialized"); 53 ServletContext ctx = event.getServletContext(); 54 try 55 { 56 location = this.getTmpLocation(); 57 }catch(JMException e) 58 { 59 log.error(e); 60 throw new RuntimeException ("Error locating tmp file loc"); 61 } 62 63 String name = ctx.getServletContextName(); 64 File file = new File (location ,name + ".txt"); 66 if(file.exists()) 67 file.delete(); 68 } 69 70 public void contextDestroyed(ServletContextEvent event) 71 { 72 log.debug("ContextDestroyed"); 73 ServletContext ctx = event.getServletContext(); 74 String name = ctx.getServletContextName(); 75 76 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 78 log.info("contextDestroyed cl=" + cl); 79 try 80 { 81 cl.loadClass("org.jboss.test.web.servlets.ContextDestroyed"); 82 } 83 catch (ClassNotFoundException e) 84 { 85 log.error("Cannot load class", e); 86 throw new RuntimeException (e.toString()); 87 } 88 89 String fileName = name + ".txt"; 91 File file = new File (location, fileName); 92 if(file.exists()) 93 throw new RuntimeException (fileName + " should not exist"); 94 try 95 { 96 file.createNewFile(); 97 } 98 catch (IOException e) 99 { 100 log.error(e); 101 throw new RuntimeException (fileName + " creation failed"); 102 } 103 } 104 105 private File getTmpLocation() throws JMException 106 { 107 MBeanServer server = MBeanServerLocator.locateJBoss(); 108 ObjectName oname = new ObjectName ("jboss.system:type=ServerConfig"); 109 return (File )server.getAttribute(oname,"ServerTempDir"); 110 } 111 } 112 | Popular Tags |