001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.io.File;
025: import java.io.IOException;
026:
027: import javax.management.JMException;
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletContextEvent;
032: import javax.servlet.ServletContextListener;
033:
034: import org.jboss.logging.Logger;
035: import org.jboss.mx.util.MBeanServerLocator;
036:
037: /**
038: * ServletContextListener class that creates a plain text file
039: * in the jboss tmp directory
040: * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
041: * @since Jan 10, 2006
042: * @version $Revision: 57211 $
043: */
044: public class TextFileServletContextListener implements
045: ServletContextListener {
046: private static final Logger log = Logger
047: .getLogger(TextFileServletContextListener.class);
048:
049: private File location = null;
050:
051: public void contextInitialized(ServletContextEvent event) {
052: log.debug("ContextInitialized");
053: ServletContext ctx = event.getServletContext();
054: try {
055: location = this .getTmpLocation();
056: } catch (JMException e) {
057: log.error(e);
058: throw new RuntimeException("Error locating tmp file loc");
059: }
060:
061: String name = ctx.getServletContextName();
062: //Delete any old files that exist
063: File file = new File(location, name + ".txt");
064: if (file.exists())
065: file.delete();
066: }
067:
068: public void contextDestroyed(ServletContextEvent event) {
069: log.debug("ContextDestroyed");
070: ServletContext ctx = event.getServletContext();
071: String name = ctx.getServletContextName();
072:
073: // Test for classloading in destroy context
074: ClassLoader cl = Thread.currentThread().getContextClassLoader();
075: log.info("contextDestroyed cl=" + cl);
076: try {
077: cl
078: .loadClass("org.jboss.test.web.servlets.ContextDestroyed");
079: } catch (ClassNotFoundException e) {
080: log.error("Cannot load class", e);
081: throw new RuntimeException(e.toString());
082: }
083:
084: //Create a text file
085: String fileName = name + ".txt";
086: File file = new File(location, fileName);
087: if (file.exists())
088: throw new RuntimeException(fileName + " should not exist");
089: try {
090: file.createNewFile();
091: } catch (IOException e) {
092: log.error(e);
093: throw new RuntimeException(fileName + " creation failed");
094: }
095: }
096:
097: private File getTmpLocation() throws JMException {
098: MBeanServer server = MBeanServerLocator.locateJBoss();
099: ObjectName oname = new ObjectName(
100: "jboss.system:type=ServerConfig");
101: return (File) server.getAttribute(oname, "ServerTempDir");
102: }
103: }
|