Coverage Report - org.mule.config.builders.ScriptConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ScriptConfigurationBuilder
0%
0/44
0%
0/7
2.857
 
 1  
 /*
 2  
  * $Id: ScriptConfigurationBuilder.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.config.builders;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.components.script.jsr223.Scriptable;
 15  
 import org.mule.config.ConfigurationBuilder;
 16  
 import org.mule.config.ConfigurationException;
 17  
 import org.mule.config.MuleProperties;
 18  
 import org.mule.config.ReaderResource;
 19  
 import org.mule.config.builders.i18n.BuildersMessages;
 20  
 import org.mule.umo.manager.UMOManager;
 21  
 import org.mule.util.PropertiesUtils;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.util.Properties;
 25  
 
 26  
 import javax.script.Bindings;
 27  
 import javax.script.CompiledScript;
 28  
 
 29  
 /**
 30  
  * Configures a MuleManager from one or more script files.
 31  
  */
 32  
 public class ScriptConfigurationBuilder extends Scriptable implements ConfigurationBuilder
 33  
 {
 34  
 
 35  
     public static final String SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine";
 36  
 
 37  0
     protected UMOManager manager = null;
 38  0
     protected QuickConfigurationBuilder builder = null;
 39  0
     protected boolean initialised = false;
 40  
 
 41  
     public ScriptConfigurationBuilder()
 42  0
     {
 43  0
         builder = new QuickConfigurationBuilder(false);
 44  0
         manager = MuleManager.getInstance();
 45  0
         String scriptName = System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY);
 46  0
         if (scriptName == null)
 47  
         {
 48  0
             throw new IllegalArgumentException(
 49  
                 BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
 50  
         }
 51  
         else
 52  
         {
 53  0
             this.setScriptEngineName(scriptName);
 54  
         }
 55  0
     }
 56  
 
 57  
     public ScriptConfigurationBuilder(String scriptEngineName)
 58  0
     {
 59  0
         builder = new QuickConfigurationBuilder(false);
 60  0
         manager = MuleManager.getInstance();
 61  0
         this.setScriptEngineName(scriptEngineName);
 62  0
     }
 63  
 
 64  
     public UMOManager configure(String configResources) throws ConfigurationException
 65  
     {
 66  0
         return configure(configResources, null);
 67  
     }
 68  
 
 69  
     /**
 70  
      * Will configure a UMOManager based on the configuration file(s) provided.
 71  
      * 
 72  
      * @param configResources a comma separated list of configuration files to load,
 73  
      *            this should be accessible on the classpath or filesystem
 74  
      * @return A configured UMOManager
 75  
      * @throws org.mule.config.ConfigurationException
 76  
      */
 77  
     public UMOManager configure(String configResources, String startupPropertiesFile)
 78  
         throws ConfigurationException
 79  
     {
 80  
         try
 81  
         {
 82  0
             ReaderResource[] readers = ReaderResource.parseResources(configResources);
 83  
 
 84  
             // Load startup properties if any.
 85  0
             if (startupPropertiesFile != null)
 86  
             {
 87  0
                 return configure(readers, PropertiesUtils.loadProperties(startupPropertiesFile, getClass()));
 88  
             }
 89  
             else
 90  
             {
 91  0
                 return configure(readers, null);
 92  
             }
 93  
         }
 94  0
         catch (IOException e)
 95  
         {
 96  0
             throw new ConfigurationException(e);
 97  
         }
 98  
     }
 99  
 
 100  
     /**
 101  
      * Will configure a UMOManager based on the configurations made available through
 102  
      * Readers
 103  
      * 
 104  
      * @param configResources an array of Readers
 105  
      * @return A configured UMOManager
 106  
      * @throws org.mule.config.ConfigurationException
 107  
      */
 108  
     public UMOManager configure(ReaderResource[] configResources, Properties startupProperties)
 109  
         throws ConfigurationException
 110  
     {
 111  0
         if (startupProperties != null)
 112  
         {
 113  0
             ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
 114  
         }
 115  
 
 116  
         try
 117  
         {
 118  0
             for (int i = 0; i < configResources.length; i++)
 119  
             {
 120  0
                 ReaderResource configResource = configResources[i];
 121  0
                 setScriptFile(configResource.getDescription());
 122  0
                 initialise();
 123  0
                 Bindings ns = getScriptEngine().createBindings();
 124  0
                 populateBindings(ns);
 125  0
                 CompiledScript script = compileScript(configResource.getReader());
 126  0
                 script.eval(ns);
 127  
             }
 128  
 
 129  0
             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
 130  
                 .equalsIgnoreCase("true"))
 131  
             {
 132  0
                 if (!manager.isStarted())
 133  
                 {
 134  0
                     manager.start();
 135  
                 }
 136  
             }
 137  
 
 138  
         }
 139  0
         catch (Exception e)
 140  
         {
 141  0
             throw new ConfigurationException(e);
 142  0
         }
 143  0
         return manager;
 144  
     }
 145  
 
 146  
     protected void populateBindings(Bindings bindings)
 147  
     {
 148  0
         bindings.put("manager", manager);
 149  0
         bindings.put("builder", builder);
 150  0
     }
 151  
 
 152  
     public boolean isConfigured()
 153  
     {
 154  0
         return manager != null;
 155  
     }
 156  
 
 157  
 }