Java tutorial
/* * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation". * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA; * vea la licencia "GNU General Public License" para obtener mas informacin. */ package ips1ap101.ejb.core; import java.text.MessageFormat; import java.util.Collections; import java.util.HashMap; import java.util.Map; import javax.naming.InitialContext; import javax.naming.NamingException; import ips1ap101.lib.core.CoreBundle; import ips1ap101.lib.core.app.Bitacora; import ips1ap101.lib.core.constant.EAC; import ips1ap101.lib.core.util.EA; import org.apache.commons.lang.StringUtils; /** * @author Jorge Campins */ public class BeanLocator { private static Map<String, Object> cache; private static final String BEANS_SUFFIX; private static final String LOCAL_SUFFIX; static { String name = BeanLocator.class.getSimpleName(); cache = Collections.synchronizedMap(new HashMap<String, Object>()); BEANS_SUFFIX = CoreBundle.getString(name, "beans_suffix"); LOCAL_SUFFIX = CoreBundle.getString(name, "local_suffix"); } public static synchronized Object getBean(Class<?> local) { Bitacora.trace(BeanLocator.class, "getBean", local); if (local == null) { return null; } Object bean; String name = local.getName(); if (cache.containsKey(name)) { bean = cache.get(name); } else { bean = lookup(local); cache.put(name, bean); } return bean; } private static Object lookup(Class<?> local) { Bitacora.trace(BeanLocator.class, "lookup", local); String key = EAC.JNDI_EJB_LOOKUP_PATTERN; String pattern = EA.getString(key); String name = local.getSimpleName(); String root = StringUtils.removeEnd(name, LOCAL_SUFFIX); String arg0 = root + BEANS_SUFFIX; String arg1 = local.getName(); String jndi = MessageFormat.format(pattern, arg0, arg1); Bitacora.trace(key + "=" + pattern); Bitacora.trace(key + "=" + jndi); try { Object object = InitialContext.doLookup(jndi); boolean assignable = object != null && local.isAssignableFrom(object.getClass()); Bitacora.trace(arg0 + "=" + object + ", assignable=" + assignable); return object; } catch (NamingException ex) { return null; } } }