1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.sql.DataSource ; 36 37 import org.jboss.test.web.interfaces.ReferenceTest; 38 import org.jboss.test.web.interfaces.StatelessSession; 39 import org.jboss.test.web.interfaces.StatelessSessionHome; 40 import org.jboss.test.web.util.Util; 41 42 48 public class EJBOnStartupServlet extends HttpServlet 49 { 50 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 51 52 StatelessSessionHome home; 53 54 public void init(ServletConfig config) throws ServletException 55 { 56 String param = config.getInitParameter("failOnError"); 57 boolean failOnError = true; 58 if( param != null && Boolean.valueOf(param).booleanValue() == false ) 59 failOnError = false; 60 try 61 { 62 URL propsURL = Util.configureLog4j(); 64 log.debug("log4j.properties = "+propsURL); 65 InitialContext ctx = new InitialContext (); 67 home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/OptimizedEJB"); 68 log.debug("EJBOnStartupServlet is initialized"); 69 } 70 catch(Exception e) 71 { 72 log.debug("failed", e); 73 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 74 try 75 { 76 log.debug(Util.displayClassLoaders(loader)); 77 } 78 catch(NamingException ne) 79 { 80 log.debug("failed", ne); 81 } 82 if( failOnError == true ) 83 throw new ServletException ("Failed to init EJBOnStartupServlet", e); 84 } 85 } 86 87 public void destroy() 88 { 89 try 90 { 91 InitialContext ctx = new InitialContext (); 92 StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/OptimizedEJB"); 93 log.debug("EJBOnStartupServlet is destroyed"); 94 } 95 catch(Exception e) 96 { 97 log.debug("failed", e); 98 } 99 } 100 101 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 102 throws ServletException , IOException 103 { 104 try 105 { 106 StatelessSession bean = home.create(); 107 bean.noop(new ReferenceTest(), true); 108 bean.remove(); 109 } 110 catch(Exception e) 111 { 112 throw new ServletException ("Failed to call OptimizedEJB", e); 113 } 114 response.setContentType("text/html"); 115 PrintWriter out = response.getWriter(); 116 out.println("<html>"); 117 out.println("<head><title>EJBOnStartupServlet</title></head>"); 118 out.println("<body>Was initialized<br>"); 119 out.println("Tests passed<br>Time:"+Util.getTime()+"</body>"); 120 out.println("</html>"); 121 out.close(); 122 } 123 124 protected void doGet(HttpServletRequest request, HttpServletResponse response) 125 throws ServletException , IOException 126 { 127 processRequest(request, response); 128 } 129 130 protected void doPost(HttpServletRequest request, HttpServletResponse response) 131 throws ServletException , IOException 132 { 133 processRequest(request, response); 134 } 135 } 136 | Popular Tags |