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  
  * $Id: ScriptConfigurationBuilder.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.builders;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.config.ConfigResource;
 16  
 import org.mule.config.builders.AbstractResourceConfigurationBuilder;
 17  
 import org.mule.config.builders.i18n.BuildersMessages;
 18  
 import org.mule.module.scripting.component.Scriptable;
 19  
 
 20  
 import javax.script.Bindings;
 21  
 
 22  
 /** Configures Mule from one or more script files. */
 23  
 public class ScriptConfigurationBuilder extends AbstractResourceConfigurationBuilder
 24  
 {
 25  
     public static final String SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine";
 26  
 
 27  
     private Scriptable scriptComponent;
 28  
 
 29  
     private String scriptEngineName;
 30  
 
 31  0
     protected MuleContext muleContext = null;
 32  
 
 33  
     public ScriptConfigurationBuilder(String configResource) throws MuleException
 34  
     {
 35  0
         this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResource);
 36  0
     }
 37  
 
 38  
     public ScriptConfigurationBuilder(String[] configResources) throws MuleException
 39  
     {
 40  0
         this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResources);
 41  0
     }
 42  
 
 43  
     public ScriptConfigurationBuilder(String scriptEngineName, String configResource) throws MuleException
 44  
     {
 45  0
         super(configResource);
 46  0
         if (scriptEngineName == null)
 47  
         {
 48  
             // we can guess engine by file extension
 49  0
             logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
 50  
         }
 51  0
         this.scriptEngineName = scriptEngineName;
 52  0
     }
 53  
 
 54  
     public ScriptConfigurationBuilder(String scriptEngineName, String[] configResources) throws MuleException
 55  
     {
 56  0
         super(configResources);
 57  0
         if (scriptEngineName == null)
 58  
         {
 59  
             // we can guess engine by file extension
 60  0
             logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
 61  
         }
 62  0
         this.scriptEngineName = scriptEngineName;
 63  0
     }
 64  
 
 65  
     protected void doConfigure(MuleContext muleContext) throws Exception
 66  
     {
 67  0
         this.muleContext = muleContext;
 68  
 
 69  0
         scriptComponent = new Scriptable(muleContext);
 70  0
         scriptComponent.setScriptEngineName(scriptEngineName);
 71  
             
 72  0
         for (int i = 0; i < configResources.length; i++)
 73  
         {
 74  0
             ConfigResource configResource = configResources[i];
 75  0
             scriptComponent.setScriptFile(configResource.getResourceName());
 76  0
             scriptComponent.initialise();
 77  
             // Set up initial script variables.
 78  0
             Bindings bindings = scriptComponent.getScriptEngine().createBindings();
 79  0
             scriptComponent.populateDefaultBindings(bindings);
 80  0
             scriptComponent.runScript(bindings);
 81  
         }
 82  0
     }
 83  
 
 84  
     protected void populateBindings(Bindings bindings)
 85  
     {
 86  0
         bindings.put("muleContext", muleContext);
 87  0
     }
 88  
 
 89  
 }