1
2
3
4
5
6
7
8
9
10
11 package org.mule.config;
12
13 import org.mule.MuleServer;
14 import org.mule.api.MuleContext;
15 import org.mule.api.config.MuleConfiguration;
16 import org.mule.api.config.MuleProperties;
17 import org.mule.api.lifecycle.FatalException;
18 import org.mule.api.lifecycle.Initialisable;
19 import org.mule.api.lifecycle.Startable;
20 import org.mule.config.i18n.CoreMessages;
21 import org.mule.util.FileUtils;
22 import org.mule.util.NumberUtils;
23 import org.mule.util.StringUtils;
24 import org.mule.util.UUID;
25
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.nio.charset.Charset;
29
30 import javax.xml.parsers.SAXParserFactory;
31
32 import org.apache.commons.lang.BooleanUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40 public class DefaultMuleConfiguration implements MuleConfiguration
41 {
42 private boolean synchronous = false;
43
44
45
46
47
48 private String systemModelType = "seda";
49
50 private String encoding = "UTF-8";
51
52
53
54
55
56 private int synchronousEventTimeout = 10000;
57
58
59
60
61
62 private int defaultTransactionTimeout = 30000;
63
64
65
66
67
68
69 private boolean remoteSync = false;
70
71
72 private String workingDirectory = "./.mule";
73
74
75
76
77
78 private boolean clientMode = false;
79
80
81 private String id;
82
83
84 private String clusterId;
85
86
87 private String domainId;
88
89
90
91 private boolean cacheMessageAsBytes = true;
92
93 private boolean cacheMessageOriginalPayload = true;
94
95 private boolean enableStreaming = true;
96
97 private boolean autoWrapMessageAwareTransform = true;
98
99 protected transient Log logger = LogFactory.getLog(DefaultMuleConfiguration.class);
100
101 public DefaultMuleConfiguration()
102 {
103 super();
104
105
106 applySystemProperties();
107
108 if (id == null)
109 {
110 id = UUID.getUUID();
111 }
112
113 if (clusterId == null)
114 {
115 clusterId = CoreMessages.notClustered().getMessage();
116 }
117
118 if (domainId == null)
119 {
120 try
121 {
122 domainId = InetAddress.getLocalHost().getHostName();
123 }
124 catch (UnknownHostException e)
125 {
126 logger.warn(e);
127 domainId = "org.mule";
128 }
129 }
130
131 try
132 {
133 validateEncoding();
134 validateXML();
135 }
136 catch (FatalException e)
137 {
138 throw new RuntimeException(e);
139 }
140 }
141
142
143
144
145 protected void applySystemProperties()
146 {
147 String p;
148
149 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "encoding");
150 if (p != null)
151 {
152 encoding = p;
153 }
154 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous");
155 if (p != null)
156 {
157 synchronous = BooleanUtils.toBoolean(p);
158 }
159 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "systemModelType");
160 if (p != null)
161 {
162 systemModelType = p;
163 }
164 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "timeout.synchronous");
165 if (p != null)
166 {
167 synchronousEventTimeout = NumberUtils.toInt(p);
168 }
169 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "timeout.transaction");
170 if (p != null)
171 {
172 defaultTransactionTimeout = NumberUtils.toInt(p);
173 }
174 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "remoteSync");
175 if (p != null)
176 {
177 remoteSync = BooleanUtils.toBoolean(p);
178 }
179 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "workingDirectory");
180 if (p != null)
181 {
182 workingDirectory = p;
183 }
184 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "clientMode");
185 if (p != null)
186 {
187 clientMode = BooleanUtils.toBoolean(p);
188 }
189 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "serverId");
190 if (p != null)
191 {
192 id = p;
193 }
194 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "clusterId");
195 if (p != null)
196 {
197 clusterId = p;
198 }
199 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "domainId");
200 if (p != null)
201 {
202 domainId = p;
203 }
204 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "message.cacheBytes");
205 if (p != null)
206 {
207 cacheMessageAsBytes = BooleanUtils.toBoolean(p);
208 }
209 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal");
210 if (p != null)
211 {
212 cacheMessageOriginalPayload = BooleanUtils.toBoolean(p);
213 }
214 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "streaming.enable");
215 if (p != null)
216 {
217 enableStreaming = BooleanUtils.toBoolean(p);
218 }
219 p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "transform.autoWrap");
220 if (p != null)
221 {
222 autoWrapMessageAwareTransform = BooleanUtils.toBoolean(p);
223 }
224 }
225
226 protected void validateEncoding() throws FatalException
227 {
228
229 if (!Charset.isSupported(encoding))
230 {
231 throw new FatalException(CoreMessages.propertyHasInvalidValue("encoding", encoding), this);
232 }
233 }
234
235
236
237
238
239
240
241
242
243
244
245 protected void validateXML() throws FatalException
246 {
247 SAXParserFactory f = SAXParserFactory.newInstance();
248 if (f == null || f.getClass().getName().indexOf("crimson") != -1)
249 {
250 throw new FatalException(CoreMessages.valueIsInvalidFor(f.getClass().getName(),
251 "javax.xml.parsers.SAXParserFactory"), this);
252 }
253 }
254
255 public boolean isDefaultSynchronousEndpoints()
256 {
257 return synchronous;
258 }
259
260 public void setDefaultSynchronousEndpoints(boolean synchronous)
261 {
262 if (verifyContextNotStarted())
263 {
264 this.synchronous = synchronous;
265 }
266 }
267
268 public int getDefaultSynchronousEventTimeout()
269 {
270 return synchronousEventTimeout;
271 }
272
273 public void setDefaultSynchronousEventTimeout(int synchronousEventTimeout)
274 {
275 if (verifyContextNotStarted())
276 {
277 this.synchronousEventTimeout = synchronousEventTimeout;
278 }
279 }
280
281 public boolean isDefaultRemoteSync()
282 {
283 return remoteSync;
284 }
285
286 public void setDefaultRemoteSync(boolean remoteSync)
287 {
288 if (verifyContextNotStarted())
289 {
290 this.remoteSync = remoteSync;
291 }
292 }
293
294 public String getWorkingDirectory()
295 {
296 return workingDirectory;
297 }
298
299 public String getMuleHomeDirectory()
300 {
301 return System.getProperty(MuleProperties.MULE_HOME_DIRECTORY_PROPERTY);
302 }
303
304 public void setWorkingDirectory(String workingDirectory)
305 {
306 if (verifyContextNotInitialized())
307 {
308
309 this.workingDirectory = FileUtils.newFile(workingDirectory).getAbsolutePath().replaceAll("\\\\", "/");
310 }
311 }
312
313 public int getDefaultTransactionTimeout()
314 {
315 return defaultTransactionTimeout;
316 }
317
318 public void setDefaultTransactionTimeout(int defaultTransactionTimeout)
319 {
320 if (verifyContextNotStarted())
321 {
322 this.defaultTransactionTimeout = defaultTransactionTimeout;
323 }
324 }
325
326 public boolean isClientMode()
327 {
328 return clientMode;
329 }
330
331 public String getDefaultEncoding()
332 {
333 return encoding;
334 }
335
336 public void setDefaultEncoding(String encoding)
337 {
338 if (verifyContextNotInitialized())
339 {
340 this.encoding = encoding;
341 }
342 }
343
344 public String getId()
345 {
346 return id;
347 }
348
349 public void setId(String id)
350 {
351 if (verifyContextNotInitialized())
352 {
353 if (StringUtils.isBlank(id))
354 {
355 throw new IllegalArgumentException("Cannot set server id to null/blank");
356 }
357 this.id = id;
358 }
359 }
360
361 public String getClusterId()
362 {
363 return clusterId;
364 }
365
366 public void setClusterId(String clusterId)
367 {
368 if (verifyContextNotInitialized())
369 {
370 this.clusterId = clusterId;
371 }
372 }
373
374 public String getDomainId()
375 {
376 return domainId;
377 }
378
379 public void setDomainId(String domainId)
380 {
381 if (verifyContextNotInitialized())
382 {
383 this.domainId = domainId;
384 }
385 }
386
387 public String getSystemModelType()
388 {
389 return systemModelType;
390 }
391
392 public void setSystemModelType(String systemModelType)
393 {
394 if (verifyContextNotStarted())
395 {
396 this.systemModelType = systemModelType;
397 }
398 }
399
400 public void setClientMode(boolean clientMode)
401 {
402 if (verifyContextNotStarted())
403 {
404 this.clientMode = clientMode;
405 }
406 }
407
408 public String getSystemName()
409 {
410 return domainId + "." + clusterId + "." + id;
411 }
412
413 public boolean isAutoWrapMessageAwareTransform()
414 {
415 return autoWrapMessageAwareTransform;
416 }
417
418 public void setAutoWrapMessageAwareTransform(boolean autoWrapMessageAwareTransform)
419 {
420 if (verifyContextNotStarted())
421 {
422 this.autoWrapMessageAwareTransform = autoWrapMessageAwareTransform;
423 }
424 }
425
426 public boolean isCacheMessageAsBytes()
427 {
428 return cacheMessageAsBytes;
429 }
430
431 public void setCacheMessageAsBytes(boolean cacheMessageAsBytes)
432 {
433 if (verifyContextNotStarted())
434 {
435 this.cacheMessageAsBytes = cacheMessageAsBytes;
436 }
437 }
438
439 public boolean isCacheMessageOriginalPayload()
440 {
441 return cacheMessageOriginalPayload;
442 }
443
444 public void setCacheMessageOriginalPayload(boolean cacheMessageOriginalPayload)
445 {
446 if (verifyContextNotStarted())
447 {
448 this.cacheMessageOriginalPayload = cacheMessageOriginalPayload;
449 }
450 }
451
452 public boolean isEnableStreaming()
453 {
454 return enableStreaming;
455 }
456
457 public void setEnableStreaming(boolean enableStreaming)
458 {
459 if (verifyContextNotStarted())
460 {
461 this.enableStreaming = enableStreaming;
462 }
463 }
464
465 protected boolean verifyContextNotInitialized()
466 {
467 MuleContext context = MuleServer.getMuleContext();
468 if (context != null && context.getLifecycleManager().isPhaseComplete(Initialisable.PHASE_NAME))
469 {
470 logger.warn("Cannot modify MuleConfiguration once the MuleContext has been initialized. Modification will be ignored.");
471 return false;
472 }
473 else
474 {
475 return true;
476 }
477 }
478
479 protected boolean verifyContextNotStarted()
480 {
481 MuleContext context = MuleServer.getMuleContext();
482 if (context != null && context.getLifecycleManager().isPhaseComplete(Startable.PHASE_NAME))
483 {
484 logger.warn("Cannot modify MuleConfiguration once the MuleContext has been started. Modification will be ignored.");
485 return false;
486 }
487 else
488 {
489 return true;
490 }
491 }
492 }