4.4. Resource Creation, Modification, and Destruction

The addition of the resource properties declaration has resulted in several changes to the generated code. We'll now examine the files where these changes have occured.

The service class MathServiceWSRF has two special methods.

Just imagine that we actually have our our service up and ready to accept invocations from a client class, and that the service class has access to the resource home and that the resource home, in turn, has access to a bunch of resource objects. Let's start with the creation of a new resource, shown in Figure 4-3.

Figure 4-3. Sequence diagram for resource creation

  1. Our client only needs to know the URI of the factory service (MathFactoryService). With it, it can invoke the createResource operation. This will return an endpoint reference, that contains the URI and key of the recently created resource.

  2. The ResourceHome is used to create and store ResourceContexts.

  3. The factory method createResource has to create a new resource. This necessarily has to be done through the resource home, which is in charge of managing all the resources. However, we have to locate our resource home first. Fortunately, we don't have to deal directly with the resource home, we call NewResourceContextMathPort in the factory method to get a new resource.

  4. Finally, the EPR is returned to the client in the response instance.

  5. Skip...

Once the createResource call has finished, the client will have the WS-Resource's endpoint reference. In all future calls, this endpoint reference will be passed along transparently in all our invocations. In other words, when we call add or subtract, the service class will know what resource we're referring to. So, let's take a close look at what happens when we invoke the add operation, as shown in Figure 4-4.

Figure 4-4. Sequence diagram for WS-Resource invocation

  1. The client invokes the add operation for the (MathService).

  2. The add operation is stateless. First the resource context must be retrieved. The resource identifier is in the endpoint reference that is included in the address parameter. Fortunately, the generated GetResourceContext(ps, address) method will read the EPR and return resource context it refers to.

  3. Once we have the resource context, the service can directly access state information, such as the "Value" and the "LastOp", in the resource properties "ctx.properties".

  4. Accessing "ctx.properties.Value" retrieves the state.

  5. Modifying "ctx.properties.Value" saves the state.

  6. Finally, set "ctx.properties.LastOp" to "ADDITION".