View Javadoc

1   /*
2    * $Id: SxcFilter.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.module.sxc;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.routing.filter.Filter;
15  
16  import com.envoisolutions.sxc.xpath.XPathBuilder;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import static org.mule.util.ClassUtils.equal;
22  import static org.mule.util.ClassUtils.hash;
23  
24  public class SxcFilter implements Filter
25  {
26      protected transient Log logger = LogFactory.getLog(getClass());
27  
28      private String pattern;
29      
30      public SxcFilter()
31      {
32          super();
33      }
34  
35      public SxcFilter(String pattern)
36      {
37          this.pattern = pattern;
38      }
39  
40      public boolean accept(MuleMessage msg)
41      {
42          Object accept = msg.getInvocationProperty(toString());
43           
44           if (accept == null && SxcFilteringOutboundRouter.getCurrentMessage() == null) 
45           {
46               return false;
47           }
48           else if (accept == null)
49           {
50               throw new UndefinedMatchException();
51           }
52           
53           return (Boolean) accept;
54      }
55  
56      /** @return XPath expression */
57      public String getPattern()
58      {
59          return pattern;
60      }
61  
62      /** @param pattern The XPath expression */
63      public void setPattern(String pattern)
64      {
65          this.pattern = pattern;
66      }
67  
68      public void addEventHandler(SxcFilteringOutboundRouter router, XPathBuilder builder)
69      {    
70          builder.listen(pattern, new FilterEventHandler(router, this));
71      }
72      
73      public boolean equals(Object obj)
74      {
75          if (this == obj) return true;
76          if (obj == null || getClass() != obj.getClass()) return false;
77  
78          final SxcFilter other = (SxcFilter) obj;
79          return equal(pattern, other.pattern);
80      }
81  
82      public int hashCode()
83      {
84          return hash(new Object[]{this.getClass(), pattern});
85      }
86  }
87  
88