1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.security.CodeSource ; 28 import java.security.ProtectionDomain ; 29 import java.security.AccessControlException ; 30 import java.net.URL ; 31 import java.util.ResourceBundle ; 32 import java.util.Enumeration ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 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 51 public class ClasspathServlet extends HttpServlet 52 { 53 private static final long serialVersionUID = 1; 54 55 private static Logger log = Logger.getLogger(ClasspathServlet.class); 56 57 private StringBuffer initInfo = new StringBuffer (); 58 private boolean failOnError = true; 59 60 public void init(ServletConfig config) throws ServletException 61 { 62 String 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 Class clazz = Class.forName("org.jboss.test.web.util.ClassInClasses"); 71 initInfo.append("Successfully loaded class: "+clazz.getName()); 72 ClassLoader cl = clazz.getClassLoader(); 73 ProtectionDomain pd = clazz.getProtectionDomain(); 74 CodeSource cs = pd.getCodeSource(); 75 initInfo.append("\n ClassLoader : "+cl.getClass().getName()+':'+cl.hashCode()); 76 initInfo.append("\n CodeSource.location : "+cs.getLocation()); 77 78 URL jbprops = cl.getResource("/org/jboss/resources/JBoss.properties"); 80 log.info("JBoss.properties: "+jbprops); 81 ResourceBundle rb = ResourceBundle.getBundle("org.jboss.resources.JBoss"); 82 log.info("Found JBoss resources: "+rb); 83 Enumeration keys = rb.getKeys(); 84 while( keys.hasMoreElements() ) 85 log.info(keys.nextElement()); 86 } 87 catch(AccessControlException e) 88 { 89 log.error("Failed to init, ignoring security exception", e); 90 } 91 catch(Exception e) 92 { 93 log.error("Failed to init", e); 94 if( failOnError == true ) 95 throw new ServletException ("Failed to init ClasspathServlet", e); 96 else 97 { 98 StringWriter sw = new StringWriter (); 99 PrintWriter pw = new PrintWriter (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 request, HttpServletResponse response) 112 throws ServletException , IOException 113 { 114 response.setContentType("text/html"); 115 PrintWriter 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 results = new StringBuffer ("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 e) 133 { 134 if( failOnError == true ) 135 throw new ServletException ("Failed to load EJBManifestClass", e); 136 else 137 { 138 StringWriter sw = new StringWriter (); 139 PrintWriter pw = new PrintWriter (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 info = EarLibUser.getClassInfo(); 151 out.println("<pre>"); 152 out.println(info); 153 out.println("</pre>"); 154 } 155 catch(Exception e) 156 { 157 if( failOnError == true ) 158 throw new ServletException ("Failed to load EarLibUser", e); 159 else 160 { 161 StringWriter sw = new StringWriter (); 162 PrintWriter pw = new PrintWriter (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 request, HttpServletResponse response) 175 throws ServletException , IOException 176 { 177 processRequest(request, response); 178 } 179 180 protected void doPost(HttpServletRequest request, HttpServletResponse response) 181 throws ServletException , IOException 182 { 183 processRequest(request, response); 184 } 185 } 186 | Popular Tags |