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 * PingServlet2Session2Entity tests key functionality of a servlet call to a 033 * stateless SessionEJB, and then to a Entity EJB representing data in a database. 034 * This servlet makes use of the Stateless Session EJB {@link Trade}, and then 035 * uses {@link TradeConfig} to generate a random stock symbol. The stocks 036 * price is looked up using the Quote Entity EJB. 037 * 038 */ 039 public class PingServlet2Session2Entity extends HttpServlet 040 { 041 042 private static String initTime; 043 private static int hitCount; 044 private static TradeHome tradeHome; 045 046 /** 047 * forwards post requests to the doGet method 048 * Creation date: (11/6/2000 10:52:39 AM) 049 * @param res javax.servlet.http.HttpServletRequest 050 * @param res2 javax.servlet.http.HttpServletResponse 051 */ 052 public void doPost(HttpServletRequest req, HttpServletResponse res) 053 throws ServletException, IOException 054 { 055 doGet(req, res); 056 } 057 058 /** 059 * this is the main method of the servlet that will service all get requests. 060 * @param request HttpServletRequest 061 * @param responce HttpServletResponce 062 **/ 063 public void doGet(HttpServletRequest req, HttpServletResponse res) 064 throws IOException, ServletException 065 { 066 067 res.setContentType("text/html"); 068 java.io.PrintWriter out = res.getWriter(); 069 String symbol = null; 070 QuoteDataBean quoteData = null; 071 Trade trade = null; 072 StringBuffer output = new StringBuffer(100); 073 074 output.append( 075 "<html><head><title>PingServlet2Session2Entity</title></head>" 076 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2Session2Entity<BR></FONT>" 077 + "<FONT size=\"-1\" color=\"#000066\">" 078 + "PingServlet2Session2Entity tests the common path of a Servlet calling a Session EJB " 079 + "which in turn calls an Entity EJB.<BR>"); 080 081 try 082 { 083 084 //only want to do this once. 085 if (tradeHome == null) 086 { 087 //only want one thread to create the EjbHome 088 synchronized (lock) 089 { 090 091 if (tradeHome == null) 092 { 093 094 //out.println("doing JNDI lookup and creating new reference to trade.TradeHome"); 095 output.append("<HR><B>Performing JNDI lookup to create new tradeHome</B>"); 096 097 try 098 { 099 //I am going to use the System env. that is set through TradeConfig 100 InitialContext ic = new InitialContext(); 101 102 tradeHome = 103 (TradeHome) PortableRemoteObject.narrow( 104 ic.lookup("java:comp/env/ejb/Trade"), 105 TradeHome.class); 106 } 107 catch (Exception ne) 108 109 { 110 Log.error(ne, "PingServlet2Session2Entity.goGet(...): exception caught looking up and narrowing 'TradeHome'"); 111 throw ne; 112 } 113 } 114 } 115 } 116 117 try 118 { 119 int iter = TradeConfig.getPrimIterations(); 120 for (int ii = 0; ii < iter; ii++) { 121 //I have the TradeBean Home, now I want to get an instance every time 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.getQuote(symbol); 127 trade.remove(); 128 } 129 } 130 catch (Exception ne) 131 { 132 Log.error(ne, "PingServlet2Session2Entity.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>Quote Information<BR><BR>" + quoteData.toHTML()); 139 out.println(output.toString()); 140 141 } 142 catch (Exception e) 143 { 144 Log.error(e, "PingServlet2Session2Entity.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 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 }