I'm following the tutorial: https://docs.oracle.com/javase/tutorial/jndi/index.html
My adventure started while setting a JNDI name for a datasource with the WildFly application server. The name started with "java:/". I was curious on what it was and how it worked.
I have Apache Directory LDAP server setup locally and I'm able to connect to it with:
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=JNDITutorial");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
try {
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("cn=Rosanna Lee,ou=People");
} catch (NamingException e) {
e.printStackTrace();
}
My confusion is the JNDI name "java:/".
Can someone please explain what "java:/" is and how I can use JNDI to interact with it?
My assumption is its a directory located somewhere on my computer.
Thank you.
The explanation is in the name: JNDI is the "Java Naming and Directory Interface". It is part of the Java EE specification and provides an API for java clients to discover and look up data and objects by name. These objects are accessible via certain contexts, e.g.
The names of system-provided objects, such as JTA UserTransaction objects, are stored in the environment naming context java:comp/env. The Java EE platform allows a component to name user-defined objects, such as enterprise beans, environment entries, JDBC DataSource objects, and message connections. An object should be named within a subcontext of the naming environment according to the type of the object. For example, enterprise beans are named within the subcontext java:comp/env/ejb, and JDBC DataSource references are named within the subcontext java:comp/env/jdbc.
ref: http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
As Pawel noted in his comment, the Wildfly docs are very helpful here:
The Java EE platform specification defines the following JNDI contexts:
- java:comp - The namespace is scoped to the current component (i.e. EJB)
- java:module - Scoped to the current module
- java:app - Scoped to the current application
- java:global - Scoped to the application server
In addition to the standard namespaces, WildFly also provides the following two global namespaces:
So "java:/" is just a global namespace (and context) in Wildfly and should be confused with a folder. It is simply a "named address" in a directory to access objects and services like JDBC, EJB, LDAP, etc.
For further information, the Java EE spec is useful:
- (Java 6) http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
- (Java 7, PDF) http://download.oracle.com/otn-pub/jcp/java_ee-7-fr-spec/JavaEE_Platform_Spec.pdf