View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.shared_orchestra.util;
    20
    21  import java.lang.reflect.Method;
    22  import java.util.ArrayList;
    23  import java.util.List;
    24
    25  /**
    26   * Various helpers to deal with exception
    27   *
    28   * @author imario@apache.org
    29   */
    30  public final class ExceptionUtils
    31  {
    32      private ExceptionUtils()
    33      {
    34      }
    35
    36      /**
    37       * <p>
    38       * returns a list of all throwables (including the one you passed in) wrapped by the given throwable.
    39       * In contrast to a simple call to <code>getClause()</code> on each throwable it will also check if the throwable class
    40       * contain a method <code>getRootCause()</code> (e.g. ServletException or JspException) and call it instead.
    41       * </p>
    42       * <p>
    43       * The first list element will your passed in exception, the last list element is the cause.
    44       * </p>
    45       */
    46      public static List getExceptions(Throwable cause)
    47      {
    48          List exceptions = new ArrayList(10);
    49          exceptions.add(cause);
    50
    51          do
    52          {
    53              Throwable nextCause;
    54              try
    55              {
    56                  Method rootCause = cause.getClass().getMethod("getRootCause", new Class[]{});
    57                  nextCause = (Throwable) rootCause.invoke(cause, new Object[]{});
    58              }
    59              catch(Exception e)
    60              {
    61                  nextCause = cause.getCause();
    62              }
    63              if (cause == nextCause)
    64              {
    65                  break;
    66              }
    67
    68              if (nextCause != null)
    69              {
    70                  exceptions.add(nextCause);
    71              }
    72
    73              cause = nextCause;
    74          }
    75          while (cause != null);
    76
    77          return exceptions;
    78      }
    79
    80      /**
    81       * Find a throwable message starting with the last element.<br />
    82       * Returns the first throwable message where <code>throwable.getMessage() != null</code>
    83       */
    84      public static String getExceptionMessage(List throwables)
    85      {
    86          if (throwables == null)
    87          {
    88              return null;
    89          }
    90
    91          for (int i = throwables.size()-1; i>0; i--)
    92          {
    93              Throwable t = (Throwable) throwables.get(i);
    94              if (t.getMessage() != null)
    95              {
    96                  return t.getMessage();
    97              }
    98          }
    99
    100         return null;
    101     }
    102 }