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 024 import org.apache.geronimo.samples.daytrader.ejb.*; 025 import org.apache.geronimo.samples.daytrader.util.*; 026 027 import org.apache.geronimo.samples.daytrader.*; 028 029 /** 030 * Primitive to test Entity Container Managed Relationshiop One to One 031 * Servlet will generate a random userID and get the profile for that user using a {@link trade.Account} Entity EJB 032 * This tests the common path of a Servlet calling a Session to Entity EJB to get CMR One to One data 033 * 034 */ 035 036 public class PingServlet2Session2CMROne2One extends HttpServlet 037 { 038 private static String initTime; 039 private static int hitCount; 040 private static TradeHome tradeHome; 041 042 /** 043 * forwards post requests to the doGet method 044 * Creation date: (11/6/2000 10:52:39 AM) 045 * @param res javax.servlet.http.HttpServletRequest 046 * @param res2 javax.servlet.http.HttpServletResponse 047 */ 048 public void doPost(HttpServletRequest req, HttpServletResponse res) 049 throws ServletException, IOException 050 { 051 doGet(req, res); 052 } 053 054 /** 055 * this is the main method of the servlet that will service all get requests. 056 * @param request HttpServletRequest 057 * @param responce HttpServletResponce 058 **/ 059 public void doGet(HttpServletRequest req, HttpServletResponse res) 060 throws IOException, ServletException 061 { 062 063 res.setContentType("text/html"); 064 java.io.PrintWriter out = res.getWriter(); 065 066 Trade trade = null; 067 String userID = null; 068 069 StringBuffer output = new StringBuffer(100); 070 output.append( 071 "<html><head><title>Servlet2Session2CMROne20ne</title></head>" 072 + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2Session2CMROne2One<BR></FONT>" 073 + "<FONT size=\"-1\" color=\"#000066\"><BR>PingServlet2Session2CMROne2One uses the Trade Session EJB" 074 + " to get the profile for a user using an EJB 2.0 CMR one to one relationship"); 075 try 076 { 077 //this is just a large general catch block. 078 079 //it is important only to look up the home once. 080 if (tradeHome == null) 081 { 082 //make sure that only one thread looks up the home 083 synchronized (lock) 084 { 085 if (tradeHome == null) 086 { 087 output.append( 088 "<HR>Performing JNDI lookup and creating reference to TradeHome</B>"); 089 try 090 { 091 //do not pass an environment so that it uses the system env. 092 InitialContext ic = new InitialContext(); 093 //lookup and narrow (cast) the reference to the ejbHome. 094 tradeHome = (TradeHome) ic.lookup("java:comp/env/ejb/Trade"); 095 } 096 catch (Exception ne) 097 { 098 //wrap and throw the exception for handling 099 Log.error(ne,"PingServlet2Session2CMROne2One.doGet(...): error looking up TradeHome"); 100 throw ne; 101 } 102 } 103 } 104 } 105 106 AccountProfileDataBean accountProfileData = null; 107 int iter = TradeConfig.getPrimIterations(); 108 for (int ii = 0; ii < iter; ii++) { 109 userID = TradeConfig.rndUserID(); 110 trade = tradeHome.create(); 111 //get the price and print the output. 112 accountProfileData = trade.getAccountProfileData(userID); 113 } 114 115 output.append("<HR>initTime: " + initTime + "<BR>Hit Count: ").append( 116 hitCount++); 117 output 118 .append("<HR>One to One CMR access of AccountProfile Information from Account Entity<BR><BR> " + accountProfileData.toHTML()); 119 output.append("</font><HR></body></html>"); 120 out.println(output.toString()); 121 } 122 catch (Exception e) 123 { 124 Log.error(e,"PingServlet2Session2CMROne2One.doGet(...): error"); 125 //this will send an Error to teh web applications defined error page. 126 res.sendError( 127 500, 128 "PingServlet2Session2CMROne2One.doGet(...): error" 129 + e.toString()); 130 131 } 132 } 133 134 /** 135 * returns a string of information about the servlet 136 * @return info String: contains info about the servlet 137 **/ 138 139 public String getServletInfo() 140 { 141 return "web primitive, tests Servlet to Entity EJB path"; 142 } 143 144 /** 145 * called when the class is loaded to initialize the servlet 146 * @param config ServletConfig: 147 **/ 148 public void init(ServletConfig config) throws ServletException 149 { 150 super.init(config); 151 hitCount = 0; 152 initTime = new java.util.Date().toString(); 153 //set this to null, this will be initialized in the doGet method. 154 tradeHome = null; 155 //this lock is used to synchronize initialization of the EJBHome 156 lock = new Integer(99); 157 158 } 159 private java.lang.Integer lock; 160 }