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