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