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 java.util.Collection;
021    import java.util.Iterator;
022    import javax.servlet.*;
023    import javax.servlet.http.*;
024    import javax.naming.*;
025    
026    import org.apache.geronimo.samples.daytrader.ejb.*;
027    import org.apache.geronimo.samples.daytrader.util.*;
028    
029    import org.apache.geronimo.samples.daytrader.*;
030    
031    /**
032     * Primitive to test Entity Container Managed Relationshiop  One to One
033     * Servlet will generate a random userID and get the profile for that user using a {@link trade.Account} Entity EJB 
034     * This tests the common path of a Servlet calling a Session to Entity EJB to get CMR One to One data
035     *
036     */
037    
038    public class PingServlet2Session2CMROne2Many extends HttpServlet
039    {
040            private static String initTime;
041            private static int hitCount;
042            private static TradeHome tradeHome;
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                    Trade  trade = null;
069                    String userID = null;
070    
071                    StringBuffer output = new StringBuffer(100);
072                    output.append(
073                            "<html><head><title>Servlet2Session2CMROne20ne</title></head>"
074                                    + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2Session2CMROne2Many<BR></FONT>"
075                                    + "<FONT size=\"-1\" color=\"#000066\"><BR>PingServlet2Session2CMROne2Many uses the Trade Session EJB"
076                                    + " to get the orders for a user using an EJB 2.0 Entity CMR one to many relationship");
077                    try
078                            {
079                            //this is just a large general catch block.
080    
081                            //it is important only to look up the home once.
082                            if (tradeHome == null)
083                                    {
084                                    //make sure that only one thread looks up the home
085                                    synchronized (lock)
086                                    {
087                                            if (tradeHome == null)
088                                                    {
089                                                    output.append(
090                                                            "<HR>Performing JNDI lookup and creating reference to TradeHome</B>");
091                                                    try
092                                                            {
093                                                            //do not pass an environment so that it uses the system env.
094                                                            InitialContext ic = new InitialContext();
095                                                            //lookup and narrow (cast) the reference to the ejbHome.  
096                                                            tradeHome = (TradeHome) ic.lookup("java:comp/env/ejb/Trade");
097                                                    }
098                                                    catch (Exception ne)
099                                                            {
100                                                            //wrap and throw the exception for handling
101                                                            Log.error(ne,"PingServlet2EntityCMROne2Many.doGet(...): error looking up TradeHome");
102                                                            throw ne;
103                                                    }
104                                            }
105                                    }
106                            }
107                            
108                            Collection orderDataBeans = null;
109                            int iter = TradeConfig.getPrimIterations();
110                            for (int ii = 0; ii < iter; ii++) {
111                                    userID = TradeConfig.rndUserID();
112                                    trade = tradeHome.create();                     
113    
114                                    //get the users orders and print the output.
115                                    orderDataBeans = trade.getOrders(userID);
116                            }                       
117    
118                            output.append("<HR>initTime: " + initTime + "<BR>Hit Count: ").append(
119                                    hitCount++);
120                            output
121                                    .append("<HR>One to Many CMR access of Account Orders from Account Entity<BR> ");
122                            output.append("<HR>User: " + userID + " currently has " + orderDataBeans.size() + " stock orders:");
123                            Iterator it = orderDataBeans.iterator();
124                            while ( it.hasNext() )
125                            {
126                                    OrderDataBean orderData = (OrderDataBean) it.next();
127                                    output.append("<BR>" + orderData.toHTML());
128                            }                               
129                            output.append("</font><HR></body></html>");
130                            out.println(output.toString());
131                    }
132                    catch (Exception e)
133                            {
134                            Log.error(e,"PingServlet2Session2CMROne2Many.doGet(...): error");
135                            //this will send an Error to teh web applications defined error page.
136                            res.sendError(
137                                    500,
138                                    "PingServlet2Session2CMROne2Many.doGet(...): error"
139                                            + e.toString());
140    
141                    }
142            }
143    
144                    /** 
145                     * returns a string of information about the servlet
146                     * @return info String: contains info about the servlet
147                     **/
148    
149            public String getServletInfo()
150            {
151                    return "web primitive, tests Servlet to Entity EJB path";
152            }
153    
154            /**
155                    * called when the class is loaded to initialize the servlet
156                    * @param config ServletConfig:
157                    **/
158            public void init(ServletConfig config) throws ServletException
159            {
160                    super.init(config);
161                    hitCount = 0;
162                    initTime = new java.util.Date().toString();
163                    //set this to null, this will be initialized in the doGet method.
164                    tradeHome = null;
165                    //this lock is used to synchronize initialization of the EJBHome
166                    lock = new Integer(99);
167    
168            }
169            private java.lang.Integer lock;
170    }