FileDocCategorySizeDatePackage
TransactionManager.javaAPI DocAzureus 3.0.3.46218Sat Jun 02 14:13:56 BST 2007com.aelitis.azureus.ui.swt.browser.txn

TransactionManager

public class TransactionManager extends Object
Manages the context for a single SWT {@link Browser} component.
author
dharkness
created
Jul 19, 2006

Fields Summary
private com.aelitis.azureus.core.messenger.ClientMessageContext
context
private Map
txnCtors
private Map
txns
private int
lastTxnId
Constructors Summary
public TransactionManager(com.aelitis.azureus.core.messenger.ClientMessageContext context)



          
        this.context = context;
    
Methods Summary
public synchronized TransactioncancelTransaction(java.lang.String type)
Cancels the current transaction if there is one for this listener and the browser in the given message.

param
type used to determine the transaction to return
return
the current transaction or null if none exists

        Transaction txn = getTransaction(type);
        if ( txn != null ) {
            txn.cancel();
        }
        
        return txn;
    
protected TransactioncreateTransaction(java.lang.String type)
Creates a new transaction for this listener and the browser in the given message. If no transaction class was specified in the constructor, subclasses must override this method.

param
message holds a reference to the browser
return
a new transaction

        Constructor ctor = (Constructor) txnCtors.get(type);
        if ( ctor == null ) {
            throw new IllegalStateException("Unregistered transaction type: " + type);
        }
        
        try {
            Object[] params = 
                    new Object[] { 
                        new Integer(getNextTransactionId()), 
                        type, 
                        context
                    };
            return (Transaction) ctor.newInstance(params);
        }
        catch ( Exception e ) {
            throw new RuntimeException("Exception creating transaction for type " + type, e);
        }
    
private intgetNextTransactionId()
Increments the last transaction ID and returns the new value.

return
a unique ID to use for a new transaction

        return ++lastTxnId;
    
public synchronized TransactiongetTransaction(java.lang.String type)
Returns the current transaction of the given type.

param
type used to locate the transaction
return
the transaction or null if none exists

        return (Transaction) txns.get(type);
    
public voidregisterTransactionType(java.lang.String type, java.lang.Class clazz)
Registers the given transaction subclass as that to be created for the given type identifier.

param
clazz used when creating transactions
throws
IllegalArgumentException if txnClass is null or not a subclass of {@link Transaction}
throws
IllegalArgumentException if the appropriate constructor of txnClass cannot be found or accessed

        if ( clazz == null ) {
            throw new IllegalArgumentException("Transaction class must be non-null");
        }
        if ( ! Transaction.class.isAssignableFrom(clazz) ) {
            throw new IllegalArgumentException("Transaction class " + clazz.getName() 
                    + " must be a subclass of Transaction");
        }
        
        try {
            Class[] ctorParams = 
                    new Class[] { 
                        Integer.TYPE, 
                        String.class, 
                        ClientMessageContext.class 
                    };
            Constructor ctor = clazz.getConstructor(ctorParams);
            txnCtors.put(type, ctor);
        }
        catch ( Exception e ) {
            throw new IllegalArgumentException(
                    "Cannot access appropriate constructor for " 
                    + clazz.getName());
        }
    
synchronized booleanremoveTransaction(Transaction txn)
Removes the given transaction from current status if it's current.

param
txn the Transaction to be removed
return
true if the transaction was current; false otherwise

        if ( txn != getTransaction(txn.getType()) ) {
            return false;
        }
        
        return txns.remove(txn.getType()) == txn;
    
public synchronized TransactionstartTransaction(java.lang.String type)
Starts a new transaction of the given type after canceling the current transaction if any.

param
type used to determine the transaction class to create
return
a new started transaction

        cancelTransaction(type);
        
        Transaction txn = createTransaction(type);
        if ( txn != null ) {
            if ( ! txn.start() ) {
                return null;
            }
            
            txns.put(type, txn);
        }
        
        return txn;