Coverage Report - org.mule.transaction.MuleTransactionConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleTransactionConfig
46%
27/59
33%
19/58
2.812
 
 1  
 /*
 2  
  * $Id: MuleTransactionConfig.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transaction;
 12  
 
 13  
 import org.mule.api.MuleRuntimeException;
 14  
 import org.mule.api.transaction.Transaction;
 15  
 import org.mule.api.transaction.TransactionConfig;
 16  
 import org.mule.api.transaction.TransactionFactory;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 import org.mule.transaction.constraints.ConstraintFilter;
 19  
 import org.mule.util.ClassUtils;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 /**
 25  
  * <p/> <code>MuleTransactionConfig</code> defines transaction configuration for a
 26  
  * transactional endpoint.
 27  
  */
 28  
 public class MuleTransactionConfig implements TransactionConfig
 29  
 {
 30  
     /**
 31  
      * logger used by this class
 32  
      */
 33  2
     protected static final Log logger = LogFactory.getLog(MuleTransactionConfig.class);
 34  
 
 35  
     public static final String ACTION_NONE_STRING = "NONE";
 36  
     public static final String ACTION_ALWAYS_BEGIN_STRING = "ALWAYS_BEGIN";
 37  
     public static final String ACTION_BEGIN_OR_JOIN_STRING = "BEGIN_OR_JOIN";
 38  
     public static final String ACTION_ALWAYS_JOIN_STRING = "ALWAYS_JOIN";
 39  
     public static final String ACTION_JOIN_IF_POSSIBLE_STRING = "JOIN_IF_POSSIBLE";
 40  
     public static final String ACTION_NEVER_STRING = "NEVER";
 41  
 
 42  
     private TransactionFactory factory;
 43  
 
 44  492
     private byte action = ACTION_DEFAULT;
 45  
 
 46  492
     private ConstraintFilter constraint = null;
 47  
 
 48  
     private int timeout;
 49  
     
 50  
     public MuleTransactionConfig()
 51  492
     {
 52  
         // todo timeout = RegistryContext.getConfiguration().getDefaultTransactionTimeout();
 53  492
     }
 54  
 
 55  
     public TransactionFactory getFactory()
 56  
     {
 57  0
         return factory;
 58  
     }
 59  
 
 60  
     public void setFactory(TransactionFactory factory)
 61  
     {
 62  2
         if (factory == null)
 63  
         {
 64  0
             throw new IllegalArgumentException("Transaction Factory cannot be null");
 65  
         }
 66  2
         this.factory = factory;
 67  2
     }
 68  
 
 69  
     public byte getAction()
 70  
     {
 71  106
         return action;
 72  
     }
 73  
 
 74  
     public void setAction(byte action)
 75  
     {
 76  14
         this.action = action;
 77  
 
 78  14
     }
 79  
 
 80  
     public void setActionAsString(String action)
 81  
     {
 82  0
         if (ACTION_ALWAYS_BEGIN_STRING.equals(action))
 83  
         {
 84  0
             this.action = ACTION_ALWAYS_BEGIN;
 85  
         }
 86  0
         else if (ACTION_BEGIN_OR_JOIN_STRING.equals(action))
 87  
         {
 88  0
             this.action = ACTION_BEGIN_OR_JOIN;
 89  
         }
 90  0
         else if (ACTION_ALWAYS_JOIN_STRING.equals(action))
 91  
         {
 92  0
             this.action = ACTION_ALWAYS_JOIN;
 93  
         }
 94  0
         else if (ACTION_JOIN_IF_POSSIBLE_STRING.equals(action))
 95  
         {
 96  0
             this.action = ACTION_JOIN_IF_POSSIBLE;
 97  
         }
 98  0
         else if (ACTION_NONE_STRING.equals(action))
 99  
         {
 100  0
             this.action = ACTION_NONE;
 101  
         }
 102  0
         else if (ACTION_NEVER_STRING.equals(action))
 103  
         {
 104  0
             this.action = ACTION_NEVER;
 105  
         }
 106  
         else
 107  
         {
 108  0
             throw new IllegalArgumentException("Action " + action + " is not recognised as a begin action.");
 109  
         }
 110  0
     }
 111  
 
 112  
     public String getActionAsString()
 113  
     {
 114  988
         switch (action)
 115  
         {
 116  
             case ACTION_ALWAYS_BEGIN:
 117  4
                 return ACTION_ALWAYS_BEGIN_STRING;
 118  
             case ACTION_BEGIN_OR_JOIN:
 119  2
                 return ACTION_BEGIN_OR_JOIN_STRING; 
 120  
             case ACTION_ALWAYS_JOIN:
 121  2
                 return ACTION_ALWAYS_JOIN_STRING;
 122  
             case ACTION_JOIN_IF_POSSIBLE:
 123  2
                 return ACTION_JOIN_IF_POSSIBLE_STRING;
 124  
             case ACTION_NONE:
 125  2
                 return ACTION_NONE_STRING;
 126  
             default :
 127  976
                 return ACTION_NEVER_STRING;
 128  
         }
 129  
     }
 130  
 
 131  
     public boolean isTransacted()
 132  
     {
 133  4
         Transaction tx = TransactionCoordination.getInstance().getTransaction(); 
 134  4
         boolean joinPossible = (action != ACTION_JOIN_IF_POSSIBLE || (action == ACTION_JOIN_IF_POSSIBLE && tx != null));
 135  4
         if (action != ACTION_NEVER && action != ACTION_NONE && factory == null)
 136  
         {
 137  
             // TODO use TransactionException here? This causes API changes as TE is a checked exception ...
 138  2
             throw new MuleRuntimeException(CoreMessages.transactionFactoryIsMandatory(getActionAsString()));
 139  
         }
 140  2
         return action != ACTION_NEVER && action != ACTION_NONE && factory.isTransacted() && joinPossible;
 141  
     }
 142  
     
 143  
     public boolean isConfigured()
 144  
     {
 145  0
         return factory != null;
 146  
     }
 147  
 
 148  
     public ConstraintFilter getConstraint()
 149  
     {
 150  0
         if (constraint == null)
 151  
         {
 152  0
             return null;
 153  
         }
 154  
         try
 155  
         {
 156  0
             return (ConstraintFilter) constraint.clone();
 157  
         }
 158  0
         catch (CloneNotSupportedException e)
 159  
         {
 160  0
             logger.fatal("Failed to clone ContraintFilter: " + e.getMessage(), e);
 161  0
             return constraint;
 162  
         }
 163  
     }
 164  
 
 165  
     public void setConstraint(ConstraintFilter constraint)
 166  
     {
 167  0
         this.constraint = constraint;
 168  0
     }
 169  
 
 170  
     public int getTimeout()
 171  
     {
 172  0
         return timeout;
 173  
     }
 174  
 
 175  
     public void setTimeout(int timeout)
 176  
     {
 177  0
         this.timeout = timeout;
 178  0
     }
 179  
 
 180  
     public String toString()
 181  
     {
 182  976
         StringBuffer buf = new StringBuffer();
 183  976
         buf.append("Transaction{factory=")
 184  
             .append(factory)
 185  
             .append(", action=")
 186  
             .append(getActionAsString())
 187  
             .append(", timeout=")
 188  
             .append(timeout)
 189  
             .append("}");
 190  976
         return buf.toString();
 191  
     }
 192  
     
 193  
     public int hashCode()
 194  
     {
 195  980
         return ClassUtils.hash(new Object[]{factory, new Byte(action), constraint, new Integer(timeout)});
 196  
     }
 197  
 
 198  
     public boolean equals(Object obj)
 199  
     {
 200  0
         if (this == obj) return true;
 201  0
         if (obj == null || getClass() != obj.getClass()) return false;
 202  
 
 203  0
         final MuleTransactionConfig other = (MuleTransactionConfig) obj;
 204  0
         return ClassUtils.equal(factory, other.factory)
 205  
                && ClassUtils.equal(new Byte(action), new Byte(other.action))
 206  
                && ClassUtils.equal(constraint, other.constraint)
 207  
                && ClassUtils.equal(new Integer(timeout), new Integer(other.timeout));
 208  
 
 209  
     }
 210  
     
 211  
 }