Package org.omnifaces.util
Class JNDIObjectLocator
- java.lang.Object
-
- org.omnifaces.util.JNDIObjectLocator
-
- All Implemented Interfaces:
Serializable
public class JNDIObjectLocator extends Object implements Serializable
JNDIObjectLocator is used to centralize JNDI lookups. It minimizes the overhead of JNDI lookups by caching the objects it looks up.Implements the ServiceLocator design pattern.
Major features are:
- thread-safe
- immutable
- serializable
- selectively disables the cache if objects are remote
Example:
locator = JNDIObjectLocator.builder().build(); MyEJB myEJB1 = locator.getObject(MyEJB.class); MyEJB myEJB2 = locator.getObject("java:module/MyEJB");
- Since:
- 3.9
- Author:
- Lenny Primak
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JNDIObjectLocator.JNDIObjectLocatorBuilder
The builder of theJNDIObjectLocator
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static JNDIObjectLocator.JNDIObjectLocatorBuilder
builder()
Returns the builder of theJNDIObjectLocator
.void
clearCache()
Clears object cache.<T> T
getEnvEntry(String name)
Same asJNDI.getEnvEntry(String)
, except that this is cached.<T> T
getObject(Class<T> beanClass)
Returns an object from JNDI based on beanClass.<T> T
getObject(String jndiName)
Returns an object based on JNDI name.<T> T
getObjectNoCache(String jndiName)
Return an object based on JNDI name, bypassing the cache.String
prependNamespaceIfNecessary(String fieldName)
Utility method used in matching fields to EJB injection points to try to find appropriate JNDI object to use for injection.protected Object
readResolve()
This deals with transient final fields correctly.
-
-
-
Method Detail
-
builder
public static JNDIObjectLocator.JNDIObjectLocatorBuilder builder()
Returns the builder of theJNDIObjectLocator
.- Returns:
- The builder of the
JNDIObjectLocator
.
-
getEnvEntry
public <T> T getEnvEntry(String name)
Same asJNDI.getEnvEntry(String)
, except that this is cached.- Type Parameters:
T
- The expected return type.- Parameters:
name
- the environment entry name relative to "java:comp/env".- Returns:
- The environment entry value associated with the given name, or
null
if there is none. - Throws:
ClassCastException
- WhenT
is of wrong type.- See Also:
JNDI.getEnvEntry(String)
-
getObject
public <T> T getObject(Class<T> beanClass)
Returns an object from JNDI based on beanClass. Uses portable object names and convention to derive appropriate JNDI name.- Type Parameters:
T
- Object type.- Parameters:
beanClass
- Type of object to look up in JNDI.- Returns:
- Resulting object, or
null
if there is none.
-
getObject
public <T> T getObject(String jndiName)
Returns an object based on JNDI name.- Type Parameters:
T
- The expected return type.- Parameters:
jndiName
- The JNDI name of the object to be retrieved.- Returns:
- The named object, or
null
if there is none. - Throws:
ClassCastException
- WhenT
is of wrong type.
-
getObjectNoCache
public <T> T getObjectNoCache(String jndiName)
Return an object based on JNDI name, bypassing the cache.- Type Parameters:
T
- The expected return type.- Parameters:
jndiName
- The JNDI name of the object to be retrieved.- Returns:
- The named object, or
null
if there is none. - Throws:
ClassCastException
- WhenT
is of wrong type.
-
clearCache
public void clearCache()
Clears object cache.
-
prependNamespaceIfNecessary
public String prependNamespaceIfNecessary(String fieldName)
Utility method used in matching fields to EJB injection points to try to find appropriate JNDI object to use for injection. It prepends the given field name with this locator's namespace when the given field name does not already start withJNDI.JNDI_NAMESPACE_PREFIX
.- Parameters:
fieldName
- The field name to prepend with this locator's name space if necessary.- Returns:
- The given field name, prepended with this locator's name space if necessary.
-
readResolve
protected Object readResolve()
This deals with transient final fields correctly.
-
-