5.6. The resource home

The resource home has to be modified, but still remains relatively simple because, as in the previous chapter, our resource class extends from a Globus-supplied class. In the previous chapter, we extended from SingletonResourceHome, a Globus-supplied class that provided most of the functionality of a resource home for a single resource. Now we will extend from ResourceHomeImpl, another Globus-supplied class for resource homes that manage several resources.

The only method we have to implement is the create method, where we will create a new resource and return its identifier. All the other methods we would expect in a resource home (such as a find method to retrieve a resource given a certain key) are already implemented for us in ResourceHomeImpl.

package org.globus.examples.services.core.factory.impl;

import org.globus.wsrf.ResourceKey;
import org.globus.wsrf.impl.ResourceHomeImpl;
import org.globus.wsrf.impl.SimpleResourceKey;

public class MathResourceHome extends ResourceHomeImpl {

	public ResourceKey create() throws Exception {
		// Create a resource and initialize it
		MathResource mathResource = (MathResource) createNewInstance(); 1
		mathResource.initialize(); 2
		// Get key
		ResourceKey key = new SimpleResourceKey(keyTypeName, mathResource
				.getID()); 3
		// Add the resource to the list of resources in this home
		add(key, mathResource); 4
		return key; 5
	}

}

[Note]

This file is $EXAMPLES_DIR/org/globus/examples/services/core/factory/impl/MathResourceHome.java.

1

We create a new instance of the resource. Notice that this must be done using the protected createNewInstance method, not by using the new operator. Also, since createNewInstance returns a Resource object, we must cast it to our specific resource type: MathResourceType

2

We initialize the resource.

3

We obtain the resource identifier using the getID method implemented earlier, and use it to create a SimpleResourceKey object. When creating the SimpleResourceKey, keyTypeName is a protected attribute of ResourceHomeImpl containing the key's type.

4

We add the recently created resource and its key to the resource home's internal list of resources. add is a protected method of ResourceHomeImpl.

5

Finally, we return the resource's key.

[Note]

Now is another good moment to review Figure 5.3, “Sequence diagram for resource creation”.

[Tip]There's more to the resource home that meets the eye...

The resource home shown above, along with the one seen in the previous chapter, covers the simplest possible case of resources: in-memory resources, or resources which reside in main memory while the container is running. However, resource homes can also be used to manage persistent resources, or resources that are stored in disk so they can survive container restarts. To do this, our resource must implement the PersistenceCallback interface. More details are available in the official Globus documentation.

Another thing we can do to a resource home is to override its add and remove methods, to control exactly what happens when a resource is added or removed from the resource home. For example, we might want to write to a log, or register our resource with an index service.