Chapter 9. Considerations 211
The code used to define the logger in the class is also ideal for a template. The
logger template can be used to insert the declaration of the log object in Java
classes:
private static LogHelper log =
LogHelper.getInstance(${enclosing_type}.class);
In this template the ${enclosing_type} is replaced with the name of the class in
which you are expanding the template.
The last template we want to mention is named ifdebug and can be used to insert
conditional debug statements. Remember that for performance reasons it is
sometimes desirable to check whether a message type is currently enabled
before preparing the message. The pattern for the ifdebug template is:
if (log.isDebugEnabled()) {
log.debug(${cursor});
}
If your logging framework supports checking whether other message types are
enabled, you may consider adding templates for them as well (for example,
ifinfo).
Using JNDI
The Java Naming and Directory Interface (JNDI) plays an important role in Java
enterprise applications. It is used for such common tasks as obtaining references
to EJB homes, finding a data source, or looking up a JavaMail session. Because
of this, virtually all Java enterprise applications have to work with JNDI in one
way or another.
Obtaining an InitialContext
The J2EE specification recommends that code accessing JNDI obtain a
reference to a JNDI InitialContext object using the default constructor, with no
arguments. Furthermore, the specification requires that the container provides
an environment in which a valid InitialContext is obtained by using the default
constructor. See Chapter 5, “Naming” in the Java 2 Platform Enterprise Edition
Specification, V1.3.
WebSphere Application Server fulfills this requirement, meaning that an
InitialContext can be obtained using the code:
import javax.naming.InitialContext;
...
InitialContext context = new InitialContext();
212 WebSphere Version 5 Application Development Handbook
This works correctly for code, such as EJBs, servlets, JSPs, and application
clients, which are executing in a WebSphere container.
It is also possible to initialize the InitialContext with settings stored in a
Hashtable passed in the constructor. The Hashtable can include settings such as
the initial context factory and the location of the naming service. This constructor
is not recommended for use with Java enterprise applications running in a
WebSphere container.
Local and global JNDI namespaces
The J2EE naming service contains two segments, and it is important to
understand their difference. The global namespace is shared among
components allowing all authorized naming service clients to find the resources.
There is also a local namespace, which holds entries private to the application or
component. The local namespace also allows components to look up local
resources, such as EJB local homes, which are not registered in the global
namespace.
The most common use of the global namespace in Java enterprise applications
is to look up the remote home interfaces of EJBs, which are required to be bound
in the global namespace. Because the global namespace is shared between all
components, it is important to avoid naming conflicts. This is particularly a
concern if you have to deploy the same component more than once on the same
Application Server. Running multiple instances of the same component is
described in “Managing application versions” on page 221. Figure 9-7 shows two
clients performing a lookup on the remote home interface of an EJB.
Figure 9-7 Global JNDI namespace
Global JNDI namespace
PiggyBank
ControllerHome
itso/ad/PiggyBankControllerHome
Client A
lookup("itso/ad/PiggyBankControllerHome")
Client B
lookup("itso/ad/PiggyBankControllerHome")

Get WebSphere Version 5 Application Development Handbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.