1 10 11 package org.mule.config.builders; 12 13 import java.io.IOException ; 14 import java.util.Properties ; 15 16 import javax.script.CompiledScript; 17 import javax.script.Namespace; 18 19 import org.mule.MuleManager; 20 import org.mule.components.script.jsr223.Scriptable; 21 import org.mule.config.ConfigurationBuilder; 22 import org.mule.config.ConfigurationException; 23 import org.mule.config.MuleProperties; 24 import org.mule.config.ReaderResource; 25 import org.mule.config.i18n.Message; 26 import org.mule.config.i18n.Messages; 27 import org.mule.umo.manager.UMOManager; 28 import org.mule.util.PropertiesUtils; 29 30 33 public class ScriptConfigurationBuilder extends Scriptable implements ConfigurationBuilder 34 { 35 36 public static final String SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine"; 37 38 protected UMOManager manager = null; 39 protected QuickConfigurationBuilder builder = null; 40 protected boolean initialised = false; 41 42 public ScriptConfigurationBuilder() 43 { 44 builder = new QuickConfigurationBuilder(false); 45 manager = MuleManager.getInstance(); 46 String scriptName = System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY); 47 if (scriptName == null) 48 { 49 throw new NullPointerException (new Message(Messages.SYSTEM_PROPERTY_X_NOT_SET, 50 SCRIPT_ENGINE_NAME_PROPERTY).getMessage()); 51 } 52 else 53 { 54 this.setScriptEngineName(scriptName); 55 } 56 } 57 58 public ScriptConfigurationBuilder(String scriptEngineName) 59 { 60 builder = new QuickConfigurationBuilder(false); 61 manager = MuleManager.getInstance(); 62 this.setScriptEngineName(scriptEngineName); 63 } 64 65 public UMOManager configure(String configResources) throws ConfigurationException 66 { 67 return configure(configResources, null); 68 } 69 70 78 public UMOManager configure(String configResources, String startupPropertiesFile) 79 throws ConfigurationException 80 { 81 try 90 { 91 ReaderResource[] readers = ReaderResource.parseResources(configResources); 92 93 if (startupPropertiesFile != null) 95 { 96 return configure(readers, PropertiesUtils.loadProperties(startupPropertiesFile, getClass())); 97 } 98 else 99 { 100 return configure(readers, null); 101 } 102 } 103 catch (IOException e) 104 { 105 throw new ConfigurationException(e); 106 } 107 } 108 109 113 public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException 114 { 115 return configure(configResources, null); 116 } 117 118 126 public UMOManager configure(ReaderResource[] configResources, Properties startupProperties) 127 throws ConfigurationException 128 { 129 if (startupProperties != null) 130 { 131 ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties); 132 } 133 134 try 135 { 136 for (int i = 0; i < configResources.length; i++) 137 { 138 ReaderResource configResource = configResources[i]; 139 setScriptFile(configResource.getDescription()); 140 initialise(); 141 Namespace ns = getScriptEngine().createNamespace(); 142 populateNamespace(ns); 143 CompiledScript script = compileScript(configResource.getReader()); 144 script.eval(ns); 145 } 146 147 if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true") 148 .equalsIgnoreCase("true")) 149 { 150 if (!manager.isStarted()) 151 { 152 manager.start(); 153 } 154 } 155 156 } 157 catch (Exception e) 158 { 159 throw new ConfigurationException(e); 160 } 161 return manager; 162 } 163 164 protected void populateNamespace(Namespace namespace) 165 { 166 namespace.put("manager", manager); 167 namespace.put("builder", builder); 168 169 } 170 171 public boolean isConfigured() 172 { 173 return manager != null; 174 } 175 } 176 | Popular Tags |