0 Replies Latest reply on Nov 18, 2009 4:12 PM by redguard

    caching the jndi call

    redguard

      hi,

      i got my ejb's up and running

      in my web project i call them like that

      TraxManager TraxManager = (TraxManager)context.lookup("TraxService/remote");
      


      in my web project i implement this call in each of managed beans, which no an optimal solution

      so i try to do a generic method ==>


      import java.util.Collections;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Properties;
      
      import javax.ejb.EJBHome;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      public class EJBHomeFactory {
      
       private static Map<String, Object> cache=Collections.synchronizedMap(new HashMap());; //used to hold references to EJBHomes/JMS Resources for re-use
      
      
       private EJBHomeFactory() throws ServiceLocatorException {
       try {
       //THIS DOESN'T GET EXECUTED :(
       cache = Collections.synchronizedMap(new HashMap());
       } catch (Exception e) {
       throw new ServiceLocatorException(e);
       }
       }
      
      
       public static Object lookupObject(String jndiName) throws ServiceLocatorException {
       EJBHome home = null;
       Context context = null;
      
       try {
       if (cache.containsKey((Object)jndiName)) {
       return cache.get(jndiName);
       } else {
       Properties props = new Properties();
       props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
       props.setProperty("java.naming.provider.url", "jnp://192.168.1.201:1099");
       props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
       props.setProperty("jnp.socket.Factory", "org.jnp.interfaces.TimedSocketFactory");
       context = new InitialContext(props);
       //Object obj = context.lookup(jndiName);
       cache.put(jndiName, context.lookup(jndiName));
       }
       } catch (NamingException ne) {
       throw new ServiceLocatorException(ne);
       } catch (Exception e) {
       throw new ServiceLocatorException(e);
       }
       return cache.get(jndiName);
      
       }
      
      
      
       public Map<String, Object> getCache() {
       return cache;
       }
      
      
      
       public void setCache(Map<String, Object> cache) {
       EJBHomeFactory.cache = cache;
       }
      
      
      }
      
      
      


      i test with 2 class, so that the first one , fill the map and the second should find a a map with the key of the jndi name , but it never found it,
       if (cache.containsKey((Object)jndiName)) {
       return cache.get(jndiName);
       }

      is never executed


      and thanks