View Javadoc

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