Java Reference
In-Depth Information
Table 5.3. Common JNDI properties for connecting to a remote JNDI Java EE environment
Property name
Description
Example value
java.naming.factory .ini-
tial
The name of the factory class that will be used to
create the context
oracle.j2ee.rmi.RMIInitialContextFactory
java.naming.provider.url The URL for the JNDI service provider
ormi://localhost:23791/chapter1
java.naming.security
.principal
The username or identity for authenticating the
caller in the JNDI service provider
oc4jadmin
java.naming.security
.credentials
The password for the username/principal being
used for authentication.
welcome1
Lookup JNDI resources
After you've connected to your JNDI provider, the Context interface provides the func-
tionality to interact with the provider and get resources. For this primer, we'll concentrate
on the lookup method and leave the rest for you to explore on your own. Table 5.4 de-
scribes this method.
Table 5.4. Context lookup method
Method
Description
Returns the named resource, which must be typecast to the type you need. A
new Context instance is returned if the resource name is empty.
Object lookup(String name)
So to look up a resource, you need to know the resource's name. Suppose the Bid-Ser-
vice EJB is bound in JNDI at "/ejb/bid/BidService" . To look up the BidSer-
vice , you can look it up directly:
Context context = new InitialContext();
BidService service = (BidService) context.lookup("/ejb/bid/BidService");
Or you can chain lookups together:
Context newContext = new InitialContext();
Context bidContext = (Context) newContext.lookup("/ejb/bid/");
BidService service = (BidService) bidContext.lookup("BidService");
Although these lookup code examples look pretty harmless, don't be taken by appearances.
JNDI lookups were one of the primary reasons for EJB 2.x complexity. First of all, you had
to do lookups to access any resource managed by the container, even if you were only ac-
cessing data sources and EJBs from other EJBs located in the same JVM. Given that most
 
 
Search WWH ::




Custom Search