View Javadoc
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      protected MuleContext muleContext = null;
28  
29      public ScriptConfigurationBuilder(String configResource) throws MuleException
30      {
31          this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResource);
32      }
33  
34      public ScriptConfigurationBuilder(String[] configResources) throws MuleException
35      {
36          this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResources);
37      }
38  
39      public ScriptConfigurationBuilder(String scriptEngineName, String configResource) throws MuleException
40      {
41          super(configResource);
42          if (scriptEngineName == null)
43          {
44              // we can guess engine by file extension
45              logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
46          }
47          this.scriptEngineName = scriptEngineName;
48      }
49  
50      public ScriptConfigurationBuilder(String scriptEngineName, String[] configResources) throws MuleException
51      {
52          super(configResources);
53          if (scriptEngineName == null)
54          {
55              // we can guess engine by file extension
56              logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
57          }
58          this.scriptEngineName = scriptEngineName;
59      }
60  
61      protected void doConfigure(MuleContext muleContext) throws Exception
62      {
63          this.muleContext = muleContext;
64  
65          scriptComponent = new Scriptable(muleContext);
66          scriptComponent.setScriptEngineName(scriptEngineName);
67              
68          for (int i = 0; i < configResources.length; i++)
69          {
70              ConfigResource configResource = configResources[i];
71              scriptComponent.setScriptFile(configResource.getResourceName());
72              scriptComponent.initialise();
73              // Set up initial script variables.
74              Bindings bindings = scriptComponent.getScriptEngine().createBindings();
75              scriptComponent.populateDefaultBindings(bindings);
76              scriptComponent.runScript(bindings);
77          }
78      }
79  
80      protected void populateBindings(Bindings bindings)
81      {
82          bindings.put("muleContext", muleContext);
83      }
84  
85  }