1 22 package org.jboss.test.classloader.scoping.override.web.log4j113; 23 24 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.lang.reflect.Method ; 28 import java.net.URL ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletConfig ; 37 38 import org.apache.log4j.Category; 39 import org.apache.log4j.PropertyConfigurator; 40 41 47 public class Log4jServlet extends HttpServlet 48 { 49 private Category log; 50 51 56 public void init(ServletConfig servletConfig) throws ServletException 57 { 58 super.init(servletConfig); 59 try 61 { 62 Class categoryClass = Category.class; 63 Class [] sig = {boolean.class, String .class}; 65 Method m = categoryClass.getDeclaredMethod("assert", sig); 66 System.out.println("found assert method: "+m); 67 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 69 URL resURL = loader.getResource("log4j.properties"); 70 System.out.println("found log4j.properties: "+resURL); 71 PropertyConfigurator config = new PropertyConfigurator(); 72 log = Category.getInstance(Log4jServlet.class); 73 config.configure(resURL); 74 75 InitialContext ctx = new InitialContext (); 77 Context enc = (Context ) ctx.lookup("java:comp/env"); 78 System.out.println("Was able to lookup ENC"); 79 } 80 catch(Throwable t) 81 { 82 throw new ServletException ("Log4jServlet init failed", t); 83 } 84 } 85 86 protected void doGet(HttpServletRequest request, HttpServletResponse response) 87 throws ServletException , IOException 88 { 89 processRequest(request, response); 90 } 91 92 protected void doPost(HttpServletRequest request, HttpServletResponse response) 93 throws ServletException , IOException 94 { 95 processRequest(request, response); 96 } 97 98 private void processRequest(HttpServletRequest request, HttpServletResponse response) 99 throws ServletException , IOException 100 { 101 try 103 { 104 InitialContext ctx = new InitialContext (); 105 Context enc = (Context ) ctx.lookup("java:comp/env"); 106 System.out.println("Was able to lookup ENC"); 107 } 108 catch(NamingException e) 109 { 110 throw new ServletException ("Failed to lookup ENC", e); 111 } 112 113 log.info("processRequest, path="+request.getPathInfo()); 114 response.setContentType("text/html"); 115 PrintWriter pw = response.getWriter(); 116 pw.println("<html><head><title>Log4j1.1.3 test servlet</title></head>"); 117 pw.println("<body><h1>Log4j1.1.3 test servlet</h1></body>"); 118 pw.println("</html>"); 119 } 120 } 121 | Popular Tags |