/* 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.ejb.connector;

import org.jfox.ejb.EJBObjectId;
import org.jfox.ejb.meta.Protocol;
import org.jfox.jndi.InitialContextHelper;

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

public class ConnectorHelper {
    public final static String INVOKER_JNDI_PREFIX = "ejb/connector";

    public static ContainerRemote lookupContainerInvoker(Protocol protocol) {
        try {
            return (ContainerRemote) javax.rmi.PortableRemoteObject.narrow(InitialContextHelper.getInitialContext().lookup("/" + INVOKER_JNDI_PREFIX + "/" + protocol.getProtocol().toLowerCase()), ContainerRemote.class);
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static ClientContainerInvoker createClientInvocationHandler(Protocol protocol, EJBObjectId objectId, ContainerRemote invoker) {
        try {
            Class invocationHandlerClass = Thread.currentThread().getContextClassLoader().loadClass(ConnectorHelper.class.getPackage().getName() + "." + protocol.getProtocol().toLowerCase() + "." + protocol.getProtocol().toUpperCase() + "ContainerInvoker");
            return (ClientContainerInvoker) invocationHandlerClass.getConstructor(new Class[]{EJBObjectId.class, ContainerRemote.class}).newInstance(new Object[]{objectId, invoker});
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getJNDIName(Protocol protocol) {
        return INVOKER_JNDI_PREFIX + "/" + protocol.getProtocol().toLowerCase();
    }

    public static void main(String[] args) {

    }
}

