KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > 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.ejb3.test.dd.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.ejb3.test.dd.web.util.EJBManifestClass;
42 import org.jboss.ejb3.test.dd.web.util.EarLibUser;
43
44 /** A servlet that accesses classes in WEB-INF/classes using Class.forName
45  * during its initialization.
46  *
47  * @author Scott.Scott@jboss.org
48  * @version $Revision: 58110 $
49  */

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