View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  
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      // Recursively search the exception tree looking for the first SQLException
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                  // Look at the cause
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  }