Enterprise Java Development@TOPIC@
Naming - provides a "white pages" lookup of an object based on name
Directory - provides a "yellow pages" lookup of an object based on properties
JavaEE providers required to supply Naming implementation
Naming consists of hierarchical Contexts (directories) and Objects
Reference to JNDI Naming tree through javax.naming.InitialContext
InitialContext jndi = null;
try {
jndi = new InitialContext();
...
Object object = jndi.lookup(jndiName);
} catch (NamingException ex) {
throw new EJBException(ex);
}
Lookups are passed a JNDI name
JNDI name have prefixes that can given them special meaning
(no prefix) - global namespace (available remotely)
java: - namespace available only within JVM
java:comp - an Enterprise Naming Context (ENC) relative to a specific component
ejb: - namespace for use by JBoss EJB Client
JavaEE resources are registered in the JNDI tree
Not all resources are available from remote clients
Resources in the global JNDI Naming Context are available to all clients
jms/RemoteConnectionFactory jms/queue/test jms/topic/test enc-config-example-ejb/XMLConfiguredEJB!ejava.ejb.examples.encconfig.ejb.JNDIReaderRemote
You can locate these global names using the java:/jboss/exported local context
java:jboss/exported/jms/RemoteConnectionFactory java:jboss/exported/jms/queue/test java:jboss/exported/jms/topic/test java:jboss/exported/enc-config-example-ejb/XMLConfiguredEJB!ejava.ejb.examples.encconfig.ejb.JNDIReaderRemote
Resources in the local JNDI Naming Context are available to components within the server
java:jboss/datasources/ExampleDS java:jboss/jaxr/ConnectionFactory java:jboss/mail/Default java:/ConnectionFactory java:/JmsXA java:/queue/test java:/topic/test
JavaEE 6/EJB3.1 added a portable JNDI naming standard for EJB that provides scoping for global, application, and module for the EJB's local and remote interfaces
java:global/enc-config-example-ejb/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB java:app/enc-config-example-ejb/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB java:module/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB java:global/enc-config-example-ejb/InjectedEJB java:app/enc-config-example-ejb/InjectedEJB java:module/InjectedEJB
Component-specific namespace within JNDI tree
Holds references to values and objects in the environment
Theoretical equivalent to Unix soft links
ln -s <some physical file path> $HOME/<my logical path>
Accessible through "java:comp/env/(encname)" when using InitialContext
Context jndi = new InitialContext();
String jndiName = "java:comp/env/" + encname;
Object object = jndi.lookup(jndiName);
Accessible through "(encname)" when using injected SessionContext
private @Resource SessionContext ctx;
...
Object object = ctx.lookup(encname);
Populated into ENC using
Annotations
@Resource(lookup="java:jboss/datasources/ExampleDS", name="jdbc/ds2")
private DataSource ds2;
XML using META-INF/ejb-jar.xml
<resource-ref>
<res-ref-name>jdbc/ds2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<lookup-name>java:jboss/datasources/ExampleDS</lookup-name>
</resource-ref>
XML using META-INF/jboss-ejb3.xml
<resource-ref>
<res-ref-name>jdbc/ds3</res-ref-name>
<jndi-name>java:jboss/datasources/ExampleDS</jndi-name>
</resource-ref>
Injected into EJB using
Annotations without ENC
@Resource(lookup="java:jboss/datasources/ExampleDS")
private DataSource ds1;
Annotations with populated ENC
@Resource(name="jdbc/ds3")
private DataSource ds3;
XML using META-INF/ejb-jar.xml
<resource-ref>
<res-ref-name>jdbc/ds1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>ds1</injection-target-name>
</injection-target>
<lookup-name>java:jboss/datasources/ExampleDS</lookup-name>
</resource-ref>
Perform actions on injected resources after injection
complete using @PostConstruct
@PostConstruct
public void init() {
...
}