View Javadoc

1   /*
2    * $Id: PayloadTypeFilter.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 static org.mule.util.ClassUtils.equal;
14  import static org.mule.util.ClassUtils.hash;
15  
16  import org.mule.api.MuleMessage;
17  import org.mule.api.routing.filter.Filter;
18  import org.mule.util.ClassUtils;
19  
20  /**
21   * <code>PayloadTypeFilter</code> filters based on the type of the object received.
22   */
23  
24  public class PayloadTypeFilter implements Filter
25  {
26      private Class expectedType;
27  
28      public PayloadTypeFilter()
29      {
30          super();
31      }
32  
33      public PayloadTypeFilter(String expectedType) throws ClassNotFoundException
34      {
35          this(ClassUtils.loadClass(expectedType, PayloadTypeFilter.class));
36      }
37  
38      public PayloadTypeFilter(Class expectedType)
39      {
40          this.expectedType = expectedType;
41      }
42  
43      public boolean accept(MuleMessage message)
44      {
45          return (expectedType != null ? expectedType.isAssignableFrom(message.getPayload().getClass()) : false);
46      }
47  
48      public Class getExpectedType()
49      {
50          return expectedType;
51      }
52  
53      public void setExpectedType(Class expectedType)
54      {
55          this.expectedType = expectedType;
56      }
57      
58      public boolean equals(Object obj)
59      {
60          if (this == obj) return true;
61          if (obj == null || getClass() != obj.getClass()) return false;
62  
63          final PayloadTypeFilter other = (PayloadTypeFilter) obj;
64          return equal(expectedType, other.expectedType);
65      }
66  
67      public int hashCode()
68      {
69          return hash(new Object[]{this.getClass(), expectedType});
70      }
71  }