FileDocCategorySizeDatePackage
AbstractTransactionalListener.javaAPI DocAzureus 3.0.3.47498Mon Dec 04 11:59:36 GMT 2006com.aelitis.azureus.ui.swt.browser.txn

AbstractTransactionalListener

public abstract class AbstractTransactionalListener extends com.aelitis.azureus.ui.swt.browser.msg.AbstractMessageListener
A listener that requires a {@link Transaction} before dispatching messages.
author
dharkness
created
Jul 19, 2006

Fields Summary
public static final String
TXN_REQUIRED
Start a new transaction or use an existing one (like J2EE REQUIRED)
public static final String
TXN_REQUIRES_NEW
Start a new transaction, aborting any existing one (like J2EE REQUIRES_NEW)
public static final String
TXN_MANDATORY
Require an existing transaction, erroring if there isn't one (like J2EE MANDATORY)
public static final String
TXN_NONE
Handle without a transaction, ignoring any existing one (like J2EE NOT_SUPPORTED)
private String
txnType
private Map
txnStatesByOp
Constructors Summary
public AbstractTransactionalListener(String id)
Uses the listener's ID as the transaction type.

param
id uniquely identifies this listener to the dispatcher and defines the transaction type



                                              
          
        this(id, id);
    
public AbstractTransactionalListener(String id, String txnType)
Provides a transaction type different from the listener's ID.

param
id uniquely identifies this listener to the dispatcher
param
txnType used to access transactions via the context

        super(id);
        this.txnType = txnType;
    
Methods Summary
protected TransactioncancelTransaction()
Cancels the current transaction if there is one for this listener.

return
the cancelled transaction or null if none exists

        return context.cancelTransaction(txnType);
    
protected java.lang.StringgetOperationTxnState(java.lang.String operationId)
Returns the transaction state (attribute)

param
operationId
return

        return (String) txnStatesByOp.get(operationId);
    
protected TransactiongetTransaction()
Returns the current transaction for this listener.

return
the current transaction or null if none exists

        return context.getTransaction(txnType);
    
public java.lang.StringgetTransactionType()
Returns the transaction type used by this listener.

return
the transaction type used by this listener

        return txnType;
    
public voidhandleMessage(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message)
Ensures the correct transaction state before handling the message.

param
message contains the operation ID used to lookup the transaction state

        String state = getOperationTxnState(message.getOperationId());
        
        if ( state == null ) {
            context.debug("Ignoring message without transactional state: " + message);
            return;
        }
        
        Transaction txn = getTransaction();
        if ( state.equals(TXN_REQUIRED) ) {
            // start new if none exists
            if ( txn == null ) {
                txn = startTransaction();
            }
        }
        else if ( state.equals(TXN_REQUIRES_NEW) ) {
            // cancel current and start new
            cancelTransaction();
            txn = startTransaction();
        }
        else if ( state.equals(TXN_MANDATORY) ) {
            // use existing or error
            if ( txn == null ) {
                transactionStateViolated(message, state, null);
                return;
            }
        }
        else if ( state.equals(TXN_NONE) ) {
            // ignore existing
            if ( txn != null ) {
                txn = null;
            }
        }
        else {
            context.debug("Ignoring message with invalid transactional state (" + state + "): " + message);
            return;
        }
        
        if ( txn != null ) {
            handleTxnlMessage(message, txn);
        }
        else {
            handleNonTxnlMessage(message);
        }
    
protected voidhandleNonTxnlMessage(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message)
Handle a browser message that doesn't require a transaction. Subclasses must override if they have any non-transactional operations.

param
message the mesage to be handled

        
    
protected abstract voidhandleTxnlMessage(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message, Transaction txn)
Handle a browser message that require a transaction. Subclasses must implement this method, otherwise they should extend {@link AbstractMessageListener}.

param
message the mesage to be handled
param
txn the current transaction

private voidregisterOperation(java.lang.String operationId, java.lang.String txnState)

        txnStatesByOp.put(operationId, txnState);
    
protected voidregisterOperationTxnMandatory(java.lang.String operationId)

        registerOperation(operationId, TXN_MANDATORY);
    
protected voidregisterOperationTxnNone(java.lang.String operationId)

        registerOperation(operationId, TXN_NONE);
    
protected voidregisterOperationTxnRequired(java.lang.String operationId)

        registerOperation(operationId, TXN_REQUIRED);
    
protected voidregisterOperationTxnRequiresNew(java.lang.String operationId)

        registerOperation(operationId, TXN_REQUIRES_NEW);
    
protected TransactionstartTransaction()
Starts a new transaction for this listener.

return
a new started transaction

        return context.startTransaction(txnType);
    
protected voidtransactionStateViolated(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message, java.lang.String state, Transaction txn)
Called when the necessary transactional state is not correct for the given message. Override to handle the error more gracefully.

param
message the message being handled
param
state the state required by the message
param
txn the existing transaction or null if none

        throw new IllegalStateException("Transaction state violated - state: " + state + ", txn: " + txn);