1 22 package org.jboss.test.web.servlets; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 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 javax.transaction.UserTransaction ; 40 41 import org.jboss.test.cts.interfaces.CtsBmpHome; 42 import org.jboss.test.cts.interfaces.CtsBmp; 43 import org.jboss.test.cts.interfaces.UserTransactionTester; 44 45 46 54 public class UserTransactionServlet extends HttpServlet 55 { 56 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 57 throws ServletException , IOException 58 { 59 InitialContext ctx; 61 try { 62 ctx = new InitialContext (); 63 } catch (NamingException ex) { 64 throw new ServletException ("Unable to get an InitialContext", ex); 65 } 66 67 UserTransaction ut; 69 try { 70 ut = (UserTransaction )ctx.lookup("java:comp/UserTransaction"); 71 } catch (NamingException ex) { 72 throw new ServletException ("Unable to lookup UserTransaction", ex); 73 } 74 75 CtsBmpHome home; 77 try { 78 home = (CtsBmpHome)ctx.lookup("java:comp/env/ejb/CtsBmp"); 79 } catch (NamingException ex) { 80 throw new ServletException ("Unable to lookup CtsBmp home", ex); 81 } 82 83 try { 85 Collection clct = home.findAll(); 88 89 if (clct.size() != 0) { 91 for (Iterator itr=clct.iterator(); itr.hasNext();) { 92 CtsBmp bean = (CtsBmp)itr.next(); 93 bean.remove(); 94 } 95 } 96 } catch (Exception ex) { 97 throw new ServletException ("Unable to initialize CtsBmp", ex); 98 } 99 100 UserTransactionTester utt = new UserTransactionTester(home, ut); 102 103 if (!utt.runAllTests()) 104 throw new ServletException ("UserTransaction tests FAILED"); 105 106 response.setContentType("text/html"); 107 PrintWriter out = response.getWriter(); 108 out.println("<html>"); 109 out.println("<head><title>UserTransactionServlet</title></head>"); 110 out.println("<body>UserTransaction tests passed</body>"); 111 out.println("</html>"); 112 out.close(); 113 } 114 115 protected void doGet(HttpServletRequest request, HttpServletResponse response) 116 throws ServletException , IOException 117 { 118 processRequest(request, response); 119 } 120 121 protected void doPost(HttpServletRequest request, HttpServletResponse response) 122 throws ServletException , IOException 123 { 124 processRequest(request, response); 125 } 126 } 127 | Popular Tags |