View Javadoc

1   /*
2    * $Id: MuleConfiguration.java 10415 2008-01-21 10:46:37Z 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;
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   * <code>MuleConfiguration</code> holds the runtime configuration specific to the
42   * <code>MuleManager</code>. Once the <code>MuleManager</code> has been
43   * initialised this class is immutable.
44   */
45  public class MuleConfiguration
46  {
47      private static final String DEFAULT_LOG_DIRECTORY = "logs";
48  
49      /**
50       * logger used by this class
51       */
52      protected transient Log logger = LogFactory.getLog(getClass());
53  
54      /**
55       * The default serverUrl used to receive incoming requests from clients
56       */
57      public static final String DEFAULT_SERVER_URL = "tcp://localhost:60504";
58  
59      /**
60       * Specifies that the transformer properties should be obtained from the Mule
61       * Manager properties
62       */
63      public static final String USE_MANAGER_PROPERTIES = "org.mule.useManagerProperties";
64  
65      /**
66       * Specifies whether mule should process messages sysnchonously, i.e. that a
67       * mule-model can only processone message at a time, or asynchonously. The
68       * default value is 'false'.
69       */
70      public static final String SYNCHRONOUS_PROPERTY = "synchronous";
71  
72      public static final String DEFAULT_ENCODING = "UTF-8";
73      /**
74       * Default encoding used in OS running Mule
75       */
76      public static final String DEFAULT_OS_ENCODING = System.getProperty("file.encoding");
77  
78      // /**
79      // * Determines the default inboundProvider that Mule uses to communicate
80      // between MuelUMO's when an
81      // * inboundProvider is not specified on a mule.
82      // * If this value is not specifed, Mule will create a default VM connection
83      // * called 'muleVMInboundProvider' which will use a VMConnector.
84      // */
85      // public static final String DEFAULT_INBOUND_PROVIDER_PROPERTY =
86      // "defaultInboundProvider";
87      //
88      // /**
89      // * Determines the default outboundProvider that Mule uses to communicate
90      // between MuelUMO's when an
91      // * outboundProvider is not specified on a mule.
92      // * If this value is not specifed, Mule will create a default VM connection
93      // * called 'muleVMOutbound' which will use a VMConnector.
94      // */
95      // public static final String DEFAULT_OUTBOUND_PROVIDER_PROPERTY =
96      // "defaultOutboundProvider";
97  
98      /**
99       * Default value for SYNCHRONOUS_PROPERTY
100      */
101     public static final boolean DEFAULT_SYNCHRONOUS = false;
102 
103     /**
104      * Default value for MAX_OUTSTANDING_MESSAGES_PROPERTY
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      * Where Mule stores any runtime files to disk
116      */
117     public static final String DEFAULT_WORKING_DIRECTORY = "./.mule";
118 
119     /**
120      * The default queueStore directory for persistence
121      */
122     public static final String DEFAULT_QUEUE_STORE = "queuestore";
123 
124     /**
125      * holds the value for SYNCHRONOUS
126      */
127     private boolean synchronous = DEFAULT_SYNCHRONOUS;
128 
129     /**
130      * The type of model used for the internal system model where system created
131      * services are registered
132      */
133     private String systemModelType = DEFAULT_SYSTEM_MODEL_TYPE;
134 
135     /**
136      * Whether Mule should fire message events for every message sent and received
137      */
138     private boolean enableMessageEvents = false;
139 
140     /**
141      * Name of the model to use. If blank the first model will be used
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      * configuration for the threadpool used by message dispatchers
153      */
154     private ThreadingProfile messageDispatcherThreadingProfile = null;
155 
156     /**
157      * configuration for the threadpool used by message receivers
158      */
159     private ThreadingProfile messageReceiverThreadingProfile = null;
160 
161     /**
162      * configuration for the threadpool used by component pooling in mule
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      * When running sychonously, return events can be received over transports that
172      * support ack or replyTo This property determines how long to wait for a receive
173      */
174     private int synchronousEventTimeout = DEFAULT_TIMEOUT;
175 
176     /**
177      * The default transaction timeout value used if no specific transaction time out
178      * has been set on the transaction config
179      */
180     private int transactionTimeout = DEFAULT_TRANSACTION_TIMEOUT;
181 
182     /**
183      * Determines whether when running synchronously, return events are received
184      * before returning the call. i.e. in jms wait for a replyTo. Vm queues do this
185      * automatically
186      */
187     private boolean remoteSync = false;
188 
189     /**
190      * Determines whether internal vm queues are persistent. If they are, if the
191      * server dies unexpectedly it can resume it's current state and continue
192      * processing
193      */
194     private boolean recoverableMode = false;
195     /**
196      * A helper thread pool configuration that is used for all other thread pool
197      * configurations
198      */
199     private ThreadingProfile defaultThreadingProfile = new ThreadingProfile();
200 
201     /**
202      * Where mule will store any runtime files to disk
203      */
204     private String workingDirectory;
205 
206     /**
207      * The configuration resources used to configure the MuleManager instance
208      */
209     private String[] configResources = new String[]{};
210 
211     /**
212      * This is the url used by the server itself to receive incomming requests. This
213      * enables clients such as the Mule Client to marshal remote requests to a
214      * MuleManager instance. The default value is tcp://localhost:61616
215      */
216     private String serverUrl = DEFAULT_SERVER_URL;
217 
218     /**
219      * The Mule Jar manifest object
220      */
221     private Manifest manifest = null;
222 
223     /**
224      * Whether the server instance is running in client mode, which means that some
225      * services will not be started
226      */
227     private boolean clientMode = false;
228 
229     /**
230      * Whether the server is embedded by another framework and certain stand-alone
231      * features
232      */
233     private boolean embedded = false;
234 
235     /**
236      * The model type to use for component invocations
237      */
238     private String modelType = "default";
239 
240     /**
241      * The default connection Strategy used for a connector when one hasn't been
242      * defined for the connector
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      * @return true if the model is running synchronously or false otherwise
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         // fix windows backslashes in absolute paths, convert them to forward ones
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      * @deprecated use MuleManifest instead
446      */
447     public String getProductVersion()
448     {
449         return MuleManifest.getProductVersion();
450     }
451 
452     /**
453      * @deprecated use MuleManifest instead
454      */
455     public String getVendorName()
456     {
457         return MuleManifest.getVendorName();
458     }
459 
460     /**
461      * @deprecated use MuleManifest instead
462      */
463     public String getVendorUrl()
464     {
465         return MuleManifest.getVendorUrl();
466     }
467 
468     /**
469      * @deprecated use MuleManifest instead
470      */
471     public String getProductUrl()
472     {
473         return MuleManifest.getProductUrl();
474     }
475 
476     /**
477      * @deprecated use MuleManifest instead
478      */
479     public String getProductName()
480     {
481         return MuleManifest.getProductName();
482     }
483 
484     /**
485      * @deprecated use MuleManifest instead
486      */
487     public String getProductMoreInfo()
488     {
489         return MuleManifest.getProductMoreInfo();
490     }
491 
492     /**
493      * @deprecated use MuleManifest instead
494      */
495     public String getProductSupport()
496     {
497         return MuleManifest.getProductSupport();
498     }
499 
500     /**
501      * @deprecated use MuleManifest instead
502      */
503     public String getProductLicenseInfo()
504     {
505         return MuleManifest.getProductLicenseInfo();
506     }
507 
508     /**
509      * @deprecated use MuleManifest instead
510      */
511     public String getProductDescription()
512     {
513         return MuleManifest.getProductDescription();
514     }
515 
516     /**
517      * @deprecated use MuleManifest.getBuildNumber() instead
518      */
519     public String getBuildDate()
520     {
521         return MuleManifest.getBuildDate();
522     }
523 
524     /**
525      * @deprecated use MuleManifest instead
526      */
527     public Manifest getManifest()
528     {
529         if (manifest == null)
530         {
531             manifest = new Manifest();
532 
533             InputStream is = null;
534             try
535             {
536                 // We want to load the MANIFEST.MF from the mule-core jar. Sine we
537                 // don't the version we're using
538                 // we have to search for the jar on the classpath
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      * @deprecated use MuleManifest instead
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      * Returns a clone of the default Connection strategy. The clone ensures that the
628      * connection strategy can be manipulated without affecting other connectors
629      * using the same strategy
630      * 
631      * @return a clone of the default Connection strategy
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      * Sets the connection strategy used by all connectors managed in this Mule
647      * instance if the connector has no connection strategy specifically set on it.
648      * 
649      * @param connectionStrategy the default strategy to use
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 }