KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > scoping > override > web > log4j113 > 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 package org.jboss.test.classloader.scoping.override.web.log4j113;
23
24
25 import java.io.IOException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.net.URL JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletConfig JavaDoc;
37
38 import org.apache.log4j.Category;
39 import org.apache.log4j.PropertyConfigurator;
40
41 /** A servlet that validates that it sees the log4j 1.1.3 version of the
42  * Category class on initialization.
43  *
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37406 $
46  */

47 public class Log4jServlet extends HttpServlet JavaDoc
48 {
49    private Category log;
50
51    /**
52     *
53     * @param servletConfig
54     * @throws ServletException
55     */

56    public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
57    {
58       super.init(servletConfig);
59       // Validate the log4j env against the 1.1.3 classes
60
try
61       {
62          Class JavaDoc categoryClass = Category.class;
63          // Check that the 1.1.3 assert(boolean, String) method exists
64
Class JavaDoc[] sig = {boolean.class, String JavaDoc.class};
65          Method JavaDoc m = categoryClass.getDeclaredMethod("assert", sig);
66          System.out.println("found assert method: "+m);
67          // Find the log4j.properties file
68
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
69          URL JavaDoc 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          // Try to access the java:comp/env context
76
InitialContext JavaDoc ctx = new InitialContext JavaDoc();
77          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
78          System.out.println("Was able to lookup ENC");
79       }
80       catch(Throwable JavaDoc t)
81       {
82          throw new ServletException JavaDoc("Log4jServlet init failed", t);
83       }
84    }
85
86    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
87          throws ServletException JavaDoc, IOException JavaDoc
88    {
89       processRequest(request, response);
90    }
91
92    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
93          throws ServletException JavaDoc, IOException JavaDoc
94    {
95       processRequest(request, response);
96    }
97
98    private void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99          throws ServletException JavaDoc, IOException JavaDoc
100    {
101       // Try to access the java:comp/env context
102
try
103       {
104          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
105          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
106          System.out.println("Was able to lookup ENC");
107       }
108       catch(NamingException JavaDoc e)
109       {
110          throw new ServletException JavaDoc("Failed to lookup ENC", e);
111       }
112
113       log.info("processRequest, path="+request.getPathInfo());
114       response.setContentType("text/html");
115       PrintWriter JavaDoc 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