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.session.TradeJDBC; 027 import org.apache.geronimo.samples.daytrader.session.TradeJDBCHome; 028 import org.apache.geronimo.samples.daytrader.util.*; 029 030 import org.apache.geronimo.samples.daytrader.*; 031 032 // TODO: fix comments 033 /** 034 * 035 * PingServlet2Session2JDBC tests key functionality of a servlet call to a 036 * stateless SessionEJB, and then to a Entity EJB representing data in a database. 037 * This servlet makes use of the Stateless Session EJB {@link Trade}, and then 038 * uses {@link TradeConfig} to generate a random stock symbol. The stocks 039 * price is looked up using the Quote Entity EJB. 040 * 041 */ 042 public class PingServlet2Session2JDBC extends HttpServlet 043 { 044 045 private static String initTime; 046 private static int hitCount; 047 private static TradeJDBCHome tradeJDBCHome; 048 049 /** 050 * forwards post requests to the doGet method 051 * Creation date: (11/6/2000 10:52:39 AM) 052 * @param res javax.servlet.http.HttpServletRequest 053 * @param res2 javax.servlet.http.HttpServletResponse 054 */ 055 public void doPost(HttpServletRequest req, HttpServletResponse res) 056 throws ServletException, IOException 057 { 058 doGet(req, res); 059 } 060 061 /** 062 * this is the main method of the servlet that will service all get requests. 063 * @param request HttpServletRequest 064 * @param responce HttpServletResponce 065 **/ 066 public void doGet(HttpServletRequest req, HttpServletResponse res) 067 throws IOException, ServletException 068 { 069 070 res.setContentType("text/html"); 071 java.io.PrintWriter out = res.getWriter(); 072 String symbol = null; 073 QuoteDataBean quoteData = null; 074 TradeJDBC trade = null; 075 StringBuffer output = new StringBuffer(100); 076 077 output.append( 078 "<html><head><title>PingServlet2Session2JDBC</title></head>" 079 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2Session2JDBC<BR></FONT>" 080 + "<FONT size=\"-1\" color=\"#000066\">" 081 + "PingServlet2Session2JDBC tests the common path of a Servlet calling a Session EJB " 082 + "which in turn calls JDBC.<BR>"); 083 084 try 085 { 086 087 //only want to do this once. 088 if (tradeJDBCHome == null) 089 { 090 //only want one thread to create the EjbHome 091 synchronized (lock) 092 { 093 094 if (tradeJDBCHome == null) 095 { 096 097 //out.println("doing JNDI lookup and creating new reference to trade.TradeHome"); 098 output.append("<HR><B>Performing JNDI lookup to create new tradeHome</B>"); 099 100 try 101 { 102 //I am going to use the System env. that is set through TradeConfig 103 InitialContext ic = new InitialContext(); 104 105 tradeJDBCHome = 106 (TradeJDBCHome) PortableRemoteObject.narrow( 107 ic.lookup("java:comp/env/ejb/TradeJDBC"), 108 TradeJDBCHome.class); 109 } 110 catch (Exception ne) 111 112 { 113 Log.error(ne, "PingServlet2Session2JDBC.goGet(...): exception caught looking up and narrowing 'TradeJDBCHome'"); 114 throw ne; 115 } 116 } 117 } 118 } 119 120 try 121 { 122 int iter = TradeConfig.getPrimIterations(); 123 for (int ii = 0; ii < iter; ii++) { 124 //I have the TradeBean Home, now I want to get an instance every time 125 symbol = TradeConfig.rndSymbol(); 126 trade = tradeJDBCHome.create(); 127 //getQuote will call findQuote which will instaniate the Quote Entity Bean 128 //and then will return a QuoteObject 129 quoteData = trade.getQuote(symbol); 130 trade.remove(); 131 } 132 } 133 catch (Exception ne) 134 { 135 Log.error(ne, "PingServlet2Session2JDBC.goGet(...): exception getting QuoteData through Trade"); 136 throw ne; 137 } 138 139 output.append("<HR>initTime: " + initTime).append( 140 "<BR>Hit Count: " + hitCount++); 141 output.append("<HR>Quote Information<BR><BR>" + quoteData.toHTML()); 142 out.println(output.toString()); 143 144 } 145 catch (Exception e) 146 { 147 Log.error(e, "PingServlet2Session2JDBC.doGet(...): General Exception caught"); 148 res.sendError(500, "General Exception caught, " + e.toString()); 149 } 150 } 151 152 /** 153 * returns a string of information about the servlet 154 * @return info String: contains info about the servlet 155 **/ 156 157 public String getServletInfo() 158 { 159 return "web primitive, tests Servlet to Session to Entity EJB path"; 160 161 } 162 /** 163 * called when the class is loaded to initialize the servlet 164 * @param config ServletConfig: 165 **/ 166 public void init(ServletConfig config) throws ServletException 167 { 168 super.init(config); 169 hitCount = 0; 170 tradeJDBCHome = null; 171 initTime = new java.util.Date().toString(); 172 //this is for synchronization 173 lock = new Integer(99); 174 } 175 private java.lang.Integer lock; 176 }