View Javadoc

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