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;
018    
019    import javax.servlet.*;
020    import javax.servlet.http.*;
021    
022    import org.apache.geronimo.samples.daytrader.util.*;
023    
024    import java.io.IOException;
025    import java.math.BigDecimal;
026    import org.apache.geronimo.samples.daytrader.*;
027    
028    public class TestServlet extends HttpServlet {
029    
030    
031            public void init(ServletConfig config) throws ServletException
032            {
033                    super.init(config);
034            }
035            
036            
037       /**
038            * Process incoming HTTP GET requests
039            *
040            * @param request Object that encapsulates the request to the servlet
041            * @param response Object that encapsulates the response from the servlet
042            */
043            public void doGet(HttpServletRequest request, HttpServletResponse response)
044                    throws ServletException, IOException
045            {
046                    performTask(request,response);
047            }
048    
049       /**
050            * Process incoming HTTP POST requests
051            *
052            * @param request Object that encapsulates the request to the servlet
053            * @param response Object that encapsulates the response from the servlet
054            */
055            public void doPost(HttpServletRequest request, HttpServletResponse response)
056                    throws ServletException, IOException
057            {
058                    performTask(request,response);
059            }       
060    
061       /**
062            * Main service method for TradeAppServlet
063            *
064            * @param request Object that encapsulates the request to the servlet
065            * @param response Object that encapsulates the response from the servlet
066            */      
067            public void performTask(HttpServletRequest req, HttpServletResponse resp)
068                    throws ServletException, IOException 
069            {
070                    try {
071                            Log.debug("Enter TestServlet doGet");
072                            TradeConfig.runTimeMode = TradeConfig.DIRECT;
073                            for (int i=0; i<10; i++) 
074                            {
075                                    new TradeAction().createQuote("s:"+i, "Company " + i, new BigDecimal(i*1.1));
076                            }
077                            /*
078                            
079                            AccountDataBean accountData = new TradeAction().register("user1", "password", "fullname", "address", 
080                                                                                            "email", "creditCard", new BigDecimal(123.45), false);
081    
082                            OrderDataBean orderData = new TradeAction().buy("user1", "s:1", 100.0);
083                            orderData = new TradeAction().buy("user1", "s:2", 200.0);
084                            Thread.sleep(5000);
085                            accountData = new TradeAction().getAccountData("user1");
086                            Collection holdingDataBeans = new TradeAction().getHoldings("user1");
087                            PrintWriter out = resp.getWriter();
088                            resp.setContentType("text/html");
089                            out.write("<HEAD></HEAD><BODY><BR><BR>");
090                            out.write(accountData.toString());
091                            Log.printCollection("user1 Holdings", holdingDataBeans);
092                            ServletContext sc  = getServletContext();
093                            req.setAttribute("results", "Success");
094                            req.setAttribute("accountData", accountData);
095                            req.setAttribute("holdingDataBeans", holdingDataBeans);
096                            getServletContext().getRequestDispatcher("/tradehome.jsp").include(req, resp);
097                            out.write("<BR><BR>done.</BODY>");
098                            */
099                    }
100                    catch (Exception e)
101                    {
102                            Log.error("TestServletException", e);
103                    }
104            }
105    }
106