001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import java.io.IOException;
028: import java.io.PrintWriter;
029:
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032:
033: import javax.servlet.ServletConfig;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import javax.transaction.UserTransaction;
040:
041: import org.jboss.test.cts.interfaces.CtsBmpHome;
042: import org.jboss.test.cts.interfaces.CtsBmp;
043: import org.jboss.test.cts.interfaces.UserTransactionTester;
044:
045: /**
046: * A servlet that tests UserTransaction support.
047: *
048: * Adapted from Scott Starks EJBServlet.java.
049: *
050: * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
051: * @version $Revision: 57211 $
052: */
053: public class UserTransactionServlet extends HttpServlet {
054: protected void processRequest(HttpServletRequest request,
055: HttpServletResponse response) throws ServletException,
056: IOException {
057: // Get an initial context
058: InitialContext ctx;
059: try {
060: ctx = new InitialContext();
061: } catch (NamingException ex) {
062: throw new ServletException(
063: "Unable to get an InitialContext", ex);
064: }
065:
066: // Get the UserTransaction
067: UserTransaction ut;
068: try {
069: ut = (UserTransaction) ctx
070: .lookup("java:comp/UserTransaction");
071: } catch (NamingException ex) {
072: throw new ServletException(
073: "Unable to lookup UserTransaction", ex);
074: }
075:
076: // Get the UserTransaction test bean home
077: CtsBmpHome home;
078: try {
079: home = (CtsBmpHome) ctx.lookup("java:comp/env/ejb/CtsBmp");
080: } catch (NamingException ex) {
081: throw new ServletException("Unable to lookup CtsBmp home",
082: ex);
083: }
084:
085: // Initialize the UserTransaction test home to empty
086: try {
087: // The findAll() implementation has the side-effect of
088: // creating the database table, if it does not exist.
089: Collection clct = home.findAll();
090:
091: // Remove any old beans
092: if (clct.size() != 0) {
093: for (Iterator itr = clct.iterator(); itr.hasNext();) {
094: CtsBmp bean = (CtsBmp) itr.next();
095: bean.remove();
096: }
097: }
098: } catch (Exception ex) {
099: throw new ServletException("Unable to initialize CtsBmp",
100: ex);
101: }
102:
103: // Create an UT tester instance
104: UserTransactionTester utt = new UserTransactionTester(home, ut);
105:
106: if (!utt.runAllTests())
107: throw new ServletException("UserTransaction tests FAILED");
108:
109: response.setContentType("text/html");
110: PrintWriter out = response.getWriter();
111: out.println("<html>");
112: out
113: .println("<head><title>UserTransactionServlet</title></head>");
114: out.println("<body>UserTransaction tests passed</body>");
115: out.println("</html>");
116: out.close();
117: }
118:
119: protected void doGet(HttpServletRequest request,
120: HttpServletResponse response) throws ServletException,
121: IOException {
122: processRequest(request, response);
123: }
124:
125: protected void doPost(HttpServletRequest request,
126: HttpServletResponse response) throws ServletException,
127: IOException {
128: processRequest(request, response);
129: }
130: }
|