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