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