1 22 23 package org.jboss.test.classloader.scoping.override.web.comlog; 24 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.io.File ; 28 import java.util.Date ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.naming.InitialContext ; 35 import javax.naming.Context ; 36 import javax.naming.NamingException ; 37 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.commons.logging.Log; 40 41 48 public class Log4jServlet extends HttpServlet  49 { 50 private static Log log = LogFactory.getLog(Log4jServlet.class); 51 52 57 public void init(ServletConfig servletConfig) throws ServletException  58 { 59 super.init(servletConfig); 60 log.info("init, servletConfig="+servletConfig); 61 System.out.println("Log class: "+log.getClass()); 62 } 63 64 protected void doGet(HttpServletRequest request, HttpServletResponse response) 65 throws ServletException , IOException  66 { 67 processRequest(request, response); 68 } 69 70 protected void doPost(HttpServletRequest request, HttpServletResponse response) 71 throws ServletException , IOException  72 { 73 processRequest(request, response); 74 } 75 76 private void processRequest(HttpServletRequest request, HttpServletResponse response) 77 throws ServletException , IOException  78 { 79 log.info("processRequest, path="+request.getPathInfo()); 80 try 81 { 82 InitialContext ctx = new InitialContext (); 83 Context enc = (Context ) ctx.lookup("java:comp/env"); 84 log.info("Was able to lookup ENC, "+enc); 85 } 86 catch(NamingException e) 87 { 88 throw new ServletException ("Failed to lookup ENC", e); 89 } 90 91 String logDir = System.getProperty("jboss.server.log.dir"); 93 File logFile = new File (logDir, "cl-test.log"); 94 if( logFile.exists() == false ) 95 throw new ServletException (logFile+" does not exist"); 96 97 long length = logFile.length(); 98 log.info("Current length = "+length); 99 for(int n = 0; n < 100; n ++) 100 log.info("Msg #"+n); 101 long lastModified = logFile.lastModified(); 102 long length2 = logFile.length(); 103 if( !(length2 > length) ) 104 throw new ServletException (logFile+" length is not increasing"); 105 response.setContentType("text/html"); 106 PrintWriter pw = response.getWriter(); 107 pw.println("<html><head><title>Commons logging test servlet</title></head>"); 108 pw.println("<body><h1>Commons logging test servlet</h1>"); 109 pw.println("Log length: "+length2); 110 pw.println(", LastModified: "+new Date (lastModified)); 111 pw.println("</body></html>"); 112 pw.flush(); 113 } 114 } 115 | Popular Tags |