View Javadoc

1   /*
2    * $Id: ScriptComponent.java 10430 2008-01-21 16:28:22Z dirk.olmes $
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.components.script.jsr223;
12  
13  import org.mule.umo.UMOEventContext;
14  import org.mule.umo.lifecycle.Callable;
15  
16  import javax.script.Bindings;
17  
18  
19  /**
20   * A JSR 223 Script component. Allows any JSR 223 compliant script engines such as
21   * JavaScript, Groovy or Rhino to be embedded as Mule components.
22   */
23  public class ScriptComponent extends AbstractScriptComponent implements Callable
24  {
25      
26      protected void populateBindings(Bindings namespace, UMOEventContext context)
27      {
28          super.populateBindings(namespace, context);
29          namespace.put("result", new Object());
30      }
31  
32      public Object onCall(UMOEventContext eventContext) throws Exception
33      {
34          Bindings bindings = this.getBindings();
35          populateBindings(bindings, eventContext);
36          Object result = runScript(bindings);
37          if (result == null)
38          {
39              result = bindings.get("result");
40          }
41          return result;
42      }
43  
44  }