View Javadoc

1   /*
2    * $Id: ScriptComponent.java 12075 2008-06-17 11:12:10Z rossmason $
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.component;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.routing.NestedRouter;
19  import org.mule.api.routing.NestedRouterCollection;
20  import org.mule.component.AbstractComponent;
21  import org.mule.routing.nested.DefaultNestedRouterCollection;
22  import org.mule.routing.nested.NestedInvocationHandler;
23  import org.mule.transformer.TransformerTemplate;
24  import org.mule.transport.NullPayload;
25  import org.mule.util.ClassUtils;
26  
27  import java.lang.reflect.Proxy;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  import javax.script.Bindings;
34  
35  /**
36   * A Script service backed by a JSR-223 compliant script engine such as
37   * Groovy, JavaScript, or Rhino.
38   */
39  public class ScriptComponent extends AbstractComponent
40  {
41  
42      protected NestedRouterCollection nestedRouter = new DefaultNestedRouterCollection();
43  
44      private Scriptable script;
45  
46      private Map proxies;
47  
48      //@Override
49      protected void doInitialise() throws InitialisationException
50      {
51          super.doInitialise();
52          try
53          {
54              configureComponentBindings();
55          }
56          catch (MuleException e)
57          {
58              throw new InitialisationException(e, this);
59          }
60  
61      }
62  
63      protected MuleMessage doOnCall(MuleEvent event) throws Exception
64      {
65          // Set up initial script variables.
66          Bindings bindings = script.getScriptEngine().createBindings();
67          if (proxies.size() > 0)
68          {
69              bindings.putAll(proxies);
70          }
71          script.populateBindings(bindings, event);
72          Object result = script.runScript(bindings);
73  
74          if (result != null)
75          {
76              if (result instanceof MuleMessage)
77              {
78                  return (MuleMessage) result;
79              }
80              else
81              {
82                  event.getMessage().applyTransformers(Collections.singletonList(new TransformerTemplate(
83                          new TransformerTemplate.OverwitePayloadCallback(result))));
84                  return event.getMessage();
85              }
86          }
87          else
88          {
89              return new DefaultMuleMessage(NullPayload.getInstance());
90          }
91      }
92  
93  
94      public Scriptable getScript()
95      {
96          return script;
97      }
98  
99      public void setScript(Scriptable script)
100     {
101         this.script = script;
102     }
103 
104     public NestedRouterCollection getNestedRouter()
105     {
106         return nestedRouter;
107     }
108 
109     public void setNestedRouter(NestedRouterCollection nestedRouter)
110     {
111         this.nestedRouter = nestedRouter;
112     }
113 
114     protected void configureComponentBindings() throws MuleException
115     {
116         proxies = new HashMap();
117         // Initialise the nested router and bind the endpoints to the methods using a
118         // Proxy
119         if (nestedRouter != null && nestedRouter.getRouters().size() > 0)
120         {
121             for (Iterator it = nestedRouter.getRouters().iterator(); it.hasNext();)
122             {
123                 NestedRouter nestedRouter = (NestedRouter) it.next();
124                 String bindingName = ClassUtils.getSimpleName(nestedRouter.getInterface());
125                 if (proxies.containsKey(bindingName))
126                 {
127                     Object proxy = proxies.get(bindingName);
128                     NestedInvocationHandler handler = (NestedInvocationHandler) Proxy.getInvocationHandler(proxy);
129                     handler.addRouterForInterface(nestedRouter);
130                 }
131                 else
132                 {
133                     Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{nestedRouter.getInterface()},
134                             new NestedInvocationHandler(nestedRouter));
135                     proxies.put(bindingName, proxy);
136                 }
137 
138             }
139         }
140     }
141 }