|
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.
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.
/* ---- 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:
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:
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.
Created and maintained by Frans Coenen. Last updated 03 September 2003