1
2
3
4
5
6
7
8
9
10
11 package org.mule.config;
12
13 import org.mule.MuleManager;
14 import org.mule.MuleRuntimeException;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.providers.ConnectionStrategy;
17 import org.mule.providers.SingleAttemptConnectionStrategy;
18 import org.mule.umo.manager.DefaultWorkListener;
19 import org.mule.util.FileUtils;
20 import org.mule.util.StringUtils;
21 import org.mule.util.queue.EventFilePersistenceStrategy;
22 import org.mule.util.queue.QueuePersistenceStrategy;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import java.util.Enumeration;
31 import java.util.jar.Attributes;
32 import java.util.jar.Manifest;
33
34 import javax.resource.spi.work.WorkListener;
35
36 import org.apache.commons.beanutils.BeanUtils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
41
42
43
44
45 public class MuleConfiguration
46 {
47 private static final String DEFAULT_LOG_DIRECTORY = "logs";
48
49
50
51
52 protected transient Log logger = LogFactory.getLog(getClass());
53
54
55
56
57 public static final String DEFAULT_SERVER_URL = "tcp://localhost:60504";
58
59
60
61
62
63 public static final String USE_MANAGER_PROPERTIES = "org.mule.useManagerProperties";
64
65
66
67
68
69
70 public static final String SYNCHRONOUS_PROPERTY = "synchronous";
71
72 public static final String DEFAULT_ENCODING = "UTF-8";
73
74
75
76 public static final String DEFAULT_OS_ENCODING = System.getProperty("file.encoding");
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static final boolean DEFAULT_SYNCHRONOUS = false;
102
103
104
105
106 public static final int DEFAULT_MAX_OUTSTANDING_MESSAGES = 1000;
107
108 public static final int DEFAULT_TIMEOUT = 10000;
109
110 public static final int DEFAULT_TRANSACTION_TIMEOUT = 30000;
111
112 public static final String DEFAULT_SYSTEM_MODEL_TYPE = "seda";
113
114
115
116
117 public static final String DEFAULT_WORKING_DIRECTORY = "./.mule";
118
119
120
121
122 public static final String DEFAULT_QUEUE_STORE = "queuestore";
123
124
125
126
127 private boolean synchronous = DEFAULT_SYNCHRONOUS;
128
129
130
131
132
133 private String systemModelType = DEFAULT_SYSTEM_MODEL_TYPE;
134
135
136
137
138 private boolean enableMessageEvents = false;
139
140
141
142
143 private String model = null;
144
145 private String encoding = DEFAULT_ENCODING;
146
147 private String osEncoding = DEFAULT_OS_ENCODING;
148
149 private PoolingProfile poolingProfile = new PoolingProfile();
150
151
152
153
154 private ThreadingProfile messageDispatcherThreadingProfile = null;
155
156
157
158
159 private ThreadingProfile messageReceiverThreadingProfile = null;
160
161
162
163
164 private ThreadingProfile componentPoolThreadingProfile = null;
165
166 private QueueProfile queueProfile = new QueueProfile(DEFAULT_MAX_OUTSTANDING_MESSAGES, false);
167
168 private QueuePersistenceStrategy persistenceStrategy = new EventFilePersistenceStrategy();
169
170
171
172
173
174 private int synchronousEventTimeout = DEFAULT_TIMEOUT;
175
176
177
178
179
180 private int transactionTimeout = DEFAULT_TRANSACTION_TIMEOUT;
181
182
183
184
185
186
187 private boolean remoteSync = false;
188
189
190
191
192
193
194 private boolean recoverableMode = false;
195
196
197
198
199 private ThreadingProfile defaultThreadingProfile = new ThreadingProfile();
200
201
202
203
204 private String workingDirectory;
205
206
207
208
209 private String[] configResources = new String[]{};
210
211
212
213
214
215
216 private String serverUrl = DEFAULT_SERVER_URL;
217
218
219
220
221 private Manifest manifest = null;
222
223
224
225
226
227 private boolean clientMode = false;
228
229
230
231
232
233 private boolean embedded = false;
234
235
236
237
238 private String modelType = "default";
239
240
241
242
243
244 private ConnectionStrategy connectionStrategy = new SingleAttemptConnectionStrategy();
245
246 private WorkListener workListener = new DefaultWorkListener();
247
248 public MuleConfiguration()
249 {
250 super();
251 setWorkingDirectory(DEFAULT_WORKING_DIRECTORY);
252 }
253
254
255
256
257 public boolean isSynchronous()
258 {
259 return synchronous;
260 }
261
262 public void setSynchronous(boolean synchronous)
263 {
264 this.synchronous = synchronous;
265 }
266
267 public String getModel()
268 {
269 return model;
270 }
271
272 public void setModel(String model)
273 {
274 this.model = model;
275 }
276
277 public ThreadingProfile getMessageDispatcherThreadingProfile()
278 {
279 return getThreadingProfile(messageDispatcherThreadingProfile);
280 }
281
282 public void setMessageDispatcherThreadingProfile(ThreadingProfile messageDispatcherThreadingProfile)
283 {
284 this.messageDispatcherThreadingProfile = messageDispatcherThreadingProfile;
285 }
286
287 public ThreadingProfile getMessageReceiverThreadingProfile()
288 {
289 return getThreadingProfile(messageReceiverThreadingProfile);
290 }
291
292 public void setMessageReceiverThreadingProfile(ThreadingProfile messageReceiverThreadingProfile)
293 {
294 this.messageReceiverThreadingProfile = messageReceiverThreadingProfile;
295 }
296
297 public ThreadingProfile getComponentThreadingProfile()
298 {
299 return getThreadingProfile(componentPoolThreadingProfile);
300 }
301
302 public void setComponentThreadingProfile(ThreadingProfile componentPoolThreadingProfile)
303 {
304 this.componentPoolThreadingProfile = componentPoolThreadingProfile;
305 }
306
307 public ThreadingProfile getDefaultThreadingProfile()
308 {
309 return getThreadingProfile(defaultThreadingProfile);
310 }
311
312 public void setDefaultThreadingProfile(ThreadingProfile defaultThreadingProfile)
313 {
314 if (defaultThreadingProfile == null)
315 {
316 return;
317 }
318 this.defaultThreadingProfile = defaultThreadingProfile;
319 }
320
321 private ThreadingProfile getThreadingProfile(ThreadingProfile profile)
322 {
323 if (profile != null)
324 {
325 return new ThreadingProfile(profile);
326 }
327 return new ThreadingProfile(defaultThreadingProfile);
328 }
329
330 public PoolingProfile getPoolingProfile()
331 {
332 return new PoolingProfile(poolingProfile);
333 }
334
335 public void setPoolingProfile(PoolingProfile poolingProfile)
336 {
337 this.poolingProfile = poolingProfile;
338 }
339
340 public int getSynchronousEventTimeout()
341 {
342 return synchronousEventTimeout;
343 }
344
345 public void setSynchronousEventTimeout(int synchronousEventTimeout)
346 {
347 this.synchronousEventTimeout = synchronousEventTimeout;
348 }
349
350 public boolean isRemoteSync()
351 {
352 return remoteSync;
353 }
354
355 public void setRemoteSync(boolean remoteSync)
356 {
357 this.remoteSync = remoteSync;
358 }
359
360 public QueueProfile getQueueProfile()
361 {
362 return new QueueProfile(queueProfile);
363 }
364
365 public void setQueueProfile(QueueProfile queueProfile)
366 {
367 this.queueProfile = queueProfile;
368 }
369
370 public boolean isRecoverableMode()
371 {
372 return recoverableMode;
373 }
374
375 public void setRecoverableMode(boolean recoverableMode)
376 {
377 this.recoverableMode = recoverableMode;
378 if (recoverableMode)
379 {
380 queueProfile.setPersistent(true);
381 }
382 }
383
384 public String getWorkingDirectory()
385 {
386 return workingDirectory;
387 }
388
389 public String getMuleHomeDirectory()
390 {
391 return System.getProperty(MuleProperties.MULE_HOME_DIRECTORY_PROPERTY);
392 }
393
394 public String getLogDirectory()
395 {
396 return getMuleHomeDirectory() + File.separator + DEFAULT_LOG_DIRECTORY;
397 }
398
399 public void setWorkingDirectory(String workingDirectory)
400 {
401
402 this.workingDirectory = FileUtils.newFile(workingDirectory).getAbsolutePath().replaceAll("\\\\", "/");
403 updateApplicationProperty(MuleProperties.MULE_WORKING_DIRECTORY_PROPERTY, this.workingDirectory);
404 }
405
406 public String[] getConfigResources()
407 {
408 return configResources;
409 }
410
411 public void setConfigResources(String[] configResources)
412 {
413 if (configResources != null)
414 {
415 int current = this.configResources.length;
416 String[] newResources = new String[configResources.length + current];
417 System.arraycopy(this.configResources, 0, newResources, 0, current);
418 System.arraycopy(configResources, 0, newResources, current, configResources.length);
419 this.configResources = newResources;
420 }
421 else
422 {
423 this.configResources = configResources;
424 }
425 }
426
427 public String getServerUrl()
428 {
429 return serverUrl;
430 }
431
432 public void setServerUrl(String serverUrl)
433 {
434 if (embedded)
435 {
436 this.serverUrl = null;
437 }
438 else
439 {
440 this.serverUrl = serverUrl;
441 }
442 }
443
444
445
446
447 public String getProductVersion()
448 {
449 return MuleManifest.getProductVersion();
450 }
451
452
453
454
455 public String getVendorName()
456 {
457 return MuleManifest.getVendorName();
458 }
459
460
461
462
463 public String getVendorUrl()
464 {
465 return MuleManifest.getVendorUrl();
466 }
467
468
469
470
471 public String getProductUrl()
472 {
473 return MuleManifest.getProductUrl();
474 }
475
476
477
478
479 public String getProductName()
480 {
481 return MuleManifest.getProductName();
482 }
483
484
485
486
487 public String getProductMoreInfo()
488 {
489 return MuleManifest.getProductMoreInfo();
490 }
491
492
493
494
495 public String getProductSupport()
496 {
497 return MuleManifest.getProductSupport();
498 }
499
500
501
502
503 public String getProductLicenseInfo()
504 {
505 return MuleManifest.getProductLicenseInfo();
506 }
507
508
509
510
511 public String getProductDescription()
512 {
513 return MuleManifest.getProductDescription();
514 }
515
516
517
518
519 public String getBuildDate()
520 {
521 return MuleManifest.getBuildDate();
522 }
523
524
525
526
527 public Manifest getManifest()
528 {
529 if (manifest == null)
530 {
531 manifest = new Manifest();
532
533 InputStream is = null;
534 try
535 {
536
537
538
539 URL url = (URL) AccessController.doPrivileged(new PrivilegedAction()
540 {
541 public Object run()
542 {
543 try
544 {
545 Enumeration e = MuleConfiguration.class.getClassLoader().getResources(
546 ("META-INF/MANIFEST.MF"));
547 while (e.hasMoreElements())
548 {
549 URL url = (URL) e.nextElement();
550 if (url.toExternalForm().indexOf("mule-core") > -1)
551 {
552 return url;
553 }
554 }
555 }
556 catch (IOException e1)
557 {
558 logger.error("Failure reading manifest: " + e1.getMessage(), e1);
559 }
560 return null;
561 }
562 });
563
564 if (url != null)
565 {
566 is = url.openStream();
567 }
568
569 if (is != null)
570 {
571 manifest.read(is);
572 }
573
574 }
575 catch (IOException e)
576 {
577 logger.warn("Failed to read manifest Info, Manifest information will not display correctly: "
578 + e.getMessage());
579 }
580 }
581 return manifest;
582 }
583
584
585
586
587 protected String getManifestProperty(String name)
588 {
589 return getManifest().getMainAttributes().getValue(new Attributes.Name(name));
590 }
591
592 public int getTransactionTimeout()
593 {
594 return transactionTimeout;
595 }
596
597 public void setTransactionTimeout(int transactionTimeout)
598 {
599 this.transactionTimeout = transactionTimeout;
600 }
601
602 public boolean isClientMode()
603 {
604 return clientMode;
605 }
606
607 public void setClientMode(boolean clientMode)
608 {
609 this.clientMode = clientMode;
610 if (clientMode)
611 {
612 setServerUrl("");
613 }
614 }
615
616 public QueuePersistenceStrategy getPersistenceStrategy()
617 {
618 return persistenceStrategy;
619 }
620
621 public void setPersistenceStrategy(QueuePersistenceStrategy persistenceStrategy)
622 {
623 this.persistenceStrategy = persistenceStrategy;
624 }
625
626
627
628
629
630
631
632
633 public ConnectionStrategy getConnectionStrategy()
634 {
635 try
636 {
637 return (ConnectionStrategy) BeanUtils.cloneBean(connectionStrategy);
638 }
639 catch (Exception e)
640 {
641 throw new MuleRuntimeException(CoreMessages.failedToClone("Connection Strategy"), e);
642 }
643 }
644
645
646
647
648
649
650
651 public void setConnectionStrategy(ConnectionStrategy connectionStrategy)
652 {
653 this.connectionStrategy = connectionStrategy;
654 }
655
656 public boolean isEmbedded()
657 {
658 return embedded;
659 }
660
661 public void setEmbedded(boolean embedded)
662 {
663 this.embedded = embedded;
664 if (embedded)
665 {
666 serverUrl = null;
667 }
668 }
669
670 public String getModelType()
671 {
672 return modelType;
673 }
674
675 public void setModelType(String modelType)
676 {
677 this.modelType = modelType;
678 }
679
680 public String getEncoding()
681 {
682 return encoding;
683 }
684
685 public void setEncoding(String encoding)
686 {
687 if (StringUtils.isEmpty(encoding))
688 {
689 logger.warn("Cannot set encoding to null or empty String");
690 return;
691 }
692 this.encoding = encoding;
693 }
694
695 public String getOSEncoding()
696 {
697 return osEncoding;
698 }
699
700 public void setOSEncoding(String osEncoding)
701 {
702 this.osEncoding = osEncoding;
703 }
704
705 public boolean isEnableMessageEvents()
706 {
707 return enableMessageEvents;
708 }
709
710 public void setEnableMessageEvents(boolean enableMessageEvents)
711 {
712 this.enableMessageEvents = enableMessageEvents;
713 }
714
715 public WorkListener getWorkListener()
716 {
717 return workListener;
718 }
719
720 public void setWorkListener(WorkListener workListener)
721 {
722 if (workListener == null)
723 {
724 throw new IllegalArgumentException("workListener is null");
725 }
726 this.workListener = workListener;
727 }
728
729 private void updateApplicationProperty(String name, Object value)
730 {
731 if (MuleManager.isInstanciated())
732 {
733 MuleManager.getInstance().setProperty(name, value);
734 }
735 }
736
737 public String getSystemModelType()
738 {
739 return systemModelType;
740 }
741
742 public void setSystemModelType(String systemModelType)
743 {
744 this.systemModelType = systemModelType;
745 }
746 }