001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.dd.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027: import java.security.CodeSource;
028: import java.security.ProtectionDomain;
029: import java.security.AccessControlException;
030: import java.net.URL;
031: import java.util.ResourceBundle;
032: import java.util.Enumeration;
033: import javax.servlet.ServletConfig;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.jboss.logging.Logger;
040:
041: import org.jboss.ejb3.test.dd.web.util.EJBManifestClass;
042: import org.jboss.ejb3.test.dd.web.util.EarLibUser;
043:
044: /** A servlet that accesses classes in WEB-INF/classes using Class.forName
045: * during its initialization.
046: *
047: * @author Scott.Scott@jboss.org
048: * @version $Revision: 60233 $
049: */
050: public class ClasspathServlet extends HttpServlet {
051: private static Logger log = Logger
052: .getLogger(ClasspathServlet.class);
053:
054: private StringBuffer initInfo = new StringBuffer();
055: private boolean failOnError = true;
056:
057: public void init(ServletConfig config) throws ServletException {
058: String param = config.getInitParameter("failOnError");
059:
060: if (param != null
061: && Boolean.valueOf(param).booleanValue() == false)
062: failOnError = false;
063: log.info("init, failOnError=" + failOnError);
064: try {
065: // Check for a
066: Class clazz = Class
067: .forName("org.jboss.ejb3.test.dd.web.util.ClassInClasses");
068: initInfo.append("Successfully loaded class: "
069: + clazz.getName());
070: ClassLoader cl = clazz.getClassLoader();
071: ProtectionDomain pd = clazz.getProtectionDomain();
072: CodeSource cs = pd.getCodeSource();
073: initInfo.append("\n ClassLoader : "
074: + cl.getClass().getName() + ':' + cl.hashCode());
075: initInfo.append("\n CodeSource.location : "
076: + cs.getLocation());
077:
078: // Load a resource bundle
079: URL jbprops = cl
080: .getResource("/org/jboss/resources/JBoss.properties");
081: log.info("JBoss.properties: " + jbprops);
082: ResourceBundle rb = ResourceBundle
083: .getBundle("org.jboss.resources.JBoss");
084: log.info("Found JBoss resources: " + rb);
085: Enumeration keys = rb.getKeys();
086: while (keys.hasMoreElements())
087: log.info(keys.nextElement());
088: } catch (AccessControlException e) {
089: log.error("Failed to init, ignoring security exception", e);
090: } catch (Exception e) {
091: log.error("Failed to init", e);
092: if (failOnError == true)
093: throw new ServletException(
094: "Failed to init ClasspathServlet", e);
095: else {
096: StringWriter sw = new StringWriter();
097: PrintWriter pw = new PrintWriter(sw);
098: e.printStackTrace(pw);
099: initInfo.append("\nFailed to init\n");
100: initInfo.append(sw.toString());
101: }
102: }
103: }
104:
105: public void destroy() {
106: }
107:
108: protected void processRequest(HttpServletRequest request,
109: HttpServletResponse response) throws ServletException,
110: IOException {
111: response.setContentType("text/html");
112: PrintWriter 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: out.println("<h1>EJBManifestClass Info</h1>");
121: EJBManifestClass mfClass = new EJBManifestClass();
122: StringBuffer results = new StringBuffer(
123: "EJBManifestClass Info:");
124:
125: out.println("<pre>");
126: out.println(results.toString());
127: out.println("</pre>");
128: } catch (Exception e) {
129: if (failOnError == true)
130: throw new ServletException(
131: "Failed to load EJBManifestClass", e);
132: else {
133: StringWriter sw = new StringWriter();
134: PrintWriter pw = new PrintWriter(sw);
135: e.printStackTrace(pw);
136: out.println("<pre>");
137: out.println(sw.toString());
138: out.println("</pre>");
139: }
140: }
141:
142: try {
143: out.println("<h1>EarLibUser Info</h1>");
144: String info = EarLibUser.getClassInfo();
145: out.println("<pre>");
146: out.println(info);
147: out.println("</pre>");
148: } catch (Exception e) {
149: if (failOnError == true)
150: throw new ServletException("Failed to load EarLibUser",
151: e);
152: else {
153: StringWriter sw = new StringWriter();
154: PrintWriter pw = new PrintWriter(sw);
155: e.printStackTrace(pw);
156: out.println("<pre>");
157: out.println(sw.toString());
158: out.println("</pre>");
159: }
160: }
161:
162: out.println("</html>");
163: out.close();
164: }
165:
166: protected void doGet(HttpServletRequest request,
167: HttpServletResponse response) throws ServletException,
168: IOException {
169: processRequest(request, response);
170: }
171:
172: protected void doPost(HttpServletRequest request,
173: HttpServletResponse response) throws ServletException,
174: IOException {
175: processRequest(request, response);
176: }
177: }
|