com.threerings.presents.util
Class PersistingUnit

java.lang.Object
  extended by com.samskivert.util.Invoker.Unit
      extended by com.threerings.presents.util.PersistingUnit
All Implemented Interfaces:
Runnable

public abstract class PersistingUnit
extends Invoker.Unit

Simplifies a common pattern which is to post an Invoker unit which does some database operation and then calls back to an InvocationService.InvocationListener of some sort. If the database operation fails, the error will be logged and the result listener will be replied to with InvocationCodes.INTERNAL_ERROR.


Field Summary
protected  Object[] _args
           
protected  Exception _error
           
protected  InvocationService.InvocationListener _listener
           
protected  Object _result
           
protected  boolean _resultSet
           
 
Fields inherited from class com.samskivert.util.Invoker.Unit
_name, queueStamp
 
Constructor Summary
PersistingUnit(InvocationService.InvocationListener listener)
           
PersistingUnit(String name, InvocationService.InvocationListener listener)
           
PersistingUnit(String name, InvocationService.InvocationListener listener, Object... args)
          Creates a persisting unit with the supplied name and listener and a set of key/value pairs that will be included in the failure message if this unit fails.
 
Method Summary
protected  String getFailureMessage()
          Provides a custom failure message in the event that the persistent action fails.
 void handleFailure(Exception error)
          Handles the failure case by logging the error and reporting an internal error to the listener.
 void handleResult()
           
 void handleSuccess()
          Handles the success case, which by default posts a response to a ConfirmListener.
 boolean invoke()
           
abstract  void invokePersistent()
          This method is where the unit performs its persistent actions.
protected  void reportRequestProcessed()
          If the listener is known to be a ConfirmListener, this will cast it and report that the request was processed.
protected  void reportRequestProcessed(Object result)
          If the listener is known to be a ResultListener, this will cast it and report that the request was processed.
protected  void setResult(Object result)
          If the service listener is a ResultListener and this method is called, then the given result will be passed along to the listener on success.
 
Methods inherited from class com.samskivert.util.Invoker.Unit
getDetail, getLongThreshold, run, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_listener

protected InvocationService.InvocationListener _listener

_error

protected Exception _error

_args

protected Object[] _args

_resultSet

protected boolean _resultSet

_result

protected Object _result
Constructor Detail

PersistingUnit

public PersistingUnit(InvocationService.InvocationListener listener)

PersistingUnit

public PersistingUnit(String name,
                      InvocationService.InvocationListener listener)

PersistingUnit

public PersistingUnit(String name,
                      InvocationService.InvocationListener listener,
                      Object... args)
Creates a persisting unit with the supplied name and listener and a set of key/value pairs that will be included in the failure message if this unit fails.

Method Detail

invokePersistent

public abstract void invokePersistent()
                               throws Exception
This method is where the unit performs its persistent actions. Any persistence exception will be caught and logged along with the output from getFailureMessage(), if any.

Throws:
Exception

handleSuccess

public void handleSuccess()
Handles the success case, which by default posts a response to a ConfirmListener. If you need something else, or to repond to a ResultListener, you'll need to override this.


handleFailure

public void handleFailure(Exception error)
Handles the failure case by logging the error and reporting an internal error to the listener.


invoke

public boolean invoke()
Specified by:
invoke in class Invoker.Unit

handleResult

public void handleResult()
Overrides:
handleResult in class Invoker.Unit

reportRequestProcessed

protected void reportRequestProcessed()
If the listener is known to be a ConfirmListener, this will cast it and report that the request was processed.


reportRequestProcessed

protected void reportRequestProcessed(Object result)
If the listener is known to be a ResultListener, this will cast it and report that the request was processed.


getFailureMessage

protected String getFailureMessage()
Provides a custom failure message in the event that the persistent action fails. This will be logged along with the exception.


setResult

protected void setResult(Object result)
If the service listener is a ResultListener and this method is called, then the given result will be passed along to the listener on success.