001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader.web.prims; 018 019 import java.io.*; 020 import javax.servlet.*; 021 import javax.servlet.http.*; 022 import javax.naming.*; 023 import javax.rmi.PortableRemoteObject; 024 025 import org.apache.geronimo.samples.daytrader.ejb.*; 026 import org.apache.geronimo.samples.daytrader.util.*; 027 028 import org.apache.geronimo.samples.daytrader.*; 029 030 /** 031 * 032 * PingServlet2TwoPhase tests key functionality of a TwoPhase commit 033 * In this primitive a servlet calls a Session EJB which begins a global txn 034 * The Session EJB then reads a DB row and sends a message to JMS Queue 035 * The txn is closed w/ a 2-phase commit 036 * 037 */ 038 public class PingServlet2TwoPhase extends HttpServlet 039 { 040 041 private static String initTime; 042 private static int hitCount; 043 private static TradeHome tradeHome; 044 045 /** 046 * forwards post requests to the doGet method 047 * Creation date: (11/6/2000 10:52:39 AM) 048 * @param res javax.servlet.http.HttpServletRequest 049 * @param res2 javax.servlet.http.HttpServletResponse 050 */ 051 public void doPost(HttpServletRequest req, HttpServletResponse res) 052 throws ServletException, IOException 053 { 054 doGet(req, res); 055 } 056 057 /** 058 * this is the main method of the servlet that will service all get requests. 059 * @param request HttpServletRequest 060 * @param responce HttpServletResponce 061 **/ 062 public void doGet(HttpServletRequest req, HttpServletResponse res) 063 throws IOException, ServletException 064 { 065 066 res.setContentType("text/html"); 067 java.io.PrintWriter out = res.getWriter(); 068 String symbol = null; 069 QuoteDataBean quoteData = null; 070 Trade trade = null; 071 StringBuffer output = new StringBuffer(100); 072 073 output.append( 074 "<html><head><title>PingServlet2TwoPhase</title></head>" 075 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2TwoPhase<BR></FONT>" 076 + "<FONT size=\"-1\" color=\"#000066\">" 077 + "PingServlet2TwoPhase tests the path of a Servlet calling a Session EJB " 078 + "which in turn calls an Entity EJB to read a DB row (quote). The Session EJB " 079 + "then posts a message to a JMS Queue. " 080 + "<BR> These operations are wrapped in a 2-phase commit<BR>"); 081 082 try 083 { 084 085 //only want to do this once. 086 if (tradeHome == null) 087 { 088 //only want one thread to create the EjbHome 089 synchronized (lock) 090 { 091 092 if (tradeHome == null) 093 { 094 095 //out.println("doing JNDI lookup and creating new reference to trade.TradeHome"); 096 output.append("<HR><B>Performing JNDI lookup to create new tradeHome</B>"); 097 098 try 099 { 100 //I am going to use the System env. that is set through TradeConfig 101 InitialContext ic = new InitialContext(); 102 103 tradeHome = 104 (TradeHome) PortableRemoteObject.narrow( 105 ic.lookup("java:comp/env/ejb/Trade"), 106 TradeHome.class); 107 } 108 catch (Exception ne) 109 110 { 111 Log.error(ne, "PingServlet2TwoPhase.goGet(...): exception caught looking up and narrowing 'TradeHome'"); 112 throw ne; 113 } 114 } 115 } 116 } 117 118 try 119 { 120 int iter = TradeConfig.getPrimIterations(); 121 for (int ii = 0; ii < iter; ii++) { 122 symbol = TradeConfig.rndSymbol(); 123 trade = tradeHome.create(); 124 //getQuote will call findQuote which will instaniate the Quote Entity Bean 125 //and then will return a QuoteObject 126 quoteData = trade.pingTwoPhase(symbol); 127 trade.remove(); 128 } 129 } 130 catch (Exception ne) 131 { 132 Log.error(ne, "PingServlet2TwoPhase.goGet(...): exception getting QuoteData through Trade"); 133 throw ne; 134 } 135 136 output.append("<HR>initTime: " + initTime).append( 137 "<BR>Hit Count: " + hitCount++); 138 output.append("<HR>Two phase ping selected a quote and sent a message to TradeBrokerQueue JMS queue<BR>Quote Information<BR><BR>" + quoteData.toHTML()); 139 out.println(output.toString()); 140 141 } 142 catch (Exception e) 143 { 144 Log.error(e, "PingServlet2TwoPhase.doGet(...): General Exception caught"); 145 res.sendError(500, "General Exception caught, " + e.toString()); 146 } 147 } 148 149 /** 150 * returns a string of information about the servlet 151 * @return info String: contains info about the servlet 152 **/ 153 154 public String getServletInfo() 155 { 156 return "web primitive, tests Servlet to Session to Entity EJB and JMS -- 2-phase commit path"; 157 158 } 159 /** 160 * called when the class is loaded to initialize the servlet 161 * @param config ServletConfig: 162 **/ 163 public void init(ServletConfig config) throws ServletException 164 { 165 super.init(config); 166 hitCount = 0; 167 tradeHome = null; 168 initTime = new java.util.Date().toString(); 169 //this is for synchronization 170 lock = new Integer(99); 171 } 172 private java.lang.Integer lock; 173 }