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.InitialContext;
023    import javax.sql.DataSource;
024    
025    import org.apache.geronimo.samples.daytrader.util.*;
026    
027    import org.apache.geronimo.samples.daytrader.*;
028    
029    /**
030     * 
031     * PingServlet2JNDI performs a basic JNDI lookup of a JDBC DataSource
032     * 
033     */
034    
035    public class PingServlet2JNDI extends HttpServlet
036    {
037    
038            private static String initTime;
039            private static int hitCount;
040            
041            /**
042             * forwards post requests to the doGet method
043             * Creation date: (11/6/2000 10:52:39 AM)
044             * @param res javax.servlet.http.HttpServletRequest
045             * @param res2 javax.servlet.http.HttpServletResponse
046             */
047            public void doPost(HttpServletRequest req, HttpServletResponse res)
048                    throws ServletException, IOException
049            {
050                    doGet(req, res);
051            }
052            /**
053            * this is the main method of the servlet that will service all get requests.
054            * @param request HttpServletRequest
055            * @param responce HttpServletResponce
056            **/
057            public void doGet(HttpServletRequest req, HttpServletResponse res)
058                    throws ServletException, IOException
059            {
060                    res.setContentType("text/html");
061                    java.io.PrintWriter out = res.getWriter();
062    
063                    StringBuffer output = new StringBuffer(100);
064    
065                    try
066                            {
067    
068                            int iter = TradeConfig.getPrimIterations();
069                            for (int ii = 0; ii < iter; ii++) {
070                                    InitialContext context = new InitialContext();
071                                    DataSource datasource = (DataSource) context.lookup(TradeConfig.DS_NAME);
072                            }                       
073    
074                            output.append(
075                                    "<html><head><title>Ping JNDI -- lookup of JDBC DataSource</title></head>"
076                                            + "<body><HR><FONT size=\"+2\" color=\"#000066\">Ping JNDI -- lookup of JDBC DataSource</FONT><HR><FONT size=\"-1\" color=\"#000066\">Init time : "
077                                            + initTime);
078                            hitCount++;
079                            output.append("</FONT><BR>Hit Count: " + hitCount);
080                            output.append("<HR></body></html>");
081                            out.println(output.toString());
082                    }
083                    catch (Exception e)
084                    {
085                            Log.error(e, "PingServlet2JNDI -- error look up of a JDBC DataSource");
086                            res.sendError(500, "PingServlet2JNDI Exception caught: " + e.toString());
087                    }
088    
089            }
090            /** 
091             * returns a string of information about the servlet
092             * @return info String: contains info about the servlet
093             **/
094            public String getServletInfo()
095            {
096                    return "Basic JNDI look up of a JDBC DataSource";
097            }
098            /**
099            * called when the class is loaded to initialize the servlet
100            * @param config ServletConfig:
101            **/
102            public void init(ServletConfig config) throws ServletException
103            {
104                    super.init(config);
105                    hitCount = 0;
106                    initTime = new java.util.Date().toString();
107            }
108    }