Coverage Report - org.mule.module.scripting.builders.ScriptConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptConfigurationBuilder
57%
13/23
50%
3/6
1.6
 
 1  
 /*
 2  
  * $Id: ScriptConfigurationBuilder.java 11879 2008-06-01 22:45:57Z 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.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  26
     private Scriptable scriptComponent = new Scriptable();
 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  26
         super(configResource);
 42  26
         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  26
         scriptComponent.setScriptEngineName(scriptEngineName);
 48  26
     }
 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
         scriptComponent.setScriptEngineName(scriptEngineName);
 59  0
     }
 60  
 
 61  
     protected void doConfigure(MuleContext muleContext) throws Exception
 62  
     {
 63  52
         for (int i = 0; i < configResources.length; i++)
 64  
         {
 65  26
             ConfigResource configResource = configResources[i];
 66  26
             scriptComponent.setScriptFile(configResource.getResourceName());
 67  26
             scriptComponent.initialise();
 68  
             // Set up initial script variables.
 69  26
             Bindings bindings = scriptComponent.getScriptEngine().createBindings();
 70  26
             scriptComponent.populateDefaultBindings(bindings);
 71  26
             scriptComponent.runScript(bindings);
 72  
         }
 73  26
     }
 74  
 }