The addressbook service

The addressbook program consists of two parts:

  1. A client, AddressClient, that connects to the addressbook server, add two addresses and then queries both addresses from the server using remote refelection and displays the results. .
  2. The server, AddressServer, which does use the DefaultRemotingHandler and simply delegates the client requests to a service class: AddressBook.
To implement the addressbook service with EJOE we will follow these steps:
  1. Create a addressbook service class as well as the required bean representing an address
  2. Create a permission configuration for remote reflection
  3. Create a server application and start EJServer with the DefaultRemotingHandler
  4. Implement a EJOE based address client

The service class

public class AddressBook
{
    private static Hashtable name2AddressTable = new Hashtable();

    public AddressBook()
    {
    }

    public void addEntry( String name, Address address )
    {
        name2AddressTable.put( name, address );
    }

    public void addEntry( String firstName, String lastName, Address address )
    {
        name2AddressTable.put( firstName + " " + lastName, address );
    }

    public Address getAddressFromName( String name ) throws IllegalArgumentException
    {
        return (Address) name2AddressTable.get( name );
    }

}

The applicable address bean:
public class Address implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;
    private int               streetNum;
    private java.lang.String  streetName;
    private java.lang.String  city;
    private java.lang.String  state;
    private int               zip;

    public Address()
    {
    }

    public int getStreetNum()
    {
        return streetNum;
    }

    public void setStreetNum( int streetNum )
    {
        this.streetNum = streetNum;
    }

    public java.lang.String getStreetName()
    {
        return streetName;
    }

    public void setStreetName( java.lang.String streetName )
    {
        this.streetName = streetName;
    }

    public java.lang.String getCity()
    {
        return city;
    }

    public void setCity( java.lang.String city )
    {
        this.city = city;
    }

    public java.lang.String getState()
    {
        return state;
    }

    public void setState( java.lang.String state )
    {
        this.state = state;
    }

    public int getZip()
    {
        return zip;
    }

    public void setZip( int zip )
    {
        this.zip = zip;
    }
}

The server application

import java.io.IOException;
import de.netseeker.ejoe.EJServer;
import de.netseeker.ejoe.handler.DefaultRemotingHandler;

public class AddressServer
{
    public static void main( String[] args )
    {
        EJServer server = new EJServer( new DefaultRemotingHandler() );
        server.setMaxReadProcessors( 1 );
        server.setMaxWriteProcessors( 1 );
        server.enableNonBlockingIO( true );
        server.enablePersistentConnections( true );
        try
        {
            server.start();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            System.exit( -1 );
        }
    }
}

The security settings for the server application

A directory containing a file named "ejoe-reflection-conf.properties" must be available on the classpath.
#####################################################################################
# This is a customized configuration for remote reflection in EJOE.
# EJOE handles received reflection requests by clients VERY restrictive:
# Only packages/classes which are listed in this file
# can be used for remote reflection.
# To overwrite the reflection settings, provide a customized
# ejoe-reflection-conf.properties on the classpath.
#####################################################################################

de.netseeker.ejoe.examples.remoting.*

The client application

import java.io.IOException;
import com.thoughtworks.xstream.XStream;
import de.netseeker.ejoe.EJClient;
import de.netseeker.ejoe.EJConstants;
import de.netseeker.ejoe.handler.RemotingRequest;

public class AddressClient
{
    public static void main( String[] args )
    {
        EJClient client = new EJClient( "localhost", EJConstants.EJOE_PORT );
        client.enablePersistentConnection( true );

        try
        {
            // create an address object for Jimmy Who
            Address address = new Address();
            address.setStreetNum( 20 );
            address.setStreetName( "Peachtree Avenue" );
            address.setCity( "Atlanta" );
            address.setState( "GA" );
            address.setZip( 39892 );            

            //create a RemotingRequest to invoke the remote AddressBook
            RemotingRequest request = new RemotingRequest( AddressBook.class.getName(), "addEntry", new Object[] {
                    "Jimmy Who", address } );
            //add Jimmys address
            System.out.println("adding Jimmys address...");
            client.execute( request );

            // create an address object for Jane Who
            address.setStreetNum( 21 );
            address.setStreetName( "Peachtree Avenue" );
            address.setCity( "Atlanta" );
            address.setState( "GA" );
            address.setZip( 39892 );

            //add Janes address
            request.setArgs( new Object[] { "Jane", "Who", address } );
            System.out.println("adding Janes address...");
            client.execute( request );

            // now query both addresses
            request.setMethod( "getAddressFromName" );
            
            request.setArgs( new Object[] { "Jimmy Who" } );
            System.out.println("querying Jimmys address...");
            Address adrJimmy = (Address) client.execute( request );            
            System.out.println("Jimmys address: ");
            XStream xstream = new XStream();
            System.out.println( xstream.toXML( adrJimmy ));
            
            System.out.println("");
            
            request.setArgs( new Object[] { "Jane Who" } );
            System.out.println("querying Janes address...");
            Address adrJane = (Address) client.execute( request );
            System.out.println("Janes address: ");
            System.out.println( xstream.toXML( adrJane ));
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }
}