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 * This primitive is designed to run inside the TradeApplication and relies upon 033 * the {@link trade_client.TradeConfig} class to set configuration parameters. 034 * PingServlet2SessionEJB tests key functionality of a servlet call to a 035 * stateless SessionEJB. 036 * This servlet makes use of the Stateless Session EJB {@link trade.Trade} by calling 037 * calculateInvestmentReturn with three random numbers. 038 * 039 */ 040 public class PingServlet2Session extends HttpServlet { 041 042 private static String initTime; 043 private static int hitCount; 044 private static TradeHome tradeHome; 045 046 047 /** 048 * forwards post requests to the doGet method 049 * Creation date: (11/6/2000 10:52:39 AM) 050 * @param res javax.servlet.http.HttpServletRequest 051 * @param res2 javax.servlet.http.HttpServletResponse 052 */ 053 public void doPost(HttpServletRequest req, HttpServletResponse res) 054 throws ServletException, IOException { 055 doGet(req, res); 056 } 057 058 059 /** 060 * this is the main method of the servlet that will service all get requests. 061 * @param request HttpServletRequest 062 * @param responce HttpServletResponce 063 **/ 064 065 public void doGet(HttpServletRequest req, HttpServletResponse res) 066 throws IOException, ServletException { 067 068 res.setContentType("text/html"); 069 java.io.PrintWriter out = res.getWriter(); 070 Trade trade = null; 071 //use a stringbuffer to avoid concatenation of Strings 072 StringBuffer output = new StringBuffer(100); 073 output.append( 074 "<html><head><title>PingServlet2Session</title></head>" 075 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2Session<BR></FONT>" 076 + "<FONT size=\"-1\" color=\"#000066\">" 077 + "Tests the basis path from a Servlet to a Session Bean."); 078 079 //get a reference to the TradeBean (session bean) Home 080 try 081 { 082 //we only want to look up the home once 083 if (tradeHome == null) 084 { 085 //we only want one thread to create the EJBHome 086 synchronized (lock) 087 { 088 if (tradeHome == null) 089 { 090 091 output.append("<HR><B>Performing JNDI lookup to create a TradeHome</B>"); 092 try 093 { 094 //I am going to use the sytem environment that is set by 095 //trade_client.TradeConfig. 096 InitialContext ic = new InitialContext(); 097 098 tradeHome = 099 (TradeHome) PortableRemoteObject.narrow( 100 ic.lookup("java:comp/env/ejb/Trade"), 101 TradeHome.class); 102 } 103 catch (Exception ne) 104 { 105 Log.error(ne,"PingServlet2Session.doGet(...): errorj looking up TradeHome"); 106 throw ne; 107 } //end of catch 108 } 109 } 110 } 111 //tradeHome will be a reference to TradeHome. 112 try 113 { 114 //create a new Trade instance 115 trade = tradeHome.create(); 116 117 //create three random numbers 118 double rnd1 = Math.random() * 1000000; 119 double rnd2 = Math.random() * 1000000; 120 double rnd3 = Math.random() * 1000000; 121 122 //use a function to do some work. 123 double increase = 0.0; 124 int iter = TradeConfig.getPrimIterations(); 125 for (int ii = 0; ii < iter; ii++) { 126 increase = trade.investmentReturn(rnd1, rnd2); 127 } 128 129 //write out the output 130 output.append("<HR>initTime: " + initTime); 131 output.append("<BR>Hit Count: " + hitCount++); 132 output.append("<HR>Investment Return Information <BR><BR>investment: " + rnd1); 133 output.append("<BR>current Value: " + rnd2); 134 output.append( 135 "<BR>investment return " + increase + "<HR></FONT></BODY></HTML>"); 136 out.println(output.toString()); 137 138 } 139 catch (Exception e) 140 { 141 Log.error("PingServlet2Session.doGet(...):exception calling trade.investmentReturn "); 142 throw e; 143 } 144 } //this is where I actually handle the exceptions 145 catch (Exception e) 146 { 147 Log.error(e, "PingServlet2Session.doGet(...): error"); 148 res.sendError(500, "PingServlet2Session.doGet(...): error, " + e.toString()); 149 150 } 151 } 152 153 154 155 156 /** 157 * returns a string of information about the servlet 158 * @return info String: contains info about the servlet 159 **/ 160 161 public String getServletInfo() 162 { 163 return "web primitive, configured with trade runtime configs, tests Servlet to Session EJB path"; 164 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 super.init(config); 172 hitCount = 0; 173 initTime = new java.util.Date().toString(); 174 tradeHome = null; 175 //for synchronization 176 lock = new Integer(99); 177 } 178 179 180 181 182 ; 183 184 185 private java.lang.Integer lock;}