KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > ClasspathServlet


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.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.security.CodeSource JavaDoc;
28 import java.security.ProtectionDomain JavaDoc;
29 import java.security.AccessControlException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.jboss.logging.Logger;
40
41 import org.jboss.test.web.util.EJBManifestClass;
42 import org.jboss.test.web.util.EarLibUser;
43 import org.jboss.test.util.Debug;
44
45 /** A servlet that accesses classes in WEB-INF/classes using Class.forName
46  * during its initialization.
47  *
48  * @author Scott.Scott@jboss.org
49  * @version $Revision: 58279 $
50  */

51 public class ClasspathServlet extends HttpServlet JavaDoc
52 {
53    private static final long serialVersionUID = 1;
54
55    private static Logger log = Logger.getLogger(ClasspathServlet.class);
56
57    private StringBuffer JavaDoc initInfo = new StringBuffer JavaDoc();
58    private boolean failOnError = true;
59
60    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
61    {
62       String JavaDoc param = config.getInitParameter("failOnError");
63
64       if( param != null && Boolean.valueOf(param).booleanValue() == false )
65          failOnError = false;
66       log.info("init, failOnError="+failOnError);
67       try
68       {
69          // Check for a
70
Class JavaDoc clazz = Class.forName("org.jboss.test.web.util.ClassInClasses");
71          initInfo.append("Successfully loaded class: "+clazz.getName());
72          ClassLoader JavaDoc cl = clazz.getClassLoader();
73          ProtectionDomain JavaDoc pd = clazz.getProtectionDomain();
74          CodeSource JavaDoc cs = pd.getCodeSource();
75          initInfo.append("\n ClassLoader : "+cl.getClass().getName()+':'+cl.hashCode());
76          initInfo.append("\n CodeSource.location : "+cs.getLocation());
77
78          // Load a resource bundle
79
URL JavaDoc jbprops = cl.getResource("/org/jboss/resources/JBoss.properties");
80          log.info("JBoss.properties: "+jbprops);
81          ResourceBundle JavaDoc rb = ResourceBundle.getBundle("org.jboss.resources.JBoss");
82          log.info("Found JBoss resources: "+rb);
83          Enumeration JavaDoc keys = rb.getKeys();
84          while( keys.hasMoreElements() )
85             log.info(keys.nextElement());
86       }
87       catch(AccessControlException JavaDoc e)
88       {
89          log.error("Failed to init, ignoring security exception", e);
90       }
91       catch(Exception JavaDoc e)
92       {
93          log.error("Failed to init", e);
94          if( failOnError == true )
95             throw new ServletException JavaDoc("Failed to init ClasspathServlet", e);
96          else
97          {
98             StringWriter JavaDoc sw = new StringWriter JavaDoc();
99             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
100             e.printStackTrace(pw);
101             initInfo.append("\nFailed to init\n");
102             initInfo.append(sw.toString());
103          }
104       }
105    }
106
107    public void destroy()
108    {
109    }
110
111    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
112       throws ServletException JavaDoc, IOException JavaDoc
113    {
114       response.setContentType("text/html");
115       PrintWriter JavaDoc out = response.getWriter();
116       out.println("<html>");
117       out.println("<head><title>ClasspathServlet</title></head>");
118       out.println("<body><h1>Initialization Info</h1>");
119       out.println("<pre>\n");
120       out.println(initInfo.toString());
121       out.println("</pre>\n");
122       try
123       {
124          out.println("<h1>EJBManifestClass Info</h1>");
125          EJBManifestClass mfClass = new EJBManifestClass();
126          StringBuffer JavaDoc results = new StringBuffer JavaDoc("EJBManifestClass Info:");
127          Debug.displayClassInfo(mfClass.getClass(), results);
128          out.println("<pre>");
129          out.println(results.toString());
130          out.println("</pre>");
131       }
132       catch(Exception JavaDoc e)
133       {
134          if( failOnError == true )
135             throw new ServletException JavaDoc("Failed to load EJBManifestClass", e);
136          else
137          {
138             StringWriter JavaDoc sw = new StringWriter JavaDoc();
139             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
140             e.printStackTrace(pw);
141             out.println("<pre>");
142             out.println(sw.toString());
143             out.println("</pre>");
144          }
145       }
146
147       try
148       {
149          out.println("<h1>EarLibUser Info</h1>");
150          String JavaDoc info = EarLibUser.getClassInfo();
151          out.println("<pre>");
152          out.println(info);
153          out.println("</pre>");
154       }
155       catch(Exception JavaDoc e)
156       {
157          if( failOnError == true )
158             throw new ServletException JavaDoc("Failed to load EarLibUser", e);
159          else
160          {
161             StringWriter JavaDoc sw = new StringWriter JavaDoc();
162             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
163             e.printStackTrace(pw);
164             out.println("<pre>");
165             out.println(sw.toString());
166             out.println("</pre>");
167          }
168       }
169
170       out.println("</html>");
171       out.close();
172    }
173
174    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
175       throws ServletException JavaDoc, IOException JavaDoc
176    {
177       processRequest(request, response);
178    }
179    
180    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
181       throws ServletException JavaDoc, IOException JavaDoc
182    {
183       processRequest(request, response);
184    }
185 }
186
Popular Tags