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     *
031     * Primitive designed to run within the TradeApplication and makes use of {@link trade_client.TradeConfig}
032     * for config parameters and random stock symbols.
033     * Servlet will generate a random stock symbol and get the price of that symbol using a {@link trade.Quote} Entity EJB 
034     * This tests the common path of a Servlet calling an Entity EJB to get data
035     *
036     */
037    
038    public class PingServlet2EntityLocal extends HttpServlet
039    {
040            private static String initTime;
041            private static int hitCount;
042            private static LocalQuoteHome quoteHome;
043    
044            /**
045                     * forwards post requests to the doGet method
046                     * Creation date: (11/6/2000 10:52:39 AM)
047                     * @param res javax.servlet.http.HttpServletRequest
048                     * @param res2 javax.servlet.http.HttpServletResponse
049                     */
050            public void doPost(HttpServletRequest req, HttpServletResponse res)
051                    throws ServletException, IOException
052            {
053                    doGet(req, res);
054            }
055    
056            /**
057                    * this is the main method of the servlet that will service all get requests.
058                    * @param request HttpServletRequest
059                    * @param responce HttpServletResponce
060                    **/
061            public void doGet(HttpServletRequest req, HttpServletResponse res)
062                    throws IOException, ServletException
063            {
064    
065                    res.setContentType("text/html");
066                    java.io.PrintWriter out = res.getWriter();
067    
068                    LocalQuote quote = null;
069                    String symbol = null;
070    
071                    StringBuffer output = new StringBuffer(100);
072                    output.append(
073                            "<html><head><title>Servlet2EntityLocal</title></head>"
074                                    + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2EntityLocal<BR></FONT>"
075                                    + "<FONT size=\"-1\" color=\"#000066\"><BR>PingServlet2Entity performs a JNDI lookup"
076                                    + " on the Local QuoteBean Home and then gets the price of a random symbol (generated by TradeConfig)"
077                                    + " through the Local interface");
078                    try
079                            {
080                            //this is just a large general catch block.
081    
082                            //it is important only to look up the home once.
083                            if (quoteHome == null)
084                                    {
085                                    //make sure that only one thread looks up the home
086                                    synchronized (lock)
087                                    {
088                                            if (quoteHome == null)
089                                                    {
090                                                    output.append(
091                                                            "<HR>Performing JNDI lookup and creating reference to QuoteHome</B>");
092                                                    try
093                                                            {
094                                                            //do not pass an environment so that it uses the system env.
095                                                            InitialContext ic = new InitialContext();
096                                                            //lookup and narrow (cast) the reference to the ejbHome.  
097                                                            quoteHome = (LocalQuoteHome) ic.lookup("java:comp/env/ejb/LocalQuote");
098                                                    }
099                                                    catch (Exception ne)
100                                                            {
101                                                            //wrap and throw the exception for handling
102                                                            Log.error(ne,"web_primtv.PingServlet2Entity.doGet(...): error looking up LocalQuoteHome");
103                                                            throw ne;
104                                                    }
105                                            }
106                                    }
107                            }
108    
109                            //generate random symbol 
110                            try {
111                                    int iter = TradeConfig.getPrimIterations();
112                                    for (int ii = 0; ii < iter; ii++) {
113                                            //get a random symbol to look up and get the key to that symbol.
114                                            symbol = TradeConfig.rndSymbol();
115                                            //find the EntityInstance.
116                                            quote = quoteHome.findByPrimaryKey(symbol);
117                                    }
118                            }
119                            catch (Exception e)
120                            {
121                                    Log.error("web_primtv.PingServlet2Entity.doGet(...): error performing findByPrimaryKey");
122                                    throw e;
123                            }
124                            //get the price and print the output.
125                            QuoteDataBean quoteData = quote.getDataBean();
126    
127                            output.append("<HR>initTime: " + initTime + "<BR>Hit Count: ").append(
128                                    hitCount++);
129                            output
130                                    .append("<HR>Quote Information<BR><BR> " + quoteData.toHTML());
131                            output.append("</font><HR></body></html>");
132                            out.println(output.toString());
133                    }
134                    catch (Exception e)
135                            {
136                            Log.error(e,"PingServlet2Entity.doGet(...): error");
137                            //this will send an Error to teh web applications defined error page.
138                            res.sendError(
139                                    500,
140                                    "PingServlet2Entity.doGet(...): error"
141                                            + e.toString());
142    
143                    }
144            }
145    
146            /** 
147                     * returns a string of information about the servlet
148                     * @return info String: contains info about the servlet
149                     **/
150    
151            public String getServletInfo()
152            {
153                    return "web primitive, tests Servlet to Entity EJB path";
154            }
155    
156            /**
157                    * called when the class is loaded to initialize the servlet
158                    * @param config ServletConfig:
159                    **/
160            public void init(ServletConfig config) throws ServletException
161            {
162                    super.init(config);
163                    hitCount = 0;
164                    initTime = new java.util.Date().toString();
165                    //set this to null, this will be initialized in the doGet method.
166                    quoteHome = null;
167                    //this lock is used to synchronize initialization of the EJBHome
168                    lock = new Integer(99);
169    
170            }
171            private java.lang.Integer lock;
172    }