View Javadoc

1   /*
2    * $Id: MuleTransactionConfig.java 10524 2008-01-24 19:20:11Z akuzmin $
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.impl;
12  
13  import org.mule.MuleManager;
14  import org.mule.transaction.constraints.ConstraintFilter;
15  import org.mule.umo.UMOTransactionConfig;
16  import org.mule.umo.UMOTransactionFactory;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * <p/> <code>MuleTransactionConfig</code> defines transaction configuration for a
23   * transactional endpoint.
24   */
25  public class MuleTransactionConfig implements UMOTransactionConfig
26  {
27      /**
28       * logger used by this class
29       */
30      protected static final Log logger = LogFactory.getLog(MuleTransactionConfig.class);
31  
32      public static final String ACTION_NONE_STRING = "NONE";
33      public static final String ACTION_ALWAYS_BEGIN_STRING = "ALWAYS_BEGIN";
34      public static final String ACTION_BEGIN_OR_JOIN_STRING = "BEGIN_OR_JOIN";
35      public static final String ACTION_ALWAYS_JOIN_STRING = "ALWAYS_JOIN";
36      public static final String ACTION_JOIN_IF_POSSIBLE_STRING = "JOIN_IF_POSSIBLE";
37  
38      private UMOTransactionFactory factory;
39  
40      private byte action = ACTION_NONE;
41  
42      private ConstraintFilter constraint = null;
43  
44      private int timeout;
45  
46      public MuleTransactionConfig()
47      {
48          timeout = MuleManager.getConfiguration().getTransactionTimeout();
49      }
50  
51      /*
52       * (non-Javadoc)
53       * 
54       * @see org.mule.umo.UMOTransactionConfig#getFactory()
55       */
56      public UMOTransactionFactory getFactory()
57      {
58          return factory;
59      }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.mule.umo.UMOTransactionConfig#setFactory(org.mule.umo.UMOTransactionFactory)
65       */
66      public void setFactory(UMOTransactionFactory factory)
67      {
68          if (factory == null)
69          {
70              throw new IllegalArgumentException("Transaction Factory cannot be null");
71          }
72          this.factory = factory;
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see org.mule.umo.UMOTransactionConfig#getAction()
79       */
80      public byte getAction()
81      {
82          return action;
83      }
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see org.mule.umo.UMOTransactionConfig#setAction(byte)
89       */
90      public void setAction(byte action)
91      {
92          this.action = action;
93  
94      }
95  
96      public void setActionAsString(String action)
97      {
98          if (ACTION_ALWAYS_BEGIN_STRING.equals(action))
99          {
100             this.action = ACTION_ALWAYS_BEGIN;
101         }
102         else if (ACTION_BEGIN_OR_JOIN_STRING.equals(action))
103         {
104             this.action = ACTION_BEGIN_OR_JOIN;
105         }
106         else if (ACTION_ALWAYS_JOIN_STRING.equals(action))
107         {
108             this.action = ACTION_ALWAYS_JOIN;
109         }
110         else if (ACTION_JOIN_IF_POSSIBLE_STRING.equals(action))
111         {
112             this.action = ACTION_JOIN_IF_POSSIBLE;
113         }
114         else if (ACTION_NONE_STRING.equals(action))
115         {
116             this.action = ACTION_NONE;
117         }
118         else
119         {
120             throw new IllegalArgumentException("Action " + action + " is not recognised as a begin action.");
121         }
122     }
123 
124     public String getActionAsString()
125     {
126         switch (action)
127         {
128             case ACTION_ALWAYS_BEGIN :
129                 return ACTION_ALWAYS_BEGIN_STRING;
130             case ACTION_BEGIN_OR_JOIN :
131                 return ACTION_BEGIN_OR_JOIN_STRING; 
132             case ACTION_ALWAYS_JOIN :
133                 return ACTION_ALWAYS_JOIN_STRING;
134             case ACTION_JOIN_IF_POSSIBLE :
135                 return ACTION_JOIN_IF_POSSIBLE_STRING;
136             default :
137                 return ACTION_NONE_STRING;
138         }
139     }
140 
141     public boolean isTransacted()
142     {
143         return isConfigured() && factory.isTransacted() && action != ACTION_NONE;
144     }
145 
146     public boolean isConfigured()
147     {
148         return factory != null;
149     }
150 
151     public ConstraintFilter getConstraint()
152     {
153         if (constraint == null)
154         {
155             return null;
156         }
157         try
158         {
159             return (ConstraintFilter) constraint.clone();
160         }
161         catch (CloneNotSupportedException e)
162         {
163             logger.fatal("Failed to clone ContraintFilter: " + e.getMessage(), e);
164             return constraint;
165         }
166     }
167 
168     public void setConstraint(ConstraintFilter constraint)
169     {
170         this.constraint = constraint;
171     }
172 
173     public int getTimeout()
174     {
175         return timeout;
176     }
177 
178     public void setTimeout(int timeout)
179     {
180         this.timeout = timeout;
181     }
182 
183     public String toString()
184     {
185         StringBuffer buf = new StringBuffer();
186         buf.append("Transaction{factory=")
187             .append(factory)
188             .append(", action=")
189             .append(getActionAsString())
190             .append(", timeout=")
191             .append(timeout)
192             .append("}");
193         return buf.toString();
194     }
195 }