Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The "JEE-Annotation" page has been changed by LennyPrimak:
http://wiki.apache.org/tapestry/JEE-Annotation?action=diff&rev1=4&rev2=5

Comment:
added JNDIObjectLocator

  
  }}}
  
+ == JNDIObjectLocator.java, variant taken from Tapestry JumpStart ==
+ 
+ {{{
+ import java.util.Collections;
+ import java.util.HashMap;
+ import java.util.Map;
+ import javax.naming.InitialContext;
+ import javax.naming.NameNotFoundException;
+ import javax.naming.NamingException;
+ import lombok.Getter;
+ import lombok.SneakyThrows;
+ 
+ /**
+  * JNDIObjectLocator is used to centralize all JNDI lookups. It minimises the 
overhead of JNDI lookups by caching the
+  * objects it looks up.
+  * 
+  * @author Geoff Callendar
+  * Enhancements by Lenny Primak
+  */
+ public class JNDIObjectLocator
+ {
+     @SneakyThrows(NamingException.class)
+     public JNDIObjectLocator()
+     {
+         initialContext = new InitialContext();
+     }
+     
+     
+     @SneakyThrows(NamingException.class)
+     public<T> T getObject(Class<T> beanClass)
+     {
+         String name = EJBAnnotationWorker.guessByType(beanClass.getName());
+         return getObject(EJBAnnotationWorker.prependPortableName(name));
+     }
+     
+     
+     @SuppressWarnings("unchecked")
+     public<T> T getObject(String jndiName) throws NamingException
+     {
+         return (T)getJNDIObject(jndiName);
+     }
+ 
+     
+     public synchronized void clear()
+     {
+         jndiObjectCache.clear();
+     }
+     
+ 
+     public Object getJNDIObject(String jndiName) throws NamingException
+     {
+         Object jndiObject = jndiObjectCache.get(jndiName);
+ 
+         if (jndiObject == null && !jndiObjectCache.containsKey(jndiName))
+         {
+             try
+             {
+                 jndiObject = lookup(jndiName);
+                 jndiObjectCache.put(jndiName, jndiObject);
+             } catch (NamingException e)
+             {
+                 clear();
+                 throw e;
+             }
+         }
+         return jndiObject;
+     }
+ 
+     
+     private synchronized Object lookup(String name) throws NamingException
+     {
+ 
+         // Recheck the cache because the name we're looking for may have been 
added while we were waiting for sync.
+ 
+         if (!jndiObjectCache.containsKey(name))
+         {
+             try
+             {
+                 return getInitialContext().lookup(name);
+             } catch (NameNotFoundException e)
+             {
+                 clear();
+                 throw e;
+             }
+         } else
+         {
+             return jndiObjectCache.get(name);
+         }
+     }
+ 
+     
+     @Getter private final InitialContext initialContext;
+     private final Map<String, Object> jndiObjectCache = 
Collections.synchronizedMap(new HashMap<String, Object>());
+ }
+ 
+ }}}
+ 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tapestry.apache.org
For additional commands, e-mail: dev-h...@tapestry.apache.org

Reply via email to