View Javadoc

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      private Scriptable scriptComponent = new Scriptable();
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          scriptComponent.setScriptEngineName(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          scriptComponent.setScriptEngineName(scriptEngineName);
59      }
60  
61      protected void doConfigure(MuleContext muleContext) throws Exception
62      {
63          for (int i = 0; i < configResources.length; i++)
64          {
65              ConfigResource configResource = configResources[i];
66              scriptComponent.setScriptFile(configResource.getResourceName());
67              scriptComponent.initialise();
68              // Set up initial script variables.
69              Bindings bindings = scriptComponent.getScriptEngine().createBindings();
70              scriptComponent.populateDefaultBindings(bindings);
71              scriptComponent.runScript(bindings);
72          }
73      }
74  }