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