Initialize the target EJB home - Java javax.naming

Java examples for javax.naming:Context

Description

Initialize the target EJB home

Demo Code


import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.rmi.CORBA.Util;

public class Main{
    private static final String LIBRARY_APP_NAMING_PROVIDER = "corbaloc:iiop:localhost:2809";
    private static final String WEBSPHERE_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory";
    private static final Map ctxMap = new HashMap();
    /**/*w w  w .ja v a2  s. c  o m*/
     * Initialize the target home
     * @return null if JNDI does not contain a specified reference or if the 
     * if the specified class could not be loaded using Class.forName
     **/
    public static EJBHome getEJBHome(Map cache, String clazz, String jndi)
            throws ClassNotFoundException, NamingException {
        Class ejbHome = Class.forName(clazz);
        EJBHome home = (EJBHome) cache.get(jndi);
        if (home == null) {
            home = lookupEJBHome(jndi, ejbHome);
            if (home != null)
                cache.put(jndi, home);
        }
        return home;
    }
    /**
     * Initialize the target home
     **/
    public static EJBHome lookupEJBHome(String jndi, Class clazz)
            throws NamingException {
        InitialContext ctx = getInitialContext(WEBSPHERE_FACTORY,
                LIBRARY_APP_NAMING_PROVIDER);
        return (ctx == null) ? null : (EJBHome) PortableRemoteObject
                .narrow(ctx.lookup(jndi), clazz);
    }
    public static InitialContext getInitialContext(String factory,
            String provider) throws NamingException {
        if (!ctxMap.containsKey(factory + "/" + provider)) {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, WEBSPHERE_FACTORY);
            env.put(Context.PROVIDER_URL, LIBRARY_APP_NAMING_PROVIDER);
            ctxMap.put(factory + "/" + provider, new InitialContext(env));
        }
        InitialContext ctx = (InitialContext) ctxMap.get(factory + "/"
                + provider);
        return ctx;
    }
}

Related Tutorials