tales from the darkstar
I can write that service in two lines...
Folks on the forums have requested facilities to support iterating over objects, primarily for debugging, and for obtaining objects by object ID. This capability is not part of the standard API available to a Darkstar application, however it is possible to write a manager to provide this functionally. It turns out that such a manager is quite simple, and can provide a straightforward example of how to create a manager. This note describes how to write, install, and access a new manager and its backing service. For a more rich (read complex) example, check out Seth's blog on writing services.
In Darkstar, managers provide many of the application level APIs. By creating a new manager one can extend the API set with functions that application could not otherwise implement. Underneath the managers, not directly visible to the application, are corresponding services. Services provide the functions needed by their managers and have access to the other services in Darkstar that applications do not have access to.
The example manager described below will be built on a service which will use the DataService to provide the desired functionality.
A Darkstar service must implement the com.sun.sgs.service.Service interface and have a specific constructor. Ignoring Javadoc, import and package statements, the class declaration and constructor look like this:
public class ObjectAccessService implements Service {
private final DataService dataService;
public ObjectAccessService(Properties properties,
ComponentRegistry systemRegistry,
TransactionProxy proxy) {
dataService = proxy.getService(DataService.class);
public ManagedReference<?> createReferenceForId(BigInteger id) {
return dataService.createReferenceForId(id);
}
public BigInteger nextObjectId(BigInteger objectId) {
return dataService.nextObjectId(objectId);
}
return ObjectAccessService.class.getName();
}
public void ready() throws Exception {
// ignore
}
return true;
}
public class ObjectAccessManager {
private final ObjectAccessService service;
public ObjectAccessManager(ObjectAccessService service) {
this.service = service;
}
public ManagedReference<?> createReferenceForId(BigInteger id) {
return service.createReferenceForId(id);
}
public BigInteger nextObjectId(BigInteger objectId) {
return service.nextObjectId(objectId);
}
In order to make the new manager available to the application, both it and its service needs to be installed in the Darkstar server. This is done by adding the following two items to the server's configuration file:
com.sun.sgs.services=mypackage.ObjectAccessService
com.sun.sgs.managers=mypackage.ObjectAccessManager
Obviously the manager and server classes need to be in the class-path of the server.
With the manager installed it is a simple matter for the application to access it:
ObjectAccessManager accessManger = AppContext.getManager(ObjectAccessManager.class);
Though this is an extremely simple example, it does provide a means to iterate over the objects in the system, by their identifier, and given an identifier, get the object. It also demonstrates the basic manager-service operation in Darkstar.
Posted at 06:07PM Oct 20, 2008 by Keith Thompson in Project Darkstar | Comments[0]
Today's Page Hits: 13