Java Naming and Directory Interface
Java Naming and Directory Interface
Naming service
A naming service is an entity that
•associates names with objects.We call this binding
names to objects. This is similar to a telephone company ’s
associating a person ’s name with a specific residence ’s
telephone number
1
1
Directory service
Directory service
2
2
Examples of Directory services
JNDI concepts
3
3
JNDI advantages
-You only need to learn a single API to access all sorts of
directory service information, such as security credentials,
phone numbers, electronic and postal mail addresses,
application preferences, network addresses, machine
configurations, and more.
-You can use JNDI to read and write whole Java objects
from directories.
JNDI advantages
Applications can store factory objects and configuration
variables in a global naming tree using the JNDI API.
4
4
JNDI Architecture
JNDI concepts
5
5
JNDI names
JNDI names look like URLs.
A typical name for a database pool is java:comp/env/jdbc/test. The
java: scheme is a memory-based tree. comp/env is the standard
location for Java configuration objects and jdbc is the standard location
for database pools.
Examples
java:comp/env Configuration environment
java:comp/env/jdbc JDBC DataSource pools
java:comp/env/ejb EJB remote home interfaces
java:comp/env/cmp EJB local home interfaces (non-standard)
java:comp/env/jms JMS connection factories
java:comp/env/mail JavaMail connection factories
java:comp/env/url URL connection factories
java:comp/UserTransaction UserTransaction interface
JNDI names
There are three commonly used levels of naming scope in JBoss:
names under java:comp,
names under java:,
any other name.
Subcontexts and object bindings directly under java: are only visible
within the JBoss server virtual machine and not to remote clients.
6
6
Contexts and Subcontexts
7
7
Acquiring an initial context
package examples;
java
-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
-Djava.naming.provider.url=jnp://193.205.194.162:1099
-Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
examples.InitCtx
8
8
Acquiring an initial context
java.naming.factory.url.pkgs:
The name of the environment property for specifying the list of package
prefixes to use when loading in URL context factories. The value of the
property should be a colon-separated list of package prefixes for the
class name of the factory class that will create a URL context factory. For
the JBoss JNDI provider this must be
org.jboss.naming:org.jnp.interfaces.
This property is essential for locating the jnp: and java: URL context
factories of the JBoss JNDI provider.
try {
// Create the initial context
Another example Context ctx = new InitialContext(env);
// Look up an object
Object obj = ctx.lookup(name);
// Print it out
System.out.println(name +
" is bound to: " + obj);
// Close the context when we're done
ctx.close();
import javax.naming.Context; } catch (NamingException e) {
import javax.naming.InitialContext; System.err.println("Problem looking up "
import javax.naming.NamingException; + name + ": " + e);
import java.util.Hashtable; }
class Lookup { }
public static void main(String[] args) { }
// Check that user has supplied name of file to lookup
if (args.length != 1) {
System.err.println("usage: java Lookup <filename>");
System.exit(-1);
}
String name = args[0];
// Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
9
9
try {
LDAP // Create the initial directory context
DirContext ctx = new InitialDirContext(env);
example
// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("cn=Ronchetti
Marco");
10
10
Operations on a JNDI context
JNDI in JBoss
11
11
JNDI in JBoss
JNDI in JBoss
12
12
JNDI e EJB: definizione di proprietà in configuration
An example ejb-jar.xml env-entry fragment
<!-- ... -->
<session>
<ejb-name>ASessionBean</ejb-name>
<!-- ... -->
<env-entry>
<description>The maximum number of tax exemptions allowed </description>
<env-entry-name>maxExemptions</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>15</env-entry-value>
</env-entry>
<env-entry>
<description>The tax rate </description>
<env-entry-name>taxRate</env-entry-name>
<env-entry-type>java.lang.Float</env-entry-type>
<env-entry-value>0.23</env-entry-value>
</env-entry>
</session>
<!-- ... -->
13
13
JNDI e EJB: definizione di proprietà in configuration
An example ejb-jar.xml ejb-ref descriptor fragment
<session>
<ejb-ref>
<ejb-name>ShoppingCartUser</ejb-name>
<!--...-->
<ejb-ref-name>ejb/ShoppingCartHome</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>org.jboss.store.ejb.ShoppingCartHome</home>
<remote> org.jboss.store.ejb.ShoppingCart</remote>
<ejb-link>ShoppingCartBean</ejb-link>
</ejb-ref>
</session>
14
14