1
2
3
4
5
6
7
8
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
26
27
28 public class MuleTransactionConfig implements TransactionConfig
29 {
30
31
32
33 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 private byte action = ACTION_DEFAULT;
45
46 private ConstraintFilter constraint = null;
47
48 private int timeout;
49
50 public MuleTransactionConfig()
51 {
52
53 }
54
55 public TransactionFactory getFactory()
56 {
57 return factory;
58 }
59
60 public void setFactory(TransactionFactory factory)
61 {
62 if (factory == null)
63 {
64 throw new IllegalArgumentException("Transaction Factory cannot be null");
65 }
66 this.factory = factory;
67 }
68
69 public byte getAction()
70 {
71 return action;
72 }
73
74 public void setAction(byte action)
75 {
76 this.action = action;
77
78 }
79
80 public void setActionAsString(String action)
81 {
82 if (ACTION_ALWAYS_BEGIN_STRING.equals(action))
83 {
84 this.action = ACTION_ALWAYS_BEGIN;
85 }
86 else if (ACTION_BEGIN_OR_JOIN_STRING.equals(action))
87 {
88 this.action = ACTION_BEGIN_OR_JOIN;
89 }
90 else if (ACTION_ALWAYS_JOIN_STRING.equals(action))
91 {
92 this.action = ACTION_ALWAYS_JOIN;
93 }
94 else if (ACTION_JOIN_IF_POSSIBLE_STRING.equals(action))
95 {
96 this.action = ACTION_JOIN_IF_POSSIBLE;
97 }
98 else if (ACTION_NONE_STRING.equals(action))
99 {
100 this.action = ACTION_NONE;
101 }
102 else if (ACTION_NEVER_STRING.equals(action))
103 {
104 this.action = ACTION_NEVER;
105 }
106 else
107 {
108 throw new IllegalArgumentException("Action " + action + " is not recognised as a begin action.");
109 }
110 }
111
112 public String getActionAsString()
113 {
114 switch (action)
115 {
116 case ACTION_ALWAYS_BEGIN:
117 return ACTION_ALWAYS_BEGIN_STRING;
118 case ACTION_BEGIN_OR_JOIN:
119 return ACTION_BEGIN_OR_JOIN_STRING;
120 case ACTION_ALWAYS_JOIN:
121 return ACTION_ALWAYS_JOIN_STRING;
122 case ACTION_JOIN_IF_POSSIBLE:
123 return ACTION_JOIN_IF_POSSIBLE_STRING;
124 case ACTION_NONE:
125 return ACTION_NONE_STRING;
126 default :
127 return ACTION_NEVER_STRING;
128 }
129 }
130
131 public boolean isTransacted()
132 {
133 Transaction tx = TransactionCoordination.getInstance().getTransaction();
134 boolean joinPossible = (action != ACTION_JOIN_IF_POSSIBLE || (action == ACTION_JOIN_IF_POSSIBLE && tx != null));
135 if (action != ACTION_NEVER && action != ACTION_NONE && factory == null)
136 {
137
138 throw new MuleRuntimeException(CoreMessages.transactionFactoryIsMandatory(getActionAsString()));
139 }
140 return action != ACTION_NEVER && action != ACTION_NONE && factory.isTransacted() && joinPossible;
141 }
142
143 public boolean isConfigured()
144 {
145 return factory != null;
146 }
147
148 public ConstraintFilter getConstraint()
149 {
150 if (constraint == null)
151 {
152 return null;
153 }
154 try
155 {
156 return (ConstraintFilter) constraint.clone();
157 }
158 catch (CloneNotSupportedException e)
159 {
160 logger.fatal("Failed to clone ContraintFilter: " + e.getMessage(), e);
161 return constraint;
162 }
163 }
164
165 public void setConstraint(ConstraintFilter constraint)
166 {
167 this.constraint = constraint;
168 }
169
170 public int getTimeout()
171 {
172 return timeout;
173 }
174
175 public void setTimeout(int timeout)
176 {
177 this.timeout = timeout;
178 }
179
180 public String toString()
181 {
182 StringBuffer buf = new StringBuffer();
183 buf.append("Transaction{factory=")
184 .append(factory)
185 .append(", action=")
186 .append(getActionAsString())
187 .append(", timeout=")
188 .append(timeout)
189 .append("}");
190 return buf.toString();
191 }
192
193 public int hashCode()
194 {
195 return ClassUtils.hash(new Object[]{factory, new Byte(action), constraint, new Integer(timeout)});
196 }
197
198 public boolean equals(Object obj)
199 {
200 if (this == obj) return true;
201 if (obj == null || getClass() != obj.getClass()) return false;
202
203 final MuleTransactionConfig other = (MuleTransactionConfig) obj;
204 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 }