View Javadoc

1   /*
2    * $Id: ExceptionTypeFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.filters;
12  
13  import org.mule.api.ExceptionPayload;
14  import org.mule.api.MuleMessage;
15  import org.mule.util.ClassUtils;
16  
17  /**
18   * A filter that accepts messages that have an exception payload. An Exception type
19   * can also be set on this filter to allow it to accept Exception messages of a
20   * particular Exception class only.
21   */
22  public class ExceptionTypeFilter extends PayloadTypeFilter
23  {
24  
25      public ExceptionTypeFilter()
26      {
27          super();
28      }
29  
30  
31      public ExceptionTypeFilter(String expectedType) throws ClassNotFoundException
32      {
33          this(ClassUtils.loadClass(expectedType, ExceptionTypeFilter.class));
34      }
35  
36      public ExceptionTypeFilter(Class expectedType)
37      {
38          super(expectedType);
39      }
40  
41      /**
42       * Check a given message against this filter.
43       * 
44       * @param message a non null message to filter.
45       * @return <code>true</code> if the message matches the filter
46       */
47      public boolean accept(MuleMessage message)
48      {
49          ExceptionPayload epl = message.getExceptionPayload();
50  
51          if (getExpectedType() == null)
52          {
53              return epl != null;
54          }
55          else if (epl != null)
56          {
57              return getExpectedType().isAssignableFrom(epl.getException().getClass());
58          }
59          else
60          {
61              return false;
62          }
63      }
64  
65  }