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.classloader.scoping.override.web.comlog;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.io.File;
027: import java.util.Date;
028: import javax.servlet.http.HttpServlet;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.ServletConfig;
032: import javax.servlet.ServletException;
033: import javax.naming.InitialContext;
034: import javax.naming.Context;
035: import javax.naming.NamingException;
036:
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.commons.logging.Log;
039:
040: /** A servlet that validates that it sees the application specific log4j output
041: * in the ${jboss.server.log.dir}/cl-test.log increasing. The logging interface
042: * used is the commons-logging LogFactory/Log.
043: *
044: * @author Scott.Stark@jboss.org
045: * @version $Revision: 60485 $
046: */
047: public class Log4jServlet extends HttpServlet {
048: private static final long serialVersionUID = 1;
049: private static Log log = LogFactory.getLog(Log4jServlet.class);
050:
051: /**
052: *
053: * @param servletConfig
054: * @throws javax.servlet.ServletException
055: */
056: public void init(ServletConfig servletConfig)
057: throws ServletException {
058: super .init(servletConfig);
059: log.info("init, servletConfig=" + servletConfig);
060: System.out.println("Log class: " + log.getClass());
061: }
062:
063: protected void doGet(HttpServletRequest request,
064: HttpServletResponse response) throws ServletException,
065: IOException {
066: processRequest(request, response);
067: }
068:
069: protected void doPost(HttpServletRequest request,
070: HttpServletResponse response) throws ServletException,
071: IOException {
072: processRequest(request, response);
073: }
074:
075: private void processRequest(HttpServletRequest request,
076: HttpServletResponse response) throws ServletException,
077: IOException {
078: log.info("processRequest, path=" + request.getPathInfo());
079: try {
080: InitialContext ctx = new InitialContext();
081: Context enc = (Context) ctx.lookup("java:comp/env");
082: log.info("Was able to lookup ENC, " + enc);
083: } catch (NamingException e) {
084: throw new ServletException("Failed to lookup ENC", e);
085: }
086:
087: // Validate that the log exists. If the log-name param is null use cl-test.log
088: String logName = request.getParameter("log-name");
089: if (logName == null)
090: logName = "cl-test.log";
091: String logDir = System.getProperty("jboss.server.log.dir");
092: File logFile = new File(logDir, logName);
093: System.err.println("Checking for logfile: "
094: + logFile.getAbsolutePath());
095: if (logFile.exists() == false)
096: throw new ServletException(logFile.getAbsolutePath()
097: + " does not exist");
098:
099: long length = logFile.length();
100: log.info("Current length = " + length);
101: for (int n = 0; n < 100; n++)
102: log.info("Msg #" + n);
103: long lastModified = logFile.lastModified();
104: long length2 = logFile.length();
105: if (!(length2 > length))
106: throw new ServletException(logFile
107: + " length is not increasing");
108: response.setContentType("text/html");
109: PrintWriter pw = response.getWriter();
110: pw
111: .println("<html><head><title>Commons logging test servlet</title></head>");
112: pw.println("<body><h1>Commons logging test servlet</h1>");
113: pw.println("Log length: " + length2);
114: pw.println(", LastModified: " + new Date(lastModified));
115: pw.println("</body></html>");
116: pw.flush();
117: }
118: }
|