/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/web/tx/TxControlServlet.java

https://github.com/ochaloup/jboss-as · Java · 145 lines · 84 code · 18 blank · 43 comment · 24 complexity · 9d2b566f44cb83d99ae913e62c023e98 MD5 · raw file

  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.as.test.integration.web.tx;
  23. import java.io.IOException;
  24. import javax.naming.InitialContext;
  25. import javax.servlet.RequestDispatcher;
  26. import javax.servlet.ServletContext;
  27. import javax.servlet.ServletException;
  28. import javax.servlet.annotation.WebServlet;
  29. import javax.servlet.http.HttpServlet;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import javax.transaction.Status;
  33. import javax.transaction.UserTransaction;
  34. import org.jboss.logging.Logger;
  35. /**
  36. * A servlet that initiates a transaction, uses a RequestDispatcher to include or forward to {@link TxStatusServlet}
  37. * and then optionally commits the transaction. Used to test that the transaction propagates and that failure to
  38. * commit it is properly detected.
  39. *
  40. * @author Brian Stansberry (c) 2012 Red Hat Inc.
  41. */
  42. @WebServlet(name = "TxControlServlet", urlPatterns = "/" + TxControlServlet.URL_PATTERN)
  43. public class TxControlServlet extends HttpServlet {
  44. private static final long serialVersionUID = -853278446594804509L;
  45. private static Logger log = Logger.getLogger(TxControlServlet.class);
  46. /** The name of the context to which requests are forwarded */
  47. private static final String forwardContext = "/tx-status";
  48. private static final String forwardPath = TxStatusServlet.URL_PATTERN;
  49. static final String URL_PATTERN = "TxControlServlet";
  50. static final String INNER_STATUS_HEADER = "X-Inner-Transaction-Status";
  51. static final String OUTER_STATUS_HEADER = "X-Outer-Transaction-Status";
  52. /**
  53. * Lookup the UserTransaction and begin a transaction.
  54. *
  55. * @param request
  56. * @param response
  57. * @throws ServletException
  58. * @throws IOException
  59. */
  60. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  61. if (log.isTraceEnabled()) {
  62. log.trace("[" + forwardContext + "], PathInfo: " + request.getPathInfo() + ", QueryString: "
  63. + request.getQueryString() + ", ContextPath: " + request.getContextPath() + ", HeaderNames: "
  64. + request.getHeaderNames() + ", isCommitted: " + response.isCommitted());
  65. }
  66. String includeParam = request.getParameter("include");
  67. if (includeParam == null)
  68. throw new IllegalStateException("No include parameter seen");
  69. boolean include = Boolean.valueOf(includeParam);
  70. String commitParam = request.getParameter("commit");
  71. if (commitParam == null)
  72. throw new IllegalStateException("No commit parameter seen");
  73. boolean commit = Boolean.valueOf(commitParam);
  74. UserTransaction transaction;
  75. try {
  76. transaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
  77. transaction.begin();
  78. } catch (Exception e) {
  79. throw new RuntimeException(e);
  80. }
  81. ServletContext sc = getServletContext().getContext(forwardContext);
  82. if (sc != null) {
  83. // if (log.isTraceEnabled())
  84. log.trace("Found ServletContext for: " + forwardContext);
  85. RequestDispatcher rd = sc.getRequestDispatcher(forwardPath);
  86. if (rd != null) {
  87. // if (log.isTraceEnabled())
  88. log.trace("Found RequestDispatcher for: " + forwardPath);
  89. if (include) {
  90. rd.include(request, response);
  91. } else {
  92. rd.forward(request, response);
  93. }
  94. // Get the tx status that TxStatusServlet saw
  95. Integer status = (Integer) request.getAttribute(TxStatusServlet.ATTRIBUTE);
  96. if (status == null) {
  97. throw new ServletException("No transaction status");
  98. }
  99. if (include) {
  100. // We can still write to the response w/ an include, so pass the status to the client
  101. response.setHeader(INNER_STATUS_HEADER, status.toString());
  102. } else if (status.intValue() != Status.STATUS_ACTIVE) {
  103. throw new ServletException("Status is " + status);
  104. }
  105. } else {
  106. throw new ServletException("No RequestDispatcher for: " + forwardContext + forwardPath);
  107. }
  108. } else {
  109. throw new ServletException("No ServletContext for: " + forwardContext);
  110. }
  111. try {
  112. // Get the tx status now
  113. int ourStatus = transaction.getStatus();
  114. if (include) {
  115. // We can still write to the response w/ an include, so pass the status to the client
  116. response.setHeader(OUTER_STATUS_HEADER, String.valueOf(ourStatus));
  117. } else if (ourStatus != Status.STATUS_ACTIVE) {
  118. throw new ServletException("Status is " + ourStatus);
  119. }
  120. if (commit) {
  121. transaction.commit();
  122. }
  123. } catch (Exception e) {
  124. throw new ServletException(e);
  125. }
  126. }
  127. }