THE SpaceAccessor CLASS

Frans Coenen and Christian SetzKorn

1. INTRODUCTION

There are many ways to gain access to a JavaSpace. Most practitioners (e.g. Freeman et al. 1999, Deitel et al. 2002) suggest that a SpaceAccessor utility class is used. In this page we will describe such a class which we will place in a package called JavaSpacesUtils.

The approach described here also makes use of a property file, the creation of such a file is described in Section 2, and a method to read this file in Section 3. The construction of the SpaceAccessor class is the detailed in Section 4, and its compilation in Section 5.




2. CREATING A PROPERTY FILE

Table 1 show a basic property file. This is a text file comprising a property list describing one or more system properties (one per line). A property list comprises keys and corresponding values. Each key and its corresponding value in the property list is a string:

< STRING_LABEL > = < STRING_DESCRIPTION >

The property file in Table 1 contains properties pertaining to a JavaSpaces application. In this case the name of a machine on which the Space is to be located (linux10 in this example) and the name of the space (frans_space in this example).

jiniURL   = jini://linux10
spaceName = frans_space

Table 1: farns_space.prop property file

Note that we have called the file frans_space.prop.




3. READING THE PROPERTY FILE

/* ---- FIELDS ---- */

static String spaceName = null;
static String jiniURL   = null;
Properties props;

/* ---- METHODS ---- */

/* INITIALISE PROPERTIES. Read property file and assign value to 
spaceName and jiniURL fields */

protected void initProperties(String propFileName) {
    
    // Create instance of Class Properties
    props = new Properties(System.getProperties());

    // Try to load property list
    try {
	props.load( new BufferedInputStream(new 
			FileInputStream(propFileName)));
	}
    catch(IOException ex) {
	ex.printStackTrace();
        System.out.println( "Exception in SpaceAccessor" );
	System.exit(-3);
	}

    // Assign values to fields
    
    jiniURL = props.getProperty("jinURL");
    spaceName = props.getProperty("spaceName");
    }  

Table 2: initProperties method to load property file

Notes:

  1. The getProperties() class method (in the System class) returns an instance of the class Properties describing the current system properties. Properties are described in the form of a property list. The property list comprises keys and corresponding values. Each key and its corresponding value in the property list is a string. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
  2. The load(InputStream inStream) instance method, found in the Properties class, reads a property list (key and element pairs) from the input stream.
  3. The getProperty(String key) instance method searches for the property with the specified key in the "this" property list and returns the corresponding element as a String.



4. SPACE ACCESSOR CLASS

In Table 3 a complete SpaceAccessor class is presented (including the initProperties method from Table 2.

package JavaSpacesUtils;

/* ----------------------------------------- */
/*                                           */
/*               SPACE ACCESSOR              */
/*                                           */
/* ----------------------------------------- */

// Jini core packages

import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.*;
import net.jini.core.entry.Entry;

// Jini extension package

import net.jini.space.JavaSpace;
import net.jini.lookup.entry.*;

// Java core packages

import java.io.*;
import java.rmi.*;
import java.net.*;
import java.util.*;


public class SpaceAccessor {

    /* ------ FIELDS ------ */
 
    static String spaceName = null;
    static String jiniURL   = null;
    Properties props;
  
    static final long MAX_LOOKUP_WAIT = 2000L;
    
    JavaSpace space;
      
    /* ------ CONSTRUCTORS ------ */

    /* GET SPACE */
    
    public SpaceAccessor(String propFileName) {
        LookupLocator locator = null;
	ServiceRegistrar registrar = null;
	
	// Security manager
	
	try {
	    System.setSecurityManager(new RMISecurityManager());
	    }
	catch (Exception e) {
	    e.printStackTrace();
	    }

        // Get properties from property file
	
        initProperties(propFileName);
        
	
	try {
	    // Get lookup service locator at "jini://hostname"
	    // use default port and register of the locator
	    locator = new LookupLocator(jiniURL);
	    registrar = locator.getRegistrar();
	    // Space name provided in property file
	    ServiceTemplate template;
	    if (spaceName != null) {
	        // Specify the service requirement, array (length 1) of 
		// Entry interfaces (such as the Name interface)
		Entry [] attr = new Entry[1];
		attr[0] = new Name(spaceName);
		template = new ServiceTemplate(null,null,attr);
		}
	    else {
	        // Specify the service requirement, array (length 1) of 
		// instances of Class
		Class [] types = new Class[] {JavaSpace.class};
		template = new ServiceTemplate (null, types, null);
		}
	    Object obj = registrar.lookup (template);
	    // Get space, 10 attempts!
	    for (int i=0; i < 10; i++) {
		if (obj instanceof JavaSpace) {
		    space = (JavaSpace) obj;
		    break;
		    }
	        System.out.println("BasicService. JavaSpace not " +
				"available. Trying again...");
		Thread.sleep(MAX_LOOKUP_WAIT);
		}
	    }
	catch(Exception e) {
	    e.printStackTrace();
	    }	
	}
	
    /* INITIALISE PROPERTIES. Read property file and assign value to 
    spaceName and jiniURL fields */

    protected void initProperties(String propFileName) {
    
    	// Create instance of Class Properties
    	props = new Properties(System.getProperties());

    	// Try to load property list
    	try {
	    props.load( new BufferedInputStream(new 
	    			FileInputStream(propFileName)));
	    }
    	catch(IOException ex) {
	    ex.printStackTrace();
            System.out.println( "Exception in SpaceAccessor" );
	    System.exit(-3);
	    }	
    
    	// Output property list (can be ommitted - testing only)
    	
    	System.out.println("jiniURL   = " + 
			props.getProperty("jiniURL"));
	System.out.println("spaceName = " + 
			props.getProperty("spaceName"));

    	// Assign values to fields
    
    	jiniURL = props.getProperty("jiniURL");
    	spaceName = props.getProperty("spaceName");
	}  	
    
    /* GET SPACE */
     
    public JavaSpace getSpace() {
	return space;
	}
    }
    ........................
    
    
    /* GET SPACE */
     
    public JavaSpace getSpace() {
	return space;
	}
    }

Table 3: SpaceASccessor class

Notes:

  1. We create an array of Entry objects. An Entry (in package net.jini.core.entry) describes a service which enables jini clients to search for services of a particular description. The Entry interface is the supertype of all entries that can be stored in a Jini Lookup service.
  2. The Name class represents the name of a service as used by users (found in the net.jini.lookup.entry package.
  3. Instances of the class Class represent classes and interfaces in a running Java application.
  4. instanceof is a infix operator that returns true its prefix operand is an instance of its postfix operand.



5. COMPILING SpaceAccessor.java

In Table 4 we present a shell shell script to compile SpaceAccessor.java. Note that although this is presented on several lines it should be encoded all on one line with a space after the -classpath, and no space after the first sem-colon (~/Java/jini1_2_1/lib/jini-core.jar:)!

javac -d . -classpath 
	/home/staff5/ra/frans/Java/jini1_2_1/lib/jini-core.jar:
	/home/staff5/ra/frans/Java/jini1_2_1/lib/jini-ext.jar: $1

Table 4 script to compile SpaceAccessor.java shell script

The -d flag is used to specify where to place the generated class files according to the nature of the package statment in the source code; the current directory (.) in this case. The following are paths to the jini library packages. $1 is the file to be compiled.

Assuming that SpaceAccessor.java is contained in a directory /users/ra/frans/JavaProgs/JavaSpaces/ the above will rersult file SpaceAccessor.class in a directory JavaSpacesUtils contained in JavaSpaces (the compiler will create the JavaSpacesUtils if it does not already exist).

We can now use the SpaceAccessor.class in the JavaSpacesUtils package in conjunction with JavaSpaces applications.

Note that the SpaceAccessor.class must have access to the frans_space.prop file, so this must also be placed in the JavaSpacesUtils Directory.




REFERENCES

  1. Deitel, H., Deitel, P. and Santry, S. (20020. Advanced Java 2 Platform: How to Programme. Prentice Hall.
  2. Freeman, E., Hupfer, S. and Arnold, K. (1999). JavaSpacesTM Principles, Patterns and Practice. Addison-Wesley.



Created and maintained by Frans Coenen. Last updated 03 September 2003