Coverage Report - org.mule.module.scripting.builders.ScriptConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptConfigurationBuilder
0%
0/28
0%
0/6
1.5
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.scripting.builders;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.config.ConfigResource;
 12  
 import org.mule.config.builders.AbstractResourceConfigurationBuilder;
 13  
 import org.mule.config.builders.i18n.BuildersMessages;
 14  
 import org.mule.module.scripting.component.Scriptable;
 15  
 
 16  
 import javax.script.Bindings;
 17  
 
 18  
 /** Configures Mule from one or more script files. */
 19  
 public class ScriptConfigurationBuilder extends AbstractResourceConfigurationBuilder
 20  
 {
 21  
     public static final String SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine";
 22  
 
 23  
     private Scriptable scriptComponent;
 24  
 
 25  
     private String scriptEngineName;
 26  
 
 27  0
     protected MuleContext muleContext = null;
 28  
 
 29  
     public ScriptConfigurationBuilder(String configResource) throws MuleException
 30  
     {
 31  0
         this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResource);
 32  0
     }
 33  
 
 34  
     public ScriptConfigurationBuilder(String[] configResources) throws MuleException
 35  
     {
 36  0
         this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResources);
 37  0
     }
 38  
 
 39  
     public ScriptConfigurationBuilder(String scriptEngineName, String configResource) throws MuleException
 40  
     {
 41  0
         super(configResource);
 42  0
         if (scriptEngineName == null)
 43  
         {
 44  
             // we can guess engine by file extension
 45  0
             logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
 46  
         }
 47  0
         this.scriptEngineName = scriptEngineName;
 48  0
     }
 49  
 
 50  
     public ScriptConfigurationBuilder(String scriptEngineName, String[] configResources) throws MuleException
 51  
     {
 52  0
         super(configResources);
 53  0
         if (scriptEngineName == null)
 54  
         {
 55  
             // we can guess engine by file extension
 56  0
             logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
 57  
         }
 58  0
         this.scriptEngineName = scriptEngineName;
 59  0
     }
 60  
 
 61  
     protected void doConfigure(MuleContext muleContext) throws Exception
 62  
     {
 63  0
         this.muleContext = muleContext;
 64  
 
 65  0
         scriptComponent = new Scriptable(muleContext);
 66  0
         scriptComponent.setScriptEngineName(scriptEngineName);
 67  
             
 68  0
         for (int i = 0; i < configResources.length; i++)
 69  
         {
 70  0
             ConfigResource configResource = configResources[i];
 71  0
             scriptComponent.setScriptFile(configResource.getResourceName());
 72  0
             scriptComponent.initialise();
 73  
             // Set up initial script variables.
 74  0
             Bindings bindings = scriptComponent.getScriptEngine().createBindings();
 75  0
             scriptComponent.populateDefaultBindings(bindings);
 76  0
             scriptComponent.runScript(bindings);
 77  
         }
 78  0
     }
 79  
 
 80  
     protected void populateBindings(Bindings bindings)
 81  
     {
 82  0
         bindings.put("muleContext", muleContext);
 83  0
     }
 84  
 
 85  
 }