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.log4j113;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.lang.reflect.Method;
027: import java.net.URL;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.ServletException;
035: import javax.servlet.ServletConfig;
036:
037: import org.apache.log4j.Category;
038: import org.apache.log4j.PropertyConfigurator;
039:
040: /** A servlet that validates that it sees the log4j 1.1.3 version of the
041: * Category class on initialization.
042: *
043: * @author Scott.Stark@jboss.org
044: * @version $Revision: 57211 $
045: */
046: public class Log4jServlet extends HttpServlet {
047: private Category log;
048:
049: /**
050: *
051: * @param servletConfig
052: * @throws ServletException
053: */
054: public void init(ServletConfig servletConfig)
055: throws ServletException {
056: super .init(servletConfig);
057: // Validate the log4j env against the 1.1.3 classes
058: try {
059: Class categoryClass = Category.class;
060: // Check that the 1.1.3 assert(boolean, String) method exists
061: Class[] sig = { boolean.class, String.class };
062: Method m = categoryClass.getDeclaredMethod("assert", sig);
063: System.out.println("found assert method: " + m);
064: // Find the log4j.properties file
065: ClassLoader loader = Thread.currentThread()
066: .getContextClassLoader();
067: URL resURL = loader.getResource("log4j.properties");
068: System.out.println("found log4j.properties: " + resURL);
069: PropertyConfigurator config = new PropertyConfigurator();
070: log = Category.getInstance(Log4jServlet.class);
071: config.configure(resURL);
072:
073: // Try to access the java:comp/env context
074: InitialContext ctx = new InitialContext();
075: Context enc = (Context) ctx.lookup("java:comp/env");
076: System.out.println("Was able to lookup ENC");
077: } catch (Throwable t) {
078: throw new ServletException("Log4jServlet init failed", t);
079: }
080: }
081:
082: protected void doGet(HttpServletRequest request,
083: HttpServletResponse response) throws ServletException,
084: IOException {
085: processRequest(request, response);
086: }
087:
088: protected void doPost(HttpServletRequest request,
089: HttpServletResponse response) throws ServletException,
090: IOException {
091: processRequest(request, response);
092: }
093:
094: private void processRequest(HttpServletRequest request,
095: HttpServletResponse response) throws ServletException,
096: IOException {
097: // Try to access the java:comp/env context
098: try {
099: InitialContext ctx = new InitialContext();
100: Context enc = (Context) ctx.lookup("java:comp/env");
101: System.out.println("Was able to lookup ENC");
102: } catch (NamingException e) {
103: throw new ServletException("Failed to lookup ENC", e);
104: }
105:
106: log.info("processRequest, path=" + request.getPathInfo());
107: response.setContentType("text/html");
108: PrintWriter pw = response.getWriter();
109: pw
110: .println("<html><head><title>Log4j1.1.3 test servlet</title></head>");
111: pw.println("<body><h1>Log4j1.1.3 test servlet</h1></body>");
112: pw.println("</html>");
113: }
114: }
|