1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
23 | |
public class ScriptConfigurationBuilder extends AbstractResourceConfigurationBuilder |
24 | |
{ |
25 | |
public static final String SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine"; |
26 | |
|
27 | 26 | private Scriptable scriptComponent = new Scriptable(); |
28 | |
|
29 | |
public ScriptConfigurationBuilder(String configResource) throws MuleException |
30 | |
{ |
31 | 0 | this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResource); |
32 | 0 | } |
33 | |
|
34 | |
public ScriptConfigurationBuilder(String[] configResources) throws MuleException |
35 | |
{ |
36 | 0 | this(System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY), configResources); |
37 | 0 | } |
38 | |
|
39 | |
public ScriptConfigurationBuilder(String scriptEngineName, String configResource) throws MuleException |
40 | |
{ |
41 | 26 | super(configResource); |
42 | 26 | if (scriptEngineName == null) |
43 | |
{ |
44 | |
|
45 | 0 | logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage()); |
46 | |
} |
47 | 26 | scriptComponent.setScriptEngineName(scriptEngineName); |
48 | 26 | } |
49 | |
|
50 | |
public ScriptConfigurationBuilder(String scriptEngineName, String[] configResources) throws MuleException |
51 | |
{ |
52 | 0 | super(configResources); |
53 | 0 | if (scriptEngineName == null) |
54 | |
{ |
55 | |
|
56 | 0 | logger.warn(BuildersMessages.systemPropertyNotSet(SCRIPT_ENGINE_NAME_PROPERTY).getMessage()); |
57 | |
} |
58 | 0 | scriptComponent.setScriptEngineName(scriptEngineName); |
59 | 0 | } |
60 | |
|
61 | |
protected void doConfigure(MuleContext muleContext) throws Exception |
62 | |
{ |
63 | 52 | for (int i = 0; i < configResources.length; i++) |
64 | |
{ |
65 | 26 | ConfigResource configResource = configResources[i]; |
66 | 26 | scriptComponent.setScriptFile(configResource.getResourceName()); |
67 | 26 | scriptComponent.initialise(); |
68 | |
|
69 | 26 | Bindings bindings = scriptComponent.getScriptEngine().createBindings(); |
70 | 26 | scriptComponent.populateDefaultBindings(bindings); |
71 | 26 | scriptComponent.runScript(bindings); |
72 | |
} |
73 | 26 | } |
74 | |
} |