Creating Adapters
Why create a new adapter?
Adapters are typically created in order to support a new tuple space implementation through the same interface used by LighTS, so that the code written once for LighTS can actually work with several other implementations.
How to create an adapter?
Creating
an adapter actually means to create four new classes, namely, a TupleSpaceFactory
class, which must extend lights.adapters.TupleSpaceFactory,
and other three classes (typically called FieldAdapter,
TupleAdapter,
and TupleSpaceAdapter)
that implement the IField,
ITuple,
ITupleSpace interfaces.
Typically, all these classes are placed in a new package, e.g., lights.adapters.myadapter. Thus,
for instance, lights.adapters.tspaces
is the package containing adapters for the last version of TSpaces.
TupleSpaceFactory is the class that determines the
Each of these classes implements the methods of the corresponding interface by calling one or more methods of the implementation they adapt. For instance, the implementation of the method out in lights.adapters.tspaces.TupleSpaceAdapter is the following:
public void out(ITuple tuple) throws TupleSpaceException {
try {
impl.write(TupleAdapter.unwrap(tuple));
} catch(com.ibm.tspaces.TupleSpaceException e) {
throw new lights.interfaces.TupleSpaceException(e);
}
}
A few things are worth noting in the above code fragment:
The only constraint on the adapter classes (e.g., TupleSpaceAdapter) is that they implement the corresponding interface (e.g., ITupleSpace). Thus, in principle a single class could be use for adapting fields, tuples, and tuple space or, more interestingly, an application class could be extended with the adapter methods.
The exceptions provided in LighTS allow the wrapping of exceptions generated by the underlying implementation. Thus, in the example the exception generated by TSpaces is actually reported by creating a LighTS exception by passing the TSpaces exception as a parameter. This is the correct way to report exceptions from the underlying implementation.