1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util.interceptors;
18
19 import java.sql.SQLException;
20 import java.util.StringTokenizer;
21
22 import org.aopalliance.intercept.MethodInvocation;
23
24 /***
25 * MethodReplayDecisionMaker intended for use with methods marked as
26 * transactional, where the decision to replay the method is based on the
27 * content of the underlying exception from the resource.
28 *
29 * @author a336317
30 * @author a202225
31 */
32 public class TransactionalMethodReplayDecisionMaker implements
33 MethodReplayDecisionMaker
34 {
35
36 private int[] sqlErrorCodes;
37
38 public boolean shouldReplay(MethodInvocation invocation, Exception exception)
39 {
40 SQLException sqlException = findSQLException(exception);
41 if (sqlException != null)
42 {
43 int errorCode = sqlException.getErrorCode();
44
45 if (errorCode != 0) { return isErrorCodeListed(errorCode); }
46 }
47
48 return false;
49 }
50
51
52 protected SQLException findSQLException(Exception exception)
53 {
54 SQLException foundException = null;
55 if (exception != null)
56 {
57 if (exception instanceof SQLException)
58 {
59 foundException = (SQLException) exception;
60 }
61 else
62 {
63
64 Throwable throwable = exception.getCause();
65 if (throwable != null && throwable instanceof Exception)
66 {
67 foundException = findSQLException((Exception) throwable);
68 }
69 }
70 }
71
72 return foundException;
73 }
74
75 public void setSqlErrorCodes(String sqlErrorCodesString)
76 {
77 StringTokenizer tokenizer = new StringTokenizer(sqlErrorCodesString,
78 ",");
79
80 this.sqlErrorCodes = new int[tokenizer.countTokens()];
81
82 for (int i = 0; tokenizer.hasMoreTokens(); i++)
83 {
84 this.sqlErrorCodes[i] = new Integer(tokenizer.nextToken())
85 .intValue();
86 }
87 }
88
89 private boolean isErrorCodeListed(int errorCode)
90 {
91 for (int i = 0; i < this.sqlErrorCodes.length; i++)
92 {
93 if (this.sqlErrorCodes[i] == errorCode) return true;
94 }
95
96 return false;
97 }
98 }