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 * Primitive designed to run within the TradeApplication and makes use of {@link trade_client.TradeConfig} 033 * for config parameters and random stock symbols. 034 * Servlet will generate a random stock symbol and get the price of that symbol using a {@link trade.Quote} Entity EJB 035 * This tests the common path of a Servlet calling an Entity EJB to get data 036 * In this servlet the call is made the through the Quote EJBs Remote Interface 037 * 038 */ 039 040 public class PingServlet2EntityRemote extends HttpServlet 041 { 042 private static String initTime; 043 private static int hitCount; 044 private static QuoteHome quoteHome; 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 070 Quote quote = null; 071 String symbol = null; 072 073 StringBuffer output = new StringBuffer(100); 074 output.append( 075 "<html><head><title>Servlet2EntityRemote</title></head>" 076 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2EntityRemote<BR></FONT>" 077 + "<FONT size=\"-1\" color=\"#000066\"><BR>PingServlet2EntityRemote performs a JNDI lookup" 078 + " on the QuoteBean Remote Home and then gets the price of a random symbol (generated by TradeConfig)" 079 + " through the Remote Interface"); 080 try 081 { 082 //this is just a large general catch block. 083 084 //it is important only to look up the home once. 085 if (quoteHome == null) 086 { 087 //make sure that only one thread looks up the home 088 synchronized (lock) 089 { 090 if (quoteHome == null) 091 { 092 output.append( 093 "<HR>Performing JNDI lookup and creating reference to QuoteHome</B>"); 094 try 095 { 096 //do not pass an environment so that it uses the system env. 097 InitialContext ic = new InitialContext(); 098 //lookup and narrow (cast) the reference to the ejbHome. 099 quoteHome = (QuoteHome) PortableRemoteObject.narrow( 100 ic.lookup("java:comp/env/ejb/Quote"), 101 QuoteHome.class); 102 } 103 catch (Exception ne) 104 { 105 //wrap and throw the exception for handling 106 Log.error(ne,"web_primtv.PingServlet2Entity.doGet(...): error looking up QuoteHome"); 107 throw ne; 108 } 109 } 110 } 111 } 112 //generate random symbol 113 try { 114 int iter = TradeConfig.getPrimIterations(); 115 for (int ii = 0; ii < iter; ii++) { 116 //get a random symbol to look up and get the key to that symbol. 117 symbol = TradeConfig.rndSymbol(); 118 //find the EntityInstance. 119 quote = quoteHome.findByPrimaryKey(symbol); 120 } 121 } 122 catch (Exception e) 123 { 124 Log.error("web_primtv.PingServlet2Entity.doGet(...): error performing findByPrimaryKey"); 125 throw e; 126 } 127 //get the price and print the output. 128 QuoteDataBean quoteData = quote.getDataBean(); 129 130 output.append("<HR>initTime: " + initTime + "<BR>Hit Count: ").append( 131 hitCount++); 132 output 133 .append("<HR>Quote Information<BR><BR> " + quoteData.toHTML()); 134 output.append("</font><HR></body></html>"); 135 out.println(output.toString()); 136 } 137 catch (Exception e) 138 { 139 Log.error(e,"PingServlet2Entity.doGet(...): error"); 140 //this will send an Error to teh web applications defined error page. 141 res.sendError( 142 500, 143 "PingServlet2Entity.doGet(...): error" 144 + e.toString()); 145 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 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 initTime = new java.util.Date().toString(); 168 //set this to null, this will be initialized in the doGet method. 169 quoteHome = null; 170 //this lock is used to synchronize initialization of the EJBHome 171 lock = new Integer(99); 172 173 } 174 private java.lang.Integer lock; 175 }