// SessSLR.java
// Stateless Session bean

package samplehttp.beans;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;


/**
 *
 */
public class SessSLR implements SessionBean {

    static private Logger logger = null;
    SessionContext ejbContext;
    private DataSource dataSource = null;




    private Connection getConnection() throws EJBException, SQLException {
        if (dataSource == null) {
            // Finds DataSource from JNDI
            Context    initialContext = null;
            try {
                initialContext = new InitialContext();
                dataSource = (DataSource)initialContext.lookup("java:comp/env/jdbc/SessDs");
            } catch (Exception e) {
                System.out.println("Cannot lookup dataSource"+e);
                throw new javax.ejb.EJBException("Cannot lookup dataSource ");
            }
        }
        return dataSource.getConnection();
    }


    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------

    /** 
     * Set the associated session context. The container calls this method
     * after the instance creation. 
     * The enterprise Bean instance should store the reference to the context
     * object in an instance variable. 
     * This method is called with no transaction context.
     *
     * @param sessionContext A SessionContext interface for the instance.
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */
    public void setSessionContext(SessionContext ctx) {
        if (logger == null) {
            logger = Log.getLogger("org.objectweb.jonas_tests");
        }
        logger.log(BasicLevel.DEBUG, "");
        ejbContext = ctx;
    }
	
    /**
     * A container invokes this method before it ends the life of the session object. 
     * This happens as a result of a client's invoking a remove operation, or when a 
     *  container decides to terminate the session object after a timeout. 
     * This method is called with no transaction context. 
     *
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */
    public void ejbRemove() {
        logger.log(BasicLevel.DEBUG, "");
    }
	
    /**
     * The Session bean must define 1 or more ejbCreate methods.
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreate() throws CreateException {
        logger.log(BasicLevel.DEBUG, "");
    }


    /**
     * A container invokes this method on an instance before the instance
     * becomes disassociated with a specific EJB object.
     */
    public void ejbPassivate() {
        logger.log(BasicLevel.DEBUG, "");
    }

    /**
     * A container invokes this method when the instance is taken out of 
     * the pool of available instances to become associated with a specific 
     * EJB object.
     */
    public void ejbActivate() {
        logger.log(BasicLevel.DEBUG, "");
    }
    
    // ------------------------------------------------------------------
    // Sess implementation
    // ------------------------------------------------------------------

    /**
     * process
     * @ param long treatment time 
     */
    public void process(long time) {
        logger.log(BasicLevel.DEBUG, "treatment time = "+time);
        try {
            Thread.currentThread().sleep(time);
        } catch (InterruptedException e) {
            System.out.println("sleep:"+e);
        }
    }

    /**
     * process
     * @ param long treatment time 
     */
    public void processwithconn(long time) {
        logger.log(BasicLevel.DEBUG, "treatment time = "+time);
        try {
            Connection conn = getConnection();
            Thread.currentThread().sleep(time);
            conn.close();
        } catch (InterruptedException e) {
            System.out.println("sleep:"+e);
        } catch (Exception e) {
            System.out.println("exception caught: "+e);
        }
    }

}
