7. JNDI Configuration

As of version 0.0.5 SwitchRMI includes a minimal JNDI Context implementation. Published objects are stored as JNDI References in a JNDI Context. A JNDI lookup at runtime will return the object used to satisfy the request.

SwitchRMI client applications can also be configured via JNDI so that a client can lookup a JNDI name to obtain a proxy to a remote object.

Service Configuration

The JNDI Context is built from the XML deployment descriptor file passed as the value of the system property: java.naming.provider.url . The system property java.naming.factory.intial must be set to the SwitchRMI JNDI Context factory class: com.mjh.switchrmi.jndi.JndiContextImplFactory .

The SwitchRMI servlet class in the SwitchRMI distribution needs only the provider URL passed as the value of the switchrmi.configuration init-parameter in the web.xml file.

Alternatively, the JNDI context can be configured via a Hashtable containing these values:

	import javax.naming.*;
	
		Hashtable env = new Hashtable();
		env.put(Context.PROVIDER_URL, "http://localhost:8080/test/jndi/TestSuiteConfiguration.xml");
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.mjh.switchrmi.jndi.JndiContextImplFactory");
		jndiCtx = new InitialContext(env);
	
If using system properties to pass these values, the no-argument constructor can be used:
		jndiCtx = new InitialContext();
	
As the InitialContext is constructed the XML deployment descriptor is read from the provider URL and used to construct a JNDI namespace. Appendix A contains a listing of the XML Document Type Definition for the deployment description file. Once this namespace is constructed, the server side, which is hosting the published objects can lookup an object thus:
		echo = (IEcho)jndiCtx.lookup("/switchrmi/service/object/echo");
	

Client Configuration

A client can be configured to declare the remote objects in a XML description file such that application code is not aware of the actual network location of the remote object. The client configuration file declares a reference for each remote object:

	<?xml version="1.0"?>

	<!DOCTYPE switchrmi PUBLIC "-//SwitchRMI//DTD Serrvice Configuration 0.0.5//EN"
			"http://localhost:8080/test/jndi/SwitchRMIConfiguration.dtd">
	<switchrmi>
	<client>

		<proxy name="echo">
			<remote url="http://localhost:8080/rmi/echo.xmlrpc" />
			<interface class="com.mjh.switchrmi.transport.test.IEcho" />
			<description>
				The echo object is included to allow testing of the SwitchRMI
				installation.
			</description>
		</proxy>
	</client>
	</switchrmi>
	
The JNDI Context is constructed, as for the server side, by passing the URL of the configuration file to the InitialContext constructor. Once a context is obtained the client may perform a JNDI lookup to obtain a proxy for the remote object without embedding the remote object URL in the source code:
		echo = (IEcho)jndiCtx.lookup("/switchrmi/client/proxy/echo");
	

The RmiClient class provides an alternative to JNDI client configuration. SwitchRMI client code uses a minimal JNDI Context internally but RMIClient constructs the proxy to a remote object directly without using JNDI Reference objects. The following listing obtains a proxy to the same remote object as the preceding JNDI client example

		RmiClient rmi = new RmiClient();
		echo = (IEcho)rmi.connect("http://localhost:8080/rmi/echo.xmlrpc", IEcho.class);
	

The JUnit test suite shipped with SwitchRMI uses both JNDI and RmiClient client configuration methods.