View Javadoc

1   /*
2    * $Id: ScriptFilter.java 20783 2010-12-16 15:07:56Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.scripting.filter;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.routing.filter.Filter;
16  import org.mule.module.scripting.component.Scriptable;
17  import org.mule.processor.AbstractFilteringMessageProcessor;
18  
19  import javax.script.Bindings;
20  
21  public class ScriptFilter extends AbstractFilteringMessageProcessor implements Filter
22  {
23  
24      private Scriptable script;
25      
26      private String name;
27      
28      @Override
29      protected boolean accept(MuleEvent event)
30      {
31          return this.accept(event.getMessage());
32      }
33      
34      public boolean accept(MuleMessage message)
35      {
36          Bindings bindings = script.getScriptEngine().createBindings();
37          script.populateBindings(bindings, message);
38          try
39          {
40              return (Boolean) script.runScript(bindings);
41          }
42          catch (Throwable e)
43          {
44              return false;
45          }
46      }
47  
48      public Scriptable getScript()
49      {
50          return script;
51      }
52  
53      public void setScript(Scriptable script)
54      {
55          this.script = script;
56      }
57  
58      public String getName()
59      {
60          return name;
61      }
62  
63      public void setName(String name)
64      {
65          this.name = name;
66      }
67  }
68  
69