Coverage Report - org.mule.module.scripting.component.ScriptComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptComponent
0%
0/36
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: ScriptComponent.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.api.MuleEvent;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.component.InterfaceBinding;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.component.AbstractComponent;
 18  
 import org.mule.component.BindingInvocationHandler;
 19  
 import org.mule.util.ClassUtils;
 20  
 
 21  
 import java.lang.reflect.Proxy;
 22  
 import java.util.ArrayList;
 23  
 import java.util.HashMap;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.script.Bindings;
 29  
 
 30  
 /**
 31  
  * A Script service backed by a JSR-223 compliant script engine such as
 32  
  * Groovy, JavaScript, or Rhino.
 33  
  */
 34  0
 public class ScriptComponent extends AbstractComponent
 35  
 {
 36  0
     protected List<InterfaceBinding> bindings = new ArrayList<InterfaceBinding>();
 37  
 
 38  
     private Scriptable script;
 39  
 
 40  
     private Map<String, Object> proxies;
 41  
 
 42  
     @Override
 43  
     protected void doInitialise() throws InitialisationException
 44  
     {
 45  0
         script.setMuleContext(muleContext);
 46  0
         super.doInitialise();
 47  
         try
 48  
         {
 49  0
             configureComponentBindings();
 50  
         }
 51  0
         catch (MuleException e)
 52  
         {
 53  0
             throw new InitialisationException(e, this);
 54  0
         }
 55  0
     }
 56  
 
 57  
     @Override
 58  
     protected Object doInvoke(MuleEvent event) throws Exception
 59  
     {
 60  
         // Set up initial script variables.
 61  0
         Bindings bindings = script.getScriptEngine().createBindings();
 62  0
         if (proxies.size() > 0)
 63  
         {
 64  0
             bindings.putAll(proxies);
 65  
         }
 66  0
         script.populateBindings(bindings, event);
 67  
         try
 68  
         {
 69  0
             return script.runScript(bindings);
 70  
         }
 71  0
         catch (Exception e)
 72  
         {
 73  
             // leave this catch block in place to help debug classloading issues
 74  0
             throw e;
 75  
         }
 76  
     }
 77  
 
 78  
     public Scriptable getScript()
 79  
     {
 80  0
         return script;
 81  
     }
 82  
 
 83  
     public void setScript(Scriptable script)
 84  
     {
 85  0
         this.script = script;
 86  0
     }
 87  
 
 88  
     public List<InterfaceBinding> getInterfaceBindings()
 89  
     {
 90  0
         return bindings;
 91  
     }
 92  
 
 93  
     public void setInterfaceBindings(List<InterfaceBinding> bindingCollection)
 94  
     {
 95  0
         this.bindings = bindingCollection;
 96  0
     }
 97  
 
 98  
     protected void configureComponentBindings() throws MuleException
 99  
     {
 100  0
         proxies = new HashMap<String, Object>();
 101  
         // Initialise the nested router and bind the endpoints to the methods using a
 102  
         // Proxy
 103  0
         if (bindings != null && bindings.size() > 0)
 104  
         {
 105  0
             for (Iterator<?> it = bindings.iterator(); it.hasNext();)
 106  
             {
 107  0
                 InterfaceBinding interfaceBinding = (InterfaceBinding) it.next();
 108  0
                 String bindingName = ClassUtils.getSimpleName(interfaceBinding.getInterface());
 109  0
                 if (proxies.containsKey(bindingName))
 110  
                 {
 111  0
                     Object proxy = proxies.get(bindingName);
 112  0
                     BindingInvocationHandler handler = (BindingInvocationHandler) Proxy.getInvocationHandler(proxy);
 113  0
                     handler.addRouterForInterface(interfaceBinding);
 114  0
                 }
 115  
                 else
 116  
                 {
 117  0
                     Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), 
 118  
                         new Class[]{interfaceBinding.getInterface()}, 
 119  
                         new BindingInvocationHandler(interfaceBinding));
 120  0
                     proxies.put(bindingName, proxy);
 121  
                 }
 122  0
             }
 123  
         }
 124  0
     }
 125  
 }