package samplehttp.servlet;

//import java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

//import javax
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.rmi.PortableRemoteObject;
import samplehttp.beans.SessHome;
import samplehttp.beans.Sess;
import samplehttp.beans.SessLocalHome;
import samplehttp.beans.SessLocal;

/**
 * 
 */
public class SampleServlet extends HttpServlet {

    /**
     * Called by the server (via the service method) to allow a servlet to 
     * handle a GET request. 
     * @param request an HttpServletRequest object that contains the request 
     * the client has made of the servlet
     * @param response an HttpServletResponse object that contains the
     * response the servlet sends to the client
     * @throws IOException if an input or output error is detected when the 
     * servlet handles the GET request
     * @throws ServletException if the request for the GET could not be handled
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        //System.out.println(Thread.currentThread().getName()+"  SampleServlet doGet");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>");
        out.println("Sample Servlet </title>");
        out.println("</head>");
        out.println("<body style=\"background : white; color : black;\">");
        out.println("<h1>Sample Servlet accessing a protected EJB");

        out.println("<h2>Initial context / UserTransaction </h2>");
        out.println("<ul>");
        Context initialContext = null;
        try {
            initialContext = new InitialContext();
            out.println("<li>Initial context OK</li>");
        } catch (Exception e) {
            out.print("<li>Cannot get initial context for JNDI: ");
            out.println(e + "</li>");
            return;
        }
     
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
 
        //System.out.println(Thread.currentThread().getName()+"  SampleServlet doPost");
        StringTokenizer st = new StringTokenizer(request.getParameter("param"));
        String timetowait = (String)st.nextElement();
        String flag = (String)st.nextElement();
        //System.out.println("timetowait = "+timetowait);
        Long lg = new Long(timetowait.trim());
        long  twait = lg.longValue();

        int  ejbflg  = new Integer(flag).intValue(); 
        // ejbflg = 0 ---> no ejb
        // ejbflg = 1 ---> ejb via remote interface
        // ejbflg = 2 ---> ejb via local interface
        // ejbflg = 3 ---> ejb via remote interface + getConnection
       
        //System.out.println("waiting : "+twait +" ejb = "+ejbflg);
        if( ejbflg == 0 ) {
            //System.out.println("No ejb");
            try {
                Thread.currentThread().sleep(twait);
            } catch (InterruptedException e) {
                //System.out.println("sleep:"+e);
            }

        } else {

            Context initialContext = null;
            try {
                initialContext = new InitialContext();
 
            } catch (Exception e) {
                System.err.println("Cannot get initial context for JNDI: "+e);
                return;
            } if( ejbflg == 2) {
                // ejbflg = 2 ---> ejb via local interface
                SessLocalHome sh = null;
                SessLocal s = null;
                try {
                    sh = (SessLocalHome) initialContext.lookup("java:comp/env/ejb/SessLocal");
                    s = sh.create();
                    s.process(twait);
                    s.remove();
                } catch (Exception e) {
                    System.err.println("Exception caught: " + e );
                    return;
                }
            } else {
                // ejbflg = 1 or 3 ---> ejb via remote interface
                SessHome sh = null;
                Sess s = null;
                try {
                    sh = (SessHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Sess"), SessHome.class);
                    s = sh.create();
                    if( ejbflg == 1) {
                        // ejbflg = 1 ---> ejb via remote interface
                        s.process(twait);
                    } else {
                        // ejbflg = 3 ---> ejb business method + getConnection
                        s.processwithconn(twait);
                    }
                    s.remove();
                } catch (Exception e) {
                    System.err.println("Exception caught: " + e );
                    return;
                }
            }
        }
        String okmsg = "OK\n";
        response.setContentLength(okmsg.length());
        PrintWriter out = response.getWriter();
        out.print(okmsg);
        

    }

}
