View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    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  package org.codehaus.activemq.util;
    19
    20  import org.codehaus.activemq.util.JMSExceptionHelper;
    21
    22  import javax.jms.JMSException;
    23
    24  /***
    25   * A helper class for ensuring that a number of tasks occur, whether they
    26   * throw exceptions or not and saving the first exception so that we can
    27   * throw it properly.
    28   * This class is particularly useful for shutting things down, where we
    29   * want to try close all resources, whether they fail or not.
    30   *
    31   * @version $Revision: 1.1 $
    32   */
    33  public class ExceptionTemplate {
    34      private Throwable firstException;
    35
    36      public ExceptionTemplate() {
    37      }
    38
    39      public void run(Callback task) {
    40          try {
    41              task.execute();
    42          }
    43          catch (Throwable t) {
    44              if (firstException == null) {
    45                  firstException = t;
    46              }
    47          }
    48      }
    49
    50      /***
    51       * Returns the first exception thrown during the execution of this
    52       * template or returns null if there has been no exception thrown yet.
    53       *
    54       * @return the first caught exception or null if none has occured yet
    55       */
    56      public Throwable getFirstException() {
    57          return firstException;
    58      }
    59
    60      /***
    61       * Throws the first exception caught during the execution of this template
    62       * as a JMS exception or do nothing if we have not caught and exception
    63       *
    64       * @throws JMSException
    65       */
    66      public void throwJMSException() throws JMSException {
    67          if (firstException != null) {
    68              if (firstException instanceof JMSException) {
    69                    throw (JMSException) firstException;
    70              }
    71              else if (firstException instanceof Exception) {
    72                  throw JMSExceptionHelper.newJMSException(firstException.getMessage(), (Exception) firstException);
    73              }
    74              else {
    75                  // lets wrap the Throwable in an Exception
    76                  throw JMSExceptionHelper.newJMSException(firstException.getMessage(), new Exception(firstException));
    77              }
    78          }
    79      }
    80  }