KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > UserTransactionServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.servlets;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import javax.transaction.UserTransaction JavaDoc;
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 /**
47  * A servlet that tests UserTransaction support.
48  *
49  * Adapted from Scott Starks EJBServlet.java.
50  *
51  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
52  * @version $Revision: 37406 $
53  */

54 public class UserTransactionServlet extends HttpServlet JavaDoc
55 {
56     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57         throws ServletException JavaDoc, IOException JavaDoc
58     {
59         // Get an initial context
60
InitialContext JavaDoc ctx;
61         try {
62             ctx = new InitialContext JavaDoc();
63         } catch (NamingException JavaDoc ex) {
64             throw new ServletException JavaDoc("Unable to get an InitialContext", ex);
65         }
66
67         // Get the UserTransaction
68
UserTransaction JavaDoc ut;
69         try {
70             ut = (UserTransaction JavaDoc)ctx.lookup("java:comp/UserTransaction");
71         } catch (NamingException JavaDoc ex) {
72             throw new ServletException JavaDoc("Unable to lookup UserTransaction", ex);
73         }
74
75         // Get the UserTransaction test bean home
76
CtsBmpHome home;
77         try {
78             home = (CtsBmpHome)ctx.lookup("java:comp/env/ejb/CtsBmp");
79         } catch (NamingException JavaDoc ex) {
80             throw new ServletException JavaDoc("Unable to lookup CtsBmp home", ex);
81         }
82
83         // Initialize the UserTransaction test home to empty
84
try {
85             // The findAll() implementation has the side-effect of
86
// creating the database table, if it does not exist.
87
Collection JavaDoc clct = home.findAll();
88
89             // Remove any old beans
90
if (clct.size() != 0) {
91                 for (Iterator JavaDoc itr=clct.iterator(); itr.hasNext();) {
92                     CtsBmp bean = (CtsBmp)itr.next();
93                     bean.remove();
94             }
95             }
96         } catch (Exception JavaDoc ex) {
97             throw new ServletException JavaDoc("Unable to initialize CtsBmp", ex);
98         }
99
100         // Create an UT tester instance
101
UserTransactionTester utt = new UserTransactionTester(home, ut);
102  
103         if (!utt.runAllTests())
104             throw new ServletException JavaDoc("UserTransaction tests FAILED");
105
106         response.setContentType("text/html");
107         PrintWriter JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
116         throws ServletException JavaDoc, IOException JavaDoc
117     {
118         processRequest(request, response);
119     }
120
121     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
122         throws ServletException JavaDoc, IOException JavaDoc
123     {
124         processRequest(request, response);
125     }
126 }
127
Popular Tags