View Javadoc

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      protected UMOManager manager = null;
38      protected QuickConfigurationBuilder builder = null;
39      protected boolean initialised = false;
40  
41      public ScriptConfigurationBuilder()
42      {
43          builder = new QuickConfigurationBuilder(false);
44          manager = MuleManager.getInstance();
45          String scriptName = System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY);
46          if (scriptName == null)
47          {
48              throw new IllegalArgumentException(
49                  BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
50          }
51          else
52          {
53              this.setScriptEngineName(scriptName);
54          }
55      }
56  
57      public ScriptConfigurationBuilder(String scriptEngineName)
58      {
59          builder = new QuickConfigurationBuilder(false);
60          manager = MuleManager.getInstance();
61          this.setScriptEngineName(scriptEngineName);
62      }
63  
64      public UMOManager configure(String configResources) throws ConfigurationException
65      {
66          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              ReaderResource[] readers = ReaderResource.parseResources(configResources);
83  
84              // Load startup properties if any.
85              if (startupPropertiesFile != null)
86              {
87                  return configure(readers, PropertiesUtils.loadProperties(startupPropertiesFile, getClass()));
88              }
89              else
90              {
91                  return configure(readers, null);
92              }
93          }
94          catch (IOException e)
95          {
96              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         if (startupProperties != null)
112         {
113             ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
114         }
115 
116         try
117         {
118             for (int i = 0; i < configResources.length; i++)
119             {
120                 ReaderResource configResource = configResources[i];
121                 setScriptFile(configResource.getDescription());
122                 initialise();
123                 Bindings ns = getScriptEngine().createBindings();
124                 populateBindings(ns);
125                 CompiledScript script = compileScript(configResource.getReader());
126                 script.eval(ns);
127             }
128 
129             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
130                 .equalsIgnoreCase("true"))
131             {
132                 if (!manager.isStarted())
133                 {
134                     manager.start();
135                 }
136             }
137 
138         }
139         catch (Exception e)
140         {
141             throw new ConfigurationException(e);
142         }
143         return manager;
144     }
145 
146     protected void populateBindings(Bindings bindings)
147     {
148         bindings.put("manager", manager);
149         bindings.put("builder", builder);
150     }
151 
152     public boolean isConfigured()
153     {
154         return manager != null;
155     }
156 
157 }