KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > script > jsr223 > ScriptComponent


1 /*
2  * $Id: ScriptComponent.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 javax.script.Namespace;
14
15 import org.mule.MuleManager;
16 import org.mule.umo.UMOEventContext;
17 import org.mule.umo.lifecycle.Callable;
18 import org.mule.umo.lifecycle.InitialisationException;
19 import org.mule.umo.lifecycle.RecoverableException;
20 import org.mule.util.MuleLogger;
21
22 /**
23  * A JSR 223 Script component. Allows any JSR 223 compliant script engines such as
24  * JavaScript, Groovy or Rhino to be embedded as Mule components.
25  */

26 public class ScriptComponent extends Scriptable implements Callable
27 {
28     private Namespace namespace;
29
30     public void initialise() throws InitialisationException, RecoverableException
31     {
32         super.initialise();
33         namespace = getScriptEngine().createNamespace();
34     }
35
36     public Object JavaDoc onCall(UMOEventContext eventContext) throws Exception JavaDoc
37     {
38         populateNamespace(namespace, eventContext);
39         Object JavaDoc result = runScript(namespace);
40         if (result == null)
41         {
42             result = namespace.get("result");
43         }
44         return result;
45     }
46
47     protected void populateNamespace(Namespace namespace, UMOEventContext context)
48     {
49         namespace.put("eventContext", context);
50         namespace.put("managementContext", MuleManager.getInstance());
51         namespace.put("message", context.getMessage());
52         namespace.put("descriptor", context.getComponentDescriptor());
53         namespace.put("componentNamespace", this.namespace);
54         namespace.put("log", new MuleLogger(logger));
55         namespace.put("result", new Object JavaDoc());
56     }
57
58     public Namespace getNamespace()
59     {
60         return namespace;
61     }
62
63 }
64
Popular Tags