01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: private int[] sqlErrorCodes;
36:
37: public boolean shouldReplay(MethodInvocation invocation,
38: Exception exception) {
39: SQLException sqlException = findSQLException(exception);
40: if (sqlException != null) {
41: int errorCode = sqlException.getErrorCode();
42:
43: if (errorCode != 0) {
44: return isErrorCodeListed(errorCode);
45: }
46: }
47:
48: return false;
49: }
50:
51: // Recursively search the exception tree looking for the first SQLException
52: protected SQLException findSQLException(Exception exception) {
53: SQLException foundException = null;
54: if (exception != null) {
55: if (exception instanceof SQLException) {
56: foundException = (SQLException) exception;
57: } else {
58: // Look at the cause
59: Throwable throwable = exception.getCause();
60: if (throwable != null && throwable instanceof Exception) {
61: foundException = findSQLException((Exception) throwable);
62: }
63: }
64: }
65:
66: return foundException;
67: }
68:
69: public void setSqlErrorCodes(String sqlErrorCodesString) {
70: StringTokenizer tokenizer = new StringTokenizer(
71: sqlErrorCodesString, ",");
72:
73: this .sqlErrorCodes = new int[tokenizer.countTokens()];
74:
75: for (int i = 0; tokenizer.hasMoreTokens(); i++) {
76: this .sqlErrorCodes[i] = new Integer(tokenizer.nextToken())
77: .intValue();
78: }
79: }
80:
81: private boolean isErrorCodeListed(int errorCode) {
82: for (int i = 0; i < this .sqlErrorCodes.length; i++) {
83: if (this .sqlErrorCodes[i] == errorCode)
84: return true;
85: }
86:
87: return false;
88: }
89: }
|