5.5. The resource

The resource implementation requires only minimal changes. Remember that each resource will now have a unique key identifying it. We will need to reflect this in our resource by implementing the ResourceIdentifier interface:

public class MathResource implements Resource, ResourceIdentifier,
		ResourceProperties {
		
}

This interface requires us to implement a getID method returning the resource's identifier. Notice that the identifier is of type Object. In other words, our unique identifier can be of any type we wish, although we will generally choose a key of type Integer. Later on we will see that we will need to specify the type of the resource identifier in the JNDI deployment file.

/* Resource key. This uniquely identifies this resource. */
private Object key;

/* Required by interface ResourceIdentifier */
public Object getID() {
	return this.key;
}

The initialization of the resource identifier takes place in the initialize method of the resource class. The simplest key we can create is the resource's hashCode (in Java, every object has an identifier which can be retrieved by calling the hashCode method). Notice how the initialize method now returns the resource identifier.

/* Initializes RPs and returns a unique identifier for this resource */
public Object initialize() throws Exception {
	this.key = new Integer(hashCode());

	// Initialize the resource properties
	
	return key;
}
[Note]

This is part of file $EXAMPLES_DIR/org/globus/examples/services/core/factory/impl/MathResource.java.

[Warning]

An object's hashCode isn't guaranteed to be unique. For the simple examples included in the tutorial, this should not be a problem. However, using other unique identifiers is prefered for more complex services, specially if our service will work with persistent resources. For example, you can use the UUIDGen class included with Apache Axis.