1 22 package org.jboss.ejb3.test.dd.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.ejb3.test.dd.web.util.EJBManifestClass; 42 import org.jboss.ejb3.test.dd.web.util.EarLibUser; 43 44 50 public class ClasspathServlet extends HttpServlet 51 { 52 private static Logger log = Logger.getLogger(ClasspathServlet.class); 53 54 private StringBuffer initInfo = new StringBuffer (); 55 private boolean failOnError = true; 56 57 public void init(ServletConfig config) throws ServletException 58 { 59 String 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 Class clazz = Class.forName("org.jboss.ejb3.test.dd.web.util.ClassInClasses"); 68 initInfo.append("Successfully loaded class: "+clazz.getName()); 69 ClassLoader cl = clazz.getClassLoader(); 70 ProtectionDomain pd = clazz.getProtectionDomain(); 71 CodeSource cs = pd.getCodeSource(); 72 initInfo.append("\n ClassLoader : "+cl.getClass().getName()+':'+cl.hashCode()); 73 initInfo.append("\n CodeSource.location : "+cs.getLocation()); 74 75 URL jbprops = cl.getResource("/org/jboss/resources/JBoss.properties"); 77 log.info("JBoss.properties: "+jbprops); 78 ResourceBundle rb = ResourceBundle.getBundle("org.jboss.resources.JBoss"); 79 log.info("Found JBoss resources: "+rb); 80 Enumeration keys = rb.getKeys(); 81 while( keys.hasMoreElements() ) 82 log.info(keys.nextElement()); 83 } 84 catch(AccessControlException e) 85 { 86 log.error("Failed to init, ignoring security exception", e); 87 } 88 catch(Exception e) 89 { 90 log.error("Failed to init", e); 91 if( failOnError == true ) 92 throw new ServletException ("Failed to init ClasspathServlet", e); 93 else 94 { 95 StringWriter sw = new StringWriter (); 96 PrintWriter pw = new PrintWriter (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 request, HttpServletResponse response) 109 throws ServletException , IOException 110 { 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 { 121 out.println("<h1>EJBManifestClass Info</h1>"); 122 EJBManifestClass mfClass = new EJBManifestClass(); 123 StringBuffer results = new StringBuffer ("EJBManifestClass Info:"); 124 125 out.println("<pre>"); 126 out.println(results.toString()); 127 out.println("</pre>"); 128 } 129 catch(Exception e) 130 { 131 if( failOnError == true ) 132 throw new ServletException ("Failed to load EJBManifestClass", e); 133 else 134 { 135 StringWriter sw = new StringWriter (); 136 PrintWriter pw = new PrintWriter (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 info = EarLibUser.getClassInfo(); 148 out.println("<pre>"); 149 out.println(info); 150 out.println("</pre>"); 151 } 152 catch(Exception e) 153 { 154 if( failOnError == true ) 155 throw new ServletException ("Failed to load EarLibUser", e); 156 else 157 { 158 StringWriter sw = new StringWriter (); 159 PrintWriter pw = new PrintWriter (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 request, HttpServletResponse response) 172 throws ServletException , IOException 173 { 174 processRequest(request, response); 175 } 176 177 protected void doPost(HttpServletRequest request, HttpServletResponse response) 178 throws ServletException , IOException 179 { 180 processRequest(request, response); 181 } 182 } 183 | Popular Tags |