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