The Artima Developer Community
Sponsored Link

Observing JavaSpace-Based Systems, Part II
How to Log and Analyze a Space-Based Distributed System
by Philip Bishop & Nigel Warren
October 14, 2002

<<  Page 3 of 6  >>


Logging Implementation

First, we must write a class that implements the SpaceLogger interface and can load the appropriate LoggingStrategy.

public class SpaceLoggerImpl extends UnicastRemoteObject
        implements SpaceLogger {
    private LoggingStrategy _impl;

    public LoggingServiceImpl(String strategyClassName)
            throws RemoteException {
        try {
            Class c = Class.forName(strategyClassName);
            _impl = (LoggingStrategy) c.newInstance();
        catch (Exception ex) {
            throw new
                    RemoteException("Failed to load

    public Object logOperation(SpaceOperation op)
            throws java.rmi.RemoteException {
        return _impl.logOperation(op);

As the code shows, because SpaceLoggerImpl will be used as a remote service, it extends java.rmi.server.UnicastRemoteObject to receive remote calls from the proxy. Most of the interesting stuff occurs in the constructor, which uses the argument strategyClassName as LoggingStrategy's class name. The method continues by using Class.forName() to load the strategy class and uses newInstance() on the Class object to instantiate it. Here, we assume that all LoggingStrategy implementations have a public no-arg constructor.

Finally, in the logOperation() method, we forward the op parameter to the LoggingStrategy class that was loaded and instantiated in the constructor.

As you can see, by breaking the design down into a logging service and logging strategies, we arrive at a simple service that is extensible and easier to maintain. This is because the detail of how the logging occurs is specific to the concrete LoggingStrategy classes rather than embedded in the service class.

<<  Page 3 of 6  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use