KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > TextFileServletContextListener


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.web.servlets;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import javax.management.JMException JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletContextEvent JavaDoc;
32 import javax.servlet.ServletContextListener JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.mx.util.MBeanServerLocator;
36
37 /**
38  * ServletContextListener class that creates a plain text file
39  * in the jboss tmp directory
40  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
41  * @since Jan 10, 2006
42  * @version $Revision: 43300 $
43  */

44 public class TextFileServletContextListener implements ServletContextListener JavaDoc
45 {
46    private static final Logger log = Logger.getLogger(TextFileServletContextListener.class);
47    
48    private File JavaDoc location = null;
49    
50    public void contextInitialized(ServletContextEvent JavaDoc event)
51    {
52       log.debug("ContextInitialized");
53       ServletContext JavaDoc ctx = event.getServletContext();
54       try
55       {
56          location = this.getTmpLocation();
57       }catch(JMException JavaDoc e)
58       {
59          log.error(e);
60          throw new RuntimeException JavaDoc("Error locating tmp file loc");
61       }
62       
63       String JavaDoc name = ctx.getServletContextName();
64       //Delete any old files that exist
65
File JavaDoc file = new File JavaDoc(location ,name + ".txt");
66       if(file.exists())
67          file.delete();
68    }
69
70    public void contextDestroyed(ServletContextEvent JavaDoc event)
71    {
72       log.debug("ContextDestroyed");
73       ServletContext JavaDoc ctx = event.getServletContext();
74       String JavaDoc name = ctx.getServletContextName();
75
76       // Test for classloading in destroy context
77
ClassLoader JavaDoc 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 JavaDoc e)
84       {
85          log.error("Cannot load class", e);
86          throw new RuntimeException JavaDoc(e.toString());
87       }
88
89       //Create a text file
90
String JavaDoc fileName = name + ".txt";
91       File JavaDoc file = new File JavaDoc(location, fileName);
92       if(file.exists())
93          throw new RuntimeException JavaDoc(fileName + " should not exist");
94       try
95       {
96          file.createNewFile();
97       }
98       catch (IOException JavaDoc e)
99       {
100          log.error(e);
101          throw new RuntimeException JavaDoc(fileName + " creation failed");
102       }
103    }
104    
105    private File JavaDoc getTmpLocation() throws JMException JavaDoc
106    {
107       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
108       ObjectName JavaDoc oname = new ObjectName JavaDoc("jboss.system:type=ServerConfig");
109       return (File JavaDoc)server.getAttribute(oname,"ServerTempDir");
110    }
111 }
112
Popular Tags