KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > scoping > override > web > comlog > Log4jServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.classloader.scoping.override.web.comlog;
24
25 import java.io.IOException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.File JavaDoc;
28 import java.util.Date JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.commons.logging.Log;
40
41 /** A servlet that validates that it sees the application specific log4j output
42  * in the ${jboss.server.log.dir}/cl-test.log increasing. The logging interface
43  * used is the commons-logging LogFactory/Log.
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 43954 $
47  */

48 public class Log4jServlet extends HttpServlet JavaDoc
49 {
50    private static Log log = LogFactory.getLog(Log4jServlet.class);
51
52    /**
53     *
54     * @param servletConfig
55     * @throws javax.servlet.ServletException
56     */

57    public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
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 JavaDoc request, HttpServletResponse JavaDoc response)
65          throws ServletException JavaDoc, IOException JavaDoc
66    {
67       processRequest(request, response);
68    }
69
70    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71          throws ServletException JavaDoc, IOException JavaDoc
72    {
73       processRequest(request, response);
74    }
75
76    private void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77          throws ServletException JavaDoc, IOException JavaDoc
78    {
79       log.info("processRequest, path="+request.getPathInfo());
80       try
81       {
82          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
83          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
84          log.info("Was able to lookup ENC, "+enc);
85       }
86       catch(NamingException JavaDoc e)
87       {
88          throw new ServletException JavaDoc("Failed to lookup ENC", e);
89       }
90
91       // Validate that the cl-test.log
92
String JavaDoc logDir = System.getProperty("jboss.server.log.dir");
93       File JavaDoc logFile = new File JavaDoc(logDir, "cl-test.log");
94       if( logFile.exists() == false )
95          throw new ServletException JavaDoc(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 JavaDoc(logFile+" length is not increasing");
105       response.setContentType("text/html");
106       PrintWriter JavaDoc 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 JavaDoc(lastModified));
111       pw.println("</body></html>");
112       pw.flush();
113    }
114 }
115
Popular Tags