Coverage Report - org.apache.ojb.broker.util.ExceptionHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionHelper
N/A
N/A
8.25
 
 1  
 package org.apache.ojb.broker.util;
 2  
 
 3  
 /* Copyright 2002-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * 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  
 
 18  
 import java.sql.SQLException;
 19  
 import java.sql.BatchUpdateException;
 20  
 import java.sql.SQLWarning;
 21  
 
 22  
 import org.apache.commons.lang.SystemUtils;
 23  
 import org.apache.commons.lang.exception.ExceptionUtils;
 24  
 import org.apache.ojb.broker.KeyConstraintViolatedException;
 25  
 import org.apache.ojb.broker.PersistenceBrokerSQLException;
 26  
 import org.apache.ojb.broker.core.ValueContainer;
 27  
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 28  
 import org.apache.ojb.broker.metadata.FieldDescriptor;
 29  
 import org.apache.ojb.broker.metadata.JdbcTypesHelper;
 30  
 import org.apache.ojb.broker.util.logging.Logger;
 31  
 
 32  
 /**
 33  
  * A helper class which endorse dealing with exceptions.
 34  
  *
 35  
  * @version $Id: ExceptionHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
 36  
  */
 37  
 abstract public class ExceptionHelper
 38  
 {
 39  
     /**
 40  
      * Method which support the conversion of {@link java.sql.SQLException} to
 41  
      * OJB's runtime exception (with additional message details).
 42  
      *
 43  
      * @param message The error message to use, if <em>null</em> a standard message is used.
 44  
      * @param ex The exception to convert (mandatory).
 45  
      * @param sql The used sql-statement or <em>null</em>.
 46  
      * @param logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed message
 47  
      * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em> to skip logging message.
 48  
      * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
 49  
      *         arguments.
 50  
      */
 51  
     public static PersistenceBrokerSQLException generateException(String message, SQLException ex,  String sql, Logger logger)
 52  
     {
 53  
         return generateException(message, ex, sql, null, null, logger, null);
 54  
     }
 55  
 
 56  
     /**
 57  
      * Method which support the conversion of {@link java.sql.SQLException} to
 58  
      * OJB's runtime exception (with additional message details).
 59  
      *
 60  
      * @param ex The exception to convert (mandatory).
 61  
      * @param sql The used sql-statement or <em>null</em>.
 62  
      * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the target object or <em>null</em>.
 63  
      * @param logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed message
 64  
      * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em> to skip logging message.
 65  
      * @param obj The target object or <em>null</em>.
 66  
      * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
 67  
      *         arguments.
 68  
      */
 69  
     public static PersistenceBrokerSQLException generateException(SQLException ex,  String sql, ClassDescriptor cld, Logger logger, Object obj)
 70  
     {
 71  
         return generateException(ex, sql, cld, null, logger, obj);
 72  
     }
 73  
 
 74  
     /**
 75  
      * Method which support the conversion of {@link java.sql.SQLException} to
 76  
      * OJB's runtime exception (with additional message details).
 77  
      *
 78  
      * @param ex The exception to convert (mandatory).
 79  
      * @param sql The used sql-statement or <em>null</em>.
 80  
      * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the target object or <em>null</em>.
 81  
      * @param values The values set in prepared statement or <em>null</em>.
 82  
      * @param logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed message
 83  
      * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em> to skip logging message.
 84  
      * @param obj The target object or <em>null</em>.
 85  
      * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
 86  
      *         arguments.
 87  
      */
 88  
     public static PersistenceBrokerSQLException generateException(SQLException ex,  String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj)
 89  
     {
 90  
         return generateException(null, ex, sql, cld, values, logger, obj);
 91  
     }
 92  
 
 93  
     /**
 94  
      * Method which support the conversion of {@link java.sql.SQLException} to
 95  
      * OJB's runtime exception (with additional message details).
 96  
      *
 97  
      * @param message The error message to use, if <em>null</em> a standard message is used.
 98  
      * @param ex The exception to convert (mandatory).
 99  
      * @param sql The used sql-statement or <em>null</em>.
 100  
      * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the target object or <em>null</em>.
 101  
      * @param values The values set in prepared statement or <em>null</em>.
 102  
      * @param logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed message
 103  
      * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em> to skip logging message.
 104  
      * @param obj The target object or <em>null</em>.
 105  
      * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
 106  
      *         arguments.
 107  
      */
 108  
     public static PersistenceBrokerSQLException generateException(String message, SQLException ex,  String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj)
 109  
     {
 110  
         /*
 111  
         X/OPEN codes within class 23:
 112  
         23000        INTEGRITY CONSTRAINT VIOLATION
 113  
         23001        RESTRICT VIOLATION
 114  
         23502        NOT NULL VIOLATION
 115  
         23503        FOREIGN KEY VIOLATION
 116  
         23505        UNIQUE VIOLATION
 117  
         23514        CHECK VIOLATION
 118  
         */
 119  
         String eol = SystemUtils.LINE_SEPARATOR;
 120  
         StringBuffer msg = new StringBuffer(eol);
 121  
         eol += "* ";
 122  
 
 123  
         if(ex instanceof BatchUpdateException)
 124  
         {
 125  
             BatchUpdateException tmp = (BatchUpdateException) ex;
 126  
             if(message != null)
 127  
             {
 128  
                 msg.append("* ").append(message);
 129  
             }
 130  
             else
 131  
             {
 132  
                 msg.append("* BatchUpdateException during execution of sql-statement:");
 133  
             }
 134  
             msg.append(eol).append("Batch update count is '").append(tmp.getUpdateCounts()).append("'");
 135  
         }
 136  
         else if(ex instanceof SQLWarning)
 137  
         {
 138  
             if(message != null)
 139  
             {
 140  
                 msg.append("* ").append(message);
 141  
             }
 142  
             else
 143  
             {
 144  
                 msg.append("* SQLWarning during execution of sql-statement:");
 145  
             }
 146  
         }
 147  
         else
 148  
         {
 149  
             if(message != null)
 150  
             {
 151  
                 msg.append("* ").append(message);
 152  
             }
 153  
             else
 154  
             {
 155  
                 msg.append("* SQLException during execution of sql-statement:");
 156  
             }
 157  
         }
 158  
 
 159  
         if(sql != null)
 160  
         {
 161  
             msg.append(eol).append("sql statement was '").append(sql).append("'");
 162  
         }
 163  
         String stateCode = null;
 164  
         if(ex != null)
 165  
         {
 166  
             msg.append(eol).append("Exception message is [").append(ex.getMessage()).append("]");
 167  
             msg.append(eol).append("Vendor error code [").append(ex.getErrorCode()).append("]");
 168  
             msg.append(eol).append("SQL state code [");
 169  
 
 170  
             stateCode = ex.getSQLState();
 171  
             if("23000".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=INTEGRITY CONSTRAINT VIOLATION");
 172  
             else if("23001".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=RESTRICT VIOLATION");
 173  
             else if("23502".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=NOT NULL VIOLATION");
 174  
             else if("23503".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=FOREIGN KEY VIOLATION");
 175  
             else if("23505".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=UNIQUE VIOLATION");
 176  
             else if("23514".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=CHECK VIOLATION");
 177  
             else msg.append(stateCode);
 178  
             msg.append("]");
 179  
         }
 180  
 
 181  
         if(cld != null)
 182  
         {
 183  
             msg.append(eol).append("Target class is '")
 184  
                     .append(cld.getClassNameOfObject())
 185  
                     .append("'");
 186  
             FieldDescriptor[] fields = cld.getPkFields();
 187  
             msg.append(eol).append("PK of the target object is [");
 188  
             for(int i = 0; i < fields.length; i++)
 189  
             {
 190  
                 try
 191  
                 {
 192  
                     if(i > 0) msg.append(", ");
 193  
                     msg.append(fields[i].getPersistentField().getName());
 194  
                     if(obj != null)
 195  
                     {
 196  
                         msg.append("=");
 197  
                         msg.append(fields[i].getPersistentField().get(obj));
 198  
                     }
 199  
                 }
 200  
                 catch(Exception ignore)
 201  
                 {
 202  
                     msg.append(" PK field build FAILED! ");
 203  
                 }
 204  
             }
 205  
             msg.append("]");
 206  
         }
 207  
         if(values != null)
 208  
         {
 209  
             msg.append(eol).append(values.length).append(" values performed in statement: ").append(eol);
 210  
             for(int i = 0; i < values.length; i++)
 211  
             {
 212  
                 ValueContainer value = values[i];
 213  
                 msg.append("[");
 214  
                 msg.append("jdbcType=").append(JdbcTypesHelper.getSqlTypeAsString(value.getJdbcType().getType()));
 215  
                 msg.append(", value=").append(value.getValue());
 216  
                 msg.append("]");
 217  
             }
 218  
         }
 219  
         if(obj != null)
 220  
         {
 221  
             msg.append(eol).append("Source object: ");
 222  
             try
 223  
             {
 224  
                 msg.append(obj.toString());
 225  
             }
 226  
             catch(Exception e)
 227  
             {
 228  
                 msg.append(obj.getClass());
 229  
             }
 230  
         }
 231  
 
 232  
         // message string for PB exception
 233  
         String shortMsg = msg.toString();
 234  
 
 235  
         if(ex != null)
 236  
         {
 237  
             // add causing stack trace
 238  
             Throwable rootCause = ExceptionUtils.getRootCause(ex);
 239  
             if(rootCause == null) rootCause = ex;
 240  
             msg.append(eol).append("The root stack trace is --> ");
 241  
             String rootStack = ExceptionUtils.getStackTrace(rootCause);
 242  
             msg.append(eol).append(rootStack);
 243  
         }
 244  
         msg.append(SystemUtils.LINE_SEPARATOR).append("**");
 245  
 
 246  
         // log error message
 247  
         if(logger != null) logger.error(msg.toString());
 248  
 
 249  
         // throw a specific type of runtime exception for a key constraint.
 250  
         if("23000".equals(stateCode) || "23505".equals(stateCode))
 251  
         {
 252  
             throw new KeyConstraintViolatedException(shortMsg, ex);
 253  
         }
 254  
         else
 255  
         {
 256  
             throw new PersistenceBrokerSQLException(shortMsg, ex);
 257  
         }
 258  
     }
 259  
 }