/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.examples.ejb.protocol;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.jfox.ejb.ExtendedEJBHome;
import org.jfox.ejb.meta.Protocol;
import org.jfox.examples.ejb.stateless.TestStateless;
import org.jfox.examples.ejb.stateless.TestStatelessHome;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class TestProtocolBean implements SessionBean {
    public TestProtocolBean() {
    }

    public void setSessionContext(SessionContext sessionContext) throws EJBException {
    }

    public void ejbRemove() throws EJBException {
    }

    public void ejbActivate() throws EJBException {
    }

    public void ejbPassivate() throws EJBException {
    }

    public void ejbCreate() throws CreateException {
    }

    public String invokeByJRMP() {
        try {
            TestStatelessHome home = getTestStatelessOnePhase();
            ((ExtendedEJBHome) home).useProtocol(Protocol.JRMP);
            TestStateless statelessEjb = home.create();
            String voice = statelessEjb.getVoice();
            return voice;

        }
        catch(Exception e) {
            e.printStackTrace();
            throw new EJBException(e);
        }
    }

    public String invokeByJRMP_SSL() {
        try {
            TestStatelessHome home = getTestStatelessOnePhase();
            ((ExtendedEJBHome) home).useProtocol(Protocol.JRMP_SSL);
            TestStateless statelessEjb = home.create();
            String voice = statelessEjb.getVoice();
            return voice;

        }
        catch(Exception e) {
            e.printStackTrace();
            throw new EJBException(e);
        }

    }

    public String invokeBySOAP() {
        try {
            TestStatelessHome home = getTestStatelessTwoPhase();
            ((ExtendedEJBHome) home).useProtocol(Protocol.SOAP);
            TestStateless statelessEjb = home.create();
            String voice = statelessEjb.getVoice();
            return voice;

        }
        catch(Exception e) {
            e.printStackTrace();
            throw new EJBException(e);
        }

    }

    public String invokeByLOCAL() {
        try {
            TestStatelessHome home = getTestStatelessTwoPhase();
            ((ExtendedEJBHome) home).useProtocol(Protocol.LOCAL);
            TestStateless statelessEjb = home.create();
            String voice = statelessEjb.getVoice();
            return voice;

        }
        catch(Exception e) {
            e.printStackTrace();
            throw new EJBException(e);
        }

    }

    private static TestStatelessHome getTestStatelessOnePhase() throws Exception {
        Context ctx = new InitialContext();
        Object home = ctx.lookup("java:comp/env/ejb/TestStatelessEJB");
        TestStatelessHome statelessHome = (TestStatelessHome) javax.rmi.PortableRemoteObject.narrow(home, TestStatelessHome.class);
        return statelessHome;
    }

    private static TestStatelessHome getTestStatelessTwoPhase() throws Exception {
        Context initCtx = new InitialContext();
        Context ctx = (Context) initCtx.lookup("java:comp/env");
        Object home = ctx.lookup("ejb/TestStatelessEJB");
        TestStatelessHome statelessHome = (TestStatelessHome) javax.rmi.PortableRemoteObject.narrow(home, TestStatelessHome.class);
        return statelessHome;
    }

}
