Java Naming and Directory Interface: Lesson 2

1. SPECIFYING JNDI PROPERTIES

The values of the basic JNDI properties (java.naming.factory.initiai and java.naming. provider.uri) may be supplied in different ways. In the previous example, we supplied them as command line arguments. The Initiaicontext class also has an overloaded constructor that takes a java.utii.Hashtabie object. So, we can place JNDI properties in a Hashtabie object and pass it to the InitiaiContext’s constructor as follows:

java.utii.Hashtabie env = new java.util.Hashtable();

env.put(”java.naming.factory.initial”,”com.sun.jndi.rmi.registry.RegistryContex

tFactory”);

env.put(”java.naming.provider.url”, ”rmi://172.16.5.81:6789”);

Context ctx = new InitialContext(env);

Another way to specify JNDI parameters is to use an application resource file having name jndi. properties. It contains a list of key/value pairs in the properties file format. The following is an example of such a property file.

#jndi.properties

java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory

java.naming.provider.url=rmi://172.16.5.81:6789

The JNDI automatically reads the application resource files from all components in the applications’ classpaths and JRE_HOME/iib/jndi.properties, where jre_home is the file directory that contains Java Runtime Environment (JRE). Note that properties files are text files and should not contain sensitive information, such as passwords.

Table 24.1: shows a list of initial context factory and provider URL for various naming service providers.

2. NAME SERVERS

The previous example used rmiregistry name server. In practice, there exists various other name servers such as file system, Java IDL’s tnameserv, and orbd. These servers do not support directory service. To illustrate the concepts of directory as well as naming operations, we shall use a simple directory server provided by Apache—known as Apache Directory Server (or simply ApacheDS). The next section describes how to install and use ApacheDS.

3. USING APACHEDS

Apache Directory Server (often abbreviated as ApacheDS) is LDAPv3 compatible, open source directory server entirely written in Java. It is a multi-platform application and runs on Mac OS X, Linux and Windows. In this chapter, in addition to file system naming service, Java’s rmiregistry and orbd, we shall use ApacheDS to demonstrate the JNDI concepts.

3.1. Installing and Starting ApacheDS

To work with ApacheDS, download it from https://directory.apache.org/apacheds/ downioads.html. Apache provides native installers for several platforms. It also provides a zip and tar.gz archive suitable for any platform. We downloaded apacheds-2.0.0-Mi5.zip. Extract this archive and place the extracted folder (apacheds-2.0.0-M15) where you want Apache DS to be installed. We placed the folder in D: drive. Hereafter, we shall refer to this D:\apacheds-2.0.0-M15 directory as <ApacheDS_TOP>).

ApacheDS requires a Java Runtime Environment 6 or later. So, make sure an appropriate version is installed in your machine. Put the directory containing java interpreter in your PATH environment variable. Go to <ApacheDS_TOP>\bin directory, where you will find two files apacheds.bat and apacheds.sh, which are startup script for windows and linux respectively. To start ApacheDS in windows, either double click on apacheds.bat or open a terminal, go to the <ApacheDS_TOP>\bin directory and use the following command:

apacheds.bat

If everything goes fine, the startup window looks like this:

Starting ApacheDS instance ‘default’…

[16:02:47] WARN [org.apache.directory.server.core.DefaultDirectoryService] –

You didn’t change the admin password of directory service instance ‘default’.

Please update the admin password as soon as possible to prevent a possible

security breach.

By default the ApacheDS listens on port 10389 (unencrypted or StartTLS) and 10636 (SSL). However, the well-known port for LDAP is 389, which you may choose your ApacheDS to listen on. The server configuration (including port number) is stored in a LDIF file <ApacheDS_TOP>\ instances\default\conf\config.ldif. The relevant portion for unencrypted LDAL is shown below:

dn: ads-transportid=ldap,ou=transports,ads-serverId=ldapServer,ou=servers,ads-

directoryServiceId=default,ou=config

ads-systemport: 10389

And, here is the port specification for SSL-enabled LDAP:

dn: ads-transportid=ldaps,ou=transports,ads-serverId=ldapServer,ou=servers,ads-

directoryServiceId=default,ou=config

ads-systemport: 10636

Change the port of your choice. Note that a root privilege is required to use a port less than 1024. We stuck on the default ports.

3.2. JNDI Properties for ApacheDS

ApacheDS stores entries in distinct areas called partitions. Partitions are disconnected from each other, meaning that changes to entries in partition P would never affect entries in partition Q. The entries in a particular partition are stored in naming contexts called the partition suffix. The ApacheDS default instance contains a partition with root suffix dc=exampie,dc=com. Although, a new partition may be created, we shall use this existing partition to demonstrate our JNDI examples.

We shall assume that ApacheDS runs on 10389 in a host 172.16.5.81. The values of these two properties java.naming.factory.initial and java.naming.provider.url will then be com.sun. jndi.ldap.LdapCtxFactory and ldap://172.16.5.81:103 8 9/dc=example,dc=com respectively and will be used in all subsequent examples that use ApacheDS as the name server. If you start ApacheDS on a different port, or in a different host, specify them in the value of java.naming. provider.url property.

4. CALCULATOR RMI APPLICATION USING LDAP

Let us now run our calculator application that will make use of ApacheDS as a naming server instead of rmiregistry. We assume that the ApacheDS is running on 10389 in host 172.16.5.81. To start the server, go the server’s home directory and use the following command:

java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory –

Djava.naming.provider.url=ldap://172.16.5.81:10389/dc=example,dc=com

CalculatorServerJNDI cn=calculator

Since an LDAP server expects names as pair of values separated by =, we have used name cn=calculator. Note that in this case, server and ApacheDS need not necessarily run in the same computer. To start the client, go to the client’s home directory and use the following command:

java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory –

Djava.naming.provider.url=ldap://172.16.5.81:10389/dc=example,dc=com

CalculatorClientJNDI cn=calculator

5. CALCULATOR RMI-IIOP APPLICATION USING JNDI

Let us now redevelop our calculator application using RMI-IIOP and JNDI. We use the same Calculator interface as used in RMI. The implementation of this interface that uses IIOP is shown below:

// SimpleCalculator.java

import javax.rmi.PortableRemoteObject;

class SimpleCalculator extends PortableRemoteObject implements Calculator {

public SimpleCalculator() throws java.rmi.RemoteException

{                                                              

}

public int add(int a, int b) throws java.rmi.RemoteException {

return a + b;

}

public String[] _ids() {

String[] id = {“IDL:CalculatorApp/Calculator:1.0”};

return id;

}

}

5.1. Server

The code for the server is shown below:

// CalculatorServerJNDI.java

import javax.naming.*;

public class CalculatorServerJNDI {

public static void main(String args[]) {

try {

SimpleCalculator calc = new SimpleCalculator();

Context ic = new InitialContext();

ic.rebind(args[0], calc );

Gystem.out.println(”Calculator Server ready…”);

} catch (Exception e) { e.printGtackTrace();}

}

}

5.2. Client

The code for the server is shown below:

//CalculatorClientJNDI.java

import javax.naming.*;

public class CalculatorClientJNDI {

public static void main( String args[] ){

try {

Context ic = new InitialContext();

Calculator calc = (Calculator)ic.lookup(args[0]);

int x =2, y = 3;

System.out.println(”Sent :” + x + ” and ” + y); int result = calc.add(x, y);

System.out.println(”Received : ” + result);

} catch( Exception e ) {e.printStackTrace();}

}

}

5.3. Running the Application

Open a terminal in any machine in the network.

start orbd

The Java ID naming service now listens on port 900. We assume that the IP address of the machine this service is started on is 172.16.5.81. Compile the server files using the following series of commands:

javac SimpleCalculator.java

rmic -iiop SimpleCalculator

javac Calculator.java CalculatorServerJNDI.java

Now, start the server:

java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory –

Djava.naming.provider.url=iiop://172.16.5.81:900 CalculatorServerJNDI

calculator

This uploads a reference to the naming service with name “calculator”. Compile the client files using the following series of commands:

javac Calculator.java rmic -iiop Calculator javac CalculatorClientJNDI.java

Start the client:

java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory –

Djava.naming.provider.url=iiop://172.16.5.81:900 CalculatorClientJNDI

calculator

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *