View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.scripting.filter;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.routing.filter.Filter;
12  import org.mule.module.scripting.component.Scriptable;
13  import org.mule.processor.AbstractFilteringMessageProcessor;
14  
15  import javax.script.Bindings;
16  
17  public class ScriptFilter extends AbstractFilteringMessageProcessor implements Filter
18  {
19  
20      private Scriptable script;
21      
22      private String name;
23      
24      @Override
25      protected boolean accept(MuleEvent event)
26      {
27          return this.accept(event.getMessage());
28      }
29      
30      public boolean accept(MuleMessage message)
31      {
32          Bindings bindings = script.getScriptEngine().createBindings();
33          script.populateBindings(bindings, message);
34          try
35          {
36              return (Boolean) script.runScript(bindings);
37          }
38          catch (Throwable e)
39          {
40              return false;
41          }
42      }
43  
44      public Scriptable getScript()
45      {
46          return script;
47      }
48  
49      public void setScript(Scriptable script)
50      {
51          this.script = script;
52      }
53  
54      public String getName()
55      {
56          return name;
57      }
58  
59      public void setName(String name)
60      {
61          this.name = name;
62      }
63  }
64  
65