package com.fp.facadeclient.helper; 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 com.fp.common.logger.APPLogger; import com.fp.facadeclient.ejb.FacadeBeanJsfRemote; public class ServiceLocatorJsf { // Nombre del ear sin .ear private static final String APP_NAME = "maiaear-2.1"; // Nombre del modulo ejb es el jar que contiene los ejb sin .jar private static final String MODULE_NAME = "facade"; // Paquete clase de la interface remota. private static final String BEAN_REMOTO = FacadeBeanJsfRemote.class.getName(); /** * Almacena una instancia de ServiceLocator. */ private static ServiceLocatorJsf cache; private final Map mbeans = new HashMap(); /** * Entrega una instancia de ServiceLocator. * @return ServiceLocator */ public static ServiceLocatorJsf getInstance() { if (ServiceLocatorJsf.cache != null) { return ServiceLocatorJsf.cache; } synchronized (ServiceLocator.class) { if (ServiceLocatorJsf.cache == null) { ServiceLocatorJsf.cache = new ServiceLocatorJsf(); } } return ServiceLocatorJsf.cache; } /** * Entrega un proxy de los ejb remotos. * @param nombesessionbean Nombre del ejb remoto a obtener un proxy. * @return FacadeBeanRemote */ public FacadeBeanJsfRemote getFacadeRemote(String nombesessionbean) throws Exception { FacadeBeanJsfRemote bean = this.mbeans.get(nombesessionbean); if (bean == null) { synchronized (this.mbeans) { bean = this.mbeans.get(nombesessionbean); if (bean == null) { APPLogger.getLogger().info("Obtiene una coneccion al EJB ==> " + nombesessionbean); bean = ServiceLocatorJsf.lookupRemoteStatelessFacadeBean(nombesessionbean); this.mbeans.put(nombesessionbean, bean); } } } return bean; } /** * Busca y entrega el proxy de un ejb con el cual se ejecuta un servicio de negocio. * @return FacadeBeanJsfRemote * @throws NamingException */ private static FacadeBeanJsfRemote lookupRemoteStatelessFacadeBean(String beanname) throws NamingException { // Tiene que ser hashtable no considerar en el pmd. Hashtable jndiProperties = new Hashtable(); // NOPMD by jorge on 6/19/12 3:36 PM jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); Context context = new InitialContext(jndiProperties); String aux = "ejb:" + ServiceLocatorJsf.APP_NAME + "/" + ServiceLocatorJsf.MODULE_NAME + "//" + beanname + "!" + ServiceLocatorJsf.BEAN_REMOTO; return (FacadeBeanJsfRemote) context.lookup(aux); } // LIbrerias // jboss-client-7.1.0.Final.jar esta bajo jbos-as-7.1.0.final\bin\client }