A naming service provides the facility to find an object based on a name. Whenever we need to associate name to things, we can use a naming service. It is similar to the idea of telephone directory that we use. JNDI and registries are other examples to a naming service.
During development, if is useful to have the powerful containers like Glassfish. It contains the web server, like tomcat, the application server, embedded in it. The user is not aware of the embedded features in a big container like JBOSS or glassfish.
But, during actual deployment, you want to have, DMZ – Demilitarized Zone. We need to separate everything, like the web tier and persistence tier, and logic tier and put firewall in between so that web tier and persistence layers are separated. It gives more security and granularity. When you learn the technology, you use just one machine! But in real world scenarios, we need to split up the whole application to different modules that essentially runs on different machine.
JNDI becomes more relevant in such scenarios. It acts as the binding between different tiers by providing API’s that can connect and look up for things or data in each other.
Naming services are made with a hierarchical organisation similar to the structure of a tree. The leaves are the actual data in the structure. JNDI is also having a tree structure.
JNDI consists of a set of set of APIs that allows us to interact with directory services like LDAP, NIS etc. Some of the advantages of using JNDI are as follows:
- You can use multiple frameworks. All we need to change is the binding.
- You just have to learn one API, and hides you from protocol and implementation details.
- You can save java objects using JNDI
- You can also link different directory services using JNDI.
Directory services like JNDI and relational databases are used for storing data. But there are significant differences between the two.
- Directory services are hierarchical in structure and not relational.
- Before relational db, we had hierarchical db, which had an internal model similar to that of directory service.
- Most of the operation in directory services is read and we rarely perform write.
- As a result, dir services are more optimised for read and writing works slower in a directory service.
Now we take a look at the API part of JNDI. It has a set of APIs.
The term binding in JNDI is the association of a name with an object. To use JNDI, we need to acquire a context. A context is a set of bindings. In the hierarchical structure that I mentioned above, each leaf is a collection of pairs – <name, object>. A context can contain many leaves of the type above. JNDI names look like urls. There is a special prefix as java:. Java is a special token that represents a scheme and in this scheme there are some predefined locations. In order to acquire a context, you need to provide IP address of server, port number, the starting location within JNDI trees. It might be protected by password as well.
Some of the operations in JNDI are as follows:
- list() – list the directory structure as in a file system
- createSubcontext() – creates a sub context, similar to creating a folder in a directory.
- bind() – creates an association, write a pair,name value in the current context
- rebind() — forces binding something that is already in the tree.