apache > ws.apache
WSRF
 

Writing a Resource Class

Introduction

The resource class is the stateful instance-representation of your Web service. The resource maintains the resource id and the ResourcePropertySet. The resource id is the unique identifier for an instance of your Web service. It allows you to have multiple resource instances, each with their own state, fronted by the same Web service. The stateful properties are represented by the ResourcePropertySet. The ResourcePropertySet is the Java representation of the Resource Properties document defined in the schema section of your WSDL file.

When a request is made for a Web service, it is expected to contain a WS-Addressing header which contains the resource id. When Apache WSRF receives the request it will use the resource id to look up the corresponding resource instance to service the request.

If you use the Wsdl2Java tool, the resource class is automatically generated, but code will need to be added to initialize the resource's ResourcePropertySet. This section discuss how to write a resource class. Initially, you should model your resource off of the included FileSystemResource example to ensure that you write a valid resource class.

Class Declaration

When declaring your Resource class you MUST implement org.apache.ws.resource.Resource which provides the necessary operations for dealing with the ResourcePropertySet and the resource's id.

Note
The resource id is a custom WS-Addressing header element which you will define in your configuration information for your service.

Optionally, you may also implement PersistentResource, for providing hooks for persistence, and ScheduledResourceTerminationResource, for adding the ability to schedule when the resource should be terminated.

The FileSystemResource class declaration is as follows:

public class FileSystemResource extends
      AbstractFileSystemResource

Notice that we've extended a base abstract class. This allows us to focus solely on the init() operation for registering callback objects and initializing the values of our ResourceProperties.

The AbstractFileSystemResource class implements Resource, PropertiesResource, and ScheduledResourceTerminationResource. We've "abstracted" the non-user specific code to the abstract class so that the user can focus on their initialization.

Instance Variables

The instance variables of AbstractFileSystemResource should include the resource id and the ResourcePropertySet. The ResourcePropertySet is the object representation of the resource properties document associated with this resource instance.

Note
Since it is not a requirement to have resource properties in your service, there is a PropertiesResource interface to denote that properties are being exposed.

Methods

The methods are defined by the interfaces you implement and are mainly setters and getters for the ResourcePropertySet and the resource id. There are also init()and destroy() operations which allow you to initialize or clean up your resource.

Read-Only ResourceProperties

Individual ResourceProperties can be defined read-only by modifying the ResourceMetadata object:

resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.DEVICESPECIALFILE );
...
resourceProperty.getMetaData().setReadOnly( true );
			

The default readOnly value for custom ResourceProperties is false. Setting readOnly to true will ensure a ResourceProperty will not be changed via a SetResourceProperty request.

Note
Attempting to modify a read-only property will generated a fault.