Coverage Report - org.mule.config.DefaultMuleConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMuleConfiguration
46%
72/158
26%
21/82
2.211
 
 1  
 /*
 2  
  * $Id: DefaultMuleConfiguration.java 12269 2008-07-10 04:19:03Z dfeist $
 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.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  
  * Configuration info. which can be set when creating the MuleContext but becomes
 38  
  * immutable after starting the MuleContext.
 39  
  */
 40  
 public class DefaultMuleConfiguration implements MuleConfiguration 
 41  
 {
 42  1164
     private boolean synchronous = false;
 43  
 
 44  
     /**
 45  
      * The type of model used for the internal system model where system created
 46  
      * services are registered
 47  
      */
 48  1164
     private String systemModelType = "seda";
 49  
 
 50  1164
     private String encoding = "UTF-8";
 51  
 
 52  
     /**
 53  
      * When running sychonously, return events can be received over transports that
 54  
      * support ack or replyTo This property determines how long to wait for a receive
 55  
      */
 56  1164
     private int synchronousEventTimeout = 10000;
 57  
 
 58  
     /**
 59  
      * The default transaction timeout value used if no specific transaction time out
 60  
      * has been set on the transaction config
 61  
      */
 62  1164
     private int defaultTransactionTimeout = 30000;
 63  
 
 64  
     /**
 65  
      * Determines whether when running synchronously, return events are received
 66  
      * before returning the call. i.e. in jms wait for a replyTo. Vm queues do this
 67  
      * automatically
 68  
      */
 69  1164
     private boolean remoteSync = false;
 70  
     
 71  
     /** Where Mule stores any runtime files to disk */
 72  1164
     private String workingDirectory = "./.mule";
 73  
 
 74  
     /**
 75  
      * Whether the server instance is running in client mode, which means that some
 76  
      * services will not be started
 77  
      */
 78  1164
     private boolean clientMode = false;
 79  
 
 80  
     /** the unique id for this Mule instance */
 81  
     private String id;
 82  
 
 83  
     /** If this node is part of a cluster then this is the shared cluster Id */
 84  
     private String clusterId;
 85  
 
 86  
     /** The domain name that this instance belongs to. */
 87  
     private String domainId;
 88  
 
 89  
     // Debug options
 90  
     
 91  1164
     private boolean cacheMessageAsBytes = true;
 92  
 
 93  1164
     private boolean cacheMessageOriginalPayload = true;
 94  
 
 95  1164
     private boolean enableStreaming = true;
 96  
 
 97  1164
     private boolean autoWrapMessageAwareTransform = true;
 98  
     
 99  1164
     protected transient Log logger = LogFactory.getLog(DefaultMuleConfiguration.class);
 100  
 
 101  
     public DefaultMuleConfiguration()  
 102  
     {
 103  1164
         super();
 104  
         
 105  
         // Apply any settings which come from the JVM system properties.
 106  1164
         applySystemProperties();
 107  
         
 108  1164
         if (id == null)
 109  
         {
 110  1164
             id = UUID.getUUID();
 111  
         }
 112  
         
 113  1164
         if (clusterId == null)
 114  
         {
 115  1164
             clusterId = CoreMessages.notClustered().getMessage();
 116  
         }
 117  
 
 118  1164
         if (domainId == null)
 119  
         {
 120  
             try
 121  
             {
 122  1164
                 domainId = InetAddress.getLocalHost().getHostName();
 123  
             }
 124  0
             catch (UnknownHostException e)
 125  
             {
 126  0
                 logger.warn(e);
 127  0
                 domainId = "org.mule";
 128  1164
             }
 129  
         }
 130  
         
 131  
         try
 132  
         {
 133  1164
             validateEncoding();
 134  1164
             validateXML();
 135  
         }
 136  0
         catch (FatalException e)
 137  
         {
 138  0
             throw new RuntimeException(e);
 139  1164
         }
 140  1164
     }
 141  
 
 142  
     /**
 143  
      * Apply any settings which come from the JVM system properties.
 144  
      */
 145  
     protected void applySystemProperties() 
 146  
     {
 147  
         String p;
 148  
         
 149  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "encoding");
 150  1164
         if (p != null)
 151  
         {
 152  0
             encoding = p;
 153  
         }
 154  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous");
 155  1164
         if (p != null)
 156  
         {
 157  0
             synchronous = BooleanUtils.toBoolean(p);
 158  
         }
 159  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "systemModelType");
 160  1164
         if (p != null)
 161  
         {
 162  0
             systemModelType = p;
 163  
         }
 164  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "timeout.synchronous");
 165  1164
         if (p != null)
 166  
         {
 167  0
             synchronousEventTimeout = NumberUtils.toInt(p);
 168  
         }
 169  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "timeout.transaction");
 170  1164
         if (p != null)
 171  
         {
 172  0
             defaultTransactionTimeout = NumberUtils.toInt(p);
 173  
         }
 174  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "remoteSync");
 175  1164
         if (p != null)
 176  
         {
 177  0
             remoteSync = BooleanUtils.toBoolean(p);
 178  
         }
 179  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "workingDirectory");
 180  1164
         if (p != null)
 181  
         {
 182  0
             workingDirectory = p;
 183  
         }
 184  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "clientMode");
 185  1164
         if (p != null)
 186  
         {
 187  0
             clientMode = BooleanUtils.toBoolean(p);
 188  
         }
 189  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "serverId");
 190  1164
         if (p != null)
 191  
         {
 192  0
             id = p;
 193  
         }
 194  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "clusterId");
 195  1164
         if (p != null)
 196  
         {
 197  0
             clusterId = p;
 198  
         }
 199  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "domainId");
 200  1164
         if (p != null)
 201  
         {
 202  0
             domainId = p;
 203  
         }
 204  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "message.cacheBytes");
 205  1164
         if (p != null)
 206  
         {
 207  0
             cacheMessageAsBytes = BooleanUtils.toBoolean(p);
 208  
         }
 209  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal");
 210  1164
         if (p != null)
 211  
         {
 212  0
             cacheMessageOriginalPayload = BooleanUtils.toBoolean(p);
 213  
         }
 214  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "streaming.enable");
 215  1164
         if (p != null)
 216  
         {
 217  0
             enableStreaming = BooleanUtils.toBoolean(p);
 218  
         }
 219  1164
         p = System.getProperty(SYSTEM_PROPERTY_PREFIX + "transform.autoWrap");
 220  1164
         if (p != null)
 221  
         {
 222  0
             autoWrapMessageAwareTransform = BooleanUtils.toBoolean(p);
 223  
         }
 224  1164
     }
 225  
 
 226  
     protected void validateEncoding() throws FatalException
 227  
     {
 228  
         //Check we have a valid and supported encoding
 229  1164
         if (!Charset.isSupported(encoding))
 230  
         {
 231  0
             throw new FatalException(CoreMessages.propertyHasInvalidValue("encoding", encoding), this);
 232  
         }
 233  1164
     }
 234  
 
 235  
     /**
 236  
      * Mule needs a proper JAXP implementation and will complain when run with a plain JDK
 237  
      * 1.4. Use the supplied launcher or specify a proper JAXP implementation via
 238  
      * <code>-Djava.endorsed.dirs</code>. See the following URLs for more information:
 239  
      * <ul>
 240  
      * <li> {@link http://xerces.apache.org/xerces2-j/faq-general.html#faq-4}
 241  
      * <li> {@link http://xml.apache.org/xalan-j/faq.html#faq-N100D6}
 242  
      * <li> {@link http://java.sun.com/j2se/1.4.2/docs/guide/standards/}
 243  
      * </ul>
 244  
      */
 245  
     protected void validateXML() throws FatalException
 246  
     {
 247  1164
         SAXParserFactory f = SAXParserFactory.newInstance();
 248  1164
         if (f == null || f.getClass().getName().indexOf("crimson") != -1)
 249  
         {
 250  0
             throw new FatalException(CoreMessages.valueIsInvalidFor(f.getClass().getName(),
 251  
                 "javax.xml.parsers.SAXParserFactory"), this);
 252  
         }
 253  1164
     }
 254  
 
 255  
     public boolean isDefaultSynchronousEndpoints()
 256  
     {
 257  484
         return synchronous;
 258  
     }
 259  
 
 260  
     public void setDefaultSynchronousEndpoints(boolean synchronous)
 261  
     {
 262  0
         if (verifyContextNotStarted())
 263  
         {
 264  0
             this.synchronous = synchronous;
 265  
         }
 266  0
     }
 267  
 
 268  
     public int getDefaultSynchronousEventTimeout()
 269  
     {
 270  2020
         return synchronousEventTimeout;
 271  
     }
 272  
 
 273  
     public void setDefaultSynchronousEventTimeout(int synchronousEventTimeout)
 274  
     {
 275  0
         if (verifyContextNotStarted())
 276  
         {
 277  0
             this.synchronousEventTimeout = synchronousEventTimeout;
 278  
         }
 279  0
     }
 280  
 
 281  
     public boolean isDefaultRemoteSync()
 282  
     {
 283  494
         return remoteSync;
 284  
     }
 285  
 
 286  
     public void setDefaultRemoteSync(boolean remoteSync)
 287  
     {
 288  0
         if (verifyContextNotStarted())
 289  
         {
 290  0
             this.remoteSync = remoteSync;
 291  
         }
 292  0
     }
 293  
 
 294  
     public String getWorkingDirectory()
 295  
     {
 296  1176
         return workingDirectory;
 297  
     }
 298  
 
 299  
     public String getMuleHomeDirectory()
 300  
     {
 301  0
         return System.getProperty(MuleProperties.MULE_HOME_DIRECTORY_PROPERTY);
 302  
     }
 303  
 
 304  
     public void setWorkingDirectory(String workingDirectory)
 305  
     {
 306  0
         if (verifyContextNotInitialized())
 307  
         {
 308  
             // fix windows backslashes in absolute paths, convert them to forward ones
 309  0
             this.workingDirectory = FileUtils.newFile(workingDirectory).getAbsolutePath().replaceAll("\\\\", "/");
 310  
         }
 311  0
     }
 312  
 
 313  
     public int getDefaultTransactionTimeout()
 314  
     {
 315  0
         return defaultTransactionTimeout;
 316  
     }
 317  
 
 318  
     public void setDefaultTransactionTimeout(int defaultTransactionTimeout)
 319  
     {
 320  0
         if (verifyContextNotStarted())
 321  
         {
 322  0
             this.defaultTransactionTimeout = defaultTransactionTimeout;
 323  
         }
 324  0
     }
 325  
 
 326  
     public boolean isClientMode()
 327  
     {
 328  0
         return clientMode;
 329  
     }
 330  
 
 331  
     public String getDefaultEncoding()
 332  
     {
 333  516
         return encoding;
 334  
     }
 335  
 
 336  
     public void setDefaultEncoding(String encoding)
 337  
     {
 338  0
         if (verifyContextNotInitialized())
 339  
         {
 340  0
             this.encoding = encoding;
 341  
         }
 342  0
     }
 343  
 
 344  
     public String getId()
 345  
     {
 346  9798
         return id;
 347  
     }
 348  
 
 349  
     public void setId(String id)
 350  
     {
 351  0
         if (verifyContextNotInitialized())
 352  
         {
 353  0
             if (StringUtils.isBlank(id))
 354  
             {
 355  0
                 throw new IllegalArgumentException("Cannot set server id to null/blank");
 356  
             }
 357  0
             this.id = id;
 358  
         }
 359  0
     }
 360  
 
 361  
     public String getClusterId()
 362  
     {
 363  14692
         return clusterId;
 364  
     }
 365  
 
 366  
     public void setClusterId(String clusterId)
 367  
     {
 368  0
         if (verifyContextNotInitialized())
 369  
         {
 370  0
             this.clusterId = clusterId;
 371  
         }
 372  0
     }
 373  
 
 374  
     public String getDomainId()
 375  
     {
 376  14692
         return domainId;
 377  
     }
 378  
 
 379  
     public void setDomainId(String domainId)
 380  
     {
 381  0
         if (verifyContextNotInitialized())
 382  
         {
 383  0
             this.domainId = domainId;
 384  
         }
 385  0
     }
 386  
 
 387  
     public String getSystemModelType()
 388  
     {
 389  0
         return systemModelType;
 390  
     }
 391  
 
 392  
     public void setSystemModelType(String systemModelType)
 393  
     {
 394  0
         if (verifyContextNotStarted())
 395  
         {
 396  0
             this.systemModelType = systemModelType;
 397  
         }
 398  0
     }
 399  
 
 400  
     public void setClientMode(boolean clientMode)
 401  
     {
 402  0
         if (verifyContextNotStarted())
 403  
         {
 404  0
             this.clientMode = clientMode;
 405  
         }
 406  0
     }
 407  
 
 408  
     public String getSystemName()
 409  
     {
 410  0
         return domainId + "." + clusterId + "." + id;
 411  
     }
 412  
     
 413  
     public boolean isAutoWrapMessageAwareTransform()
 414  
     {
 415  0
         return autoWrapMessageAwareTransform;
 416  
     }
 417  
 
 418  
     public void setAutoWrapMessageAwareTransform(boolean autoWrapMessageAwareTransform)
 419  
     {
 420  0
         if (verifyContextNotStarted())
 421  
         {
 422  0
             this.autoWrapMessageAwareTransform = autoWrapMessageAwareTransform;
 423  
         }
 424  0
     }
 425  
 
 426  
     public boolean isCacheMessageAsBytes()
 427  
     {
 428  60
         return cacheMessageAsBytes;
 429  
     }
 430  
 
 431  
     public void setCacheMessageAsBytes(boolean cacheMessageAsBytes)
 432  
     {
 433  0
         if (verifyContextNotStarted())
 434  
         {
 435  0
             this.cacheMessageAsBytes = cacheMessageAsBytes;
 436  
         }
 437  0
     }
 438  
 
 439  
     public boolean isCacheMessageOriginalPayload()
 440  
     {
 441  116
         return cacheMessageOriginalPayload;
 442  
     }
 443  
 
 444  
     public void setCacheMessageOriginalPayload(boolean cacheMessageOriginalPayload)
 445  
     {
 446  0
         if (verifyContextNotStarted())
 447  
         {
 448  0
             this.cacheMessageOriginalPayload = cacheMessageOriginalPayload;
 449  
         }
 450  0
     }
 451  
 
 452  
     public boolean isEnableStreaming()
 453  
     {
 454  0
         return enableStreaming;
 455  
     }
 456  
 
 457  
     public void setEnableStreaming(boolean enableStreaming)
 458  
     {
 459  0
         if (verifyContextNotStarted())
 460  
         {
 461  0
             this.enableStreaming = enableStreaming;
 462  
         }
 463  0
     }
 464  
 
 465  
     protected boolean verifyContextNotInitialized()
 466  
     {
 467  0
         MuleContext context = MuleServer.getMuleContext();
 468  0
         if (context != null && context.getLifecycleManager().isPhaseComplete(Initialisable.PHASE_NAME))
 469  
         {
 470  0
             logger.warn("Cannot modify MuleConfiguration once the MuleContext has been initialized.  Modification will be ignored.");
 471  0
             return false;
 472  
         }
 473  
         else
 474  
         {
 475  0
             return true;
 476  
         }
 477  
     }
 478  
     
 479  
     protected boolean verifyContextNotStarted()
 480  
     {
 481  0
         MuleContext context = MuleServer.getMuleContext();
 482  0
         if (context != null && context.getLifecycleManager().isPhaseComplete(Startable.PHASE_NAME))
 483  
         {
 484  0
             logger.warn("Cannot modify MuleConfiguration once the MuleContext has been started.  Modification will be ignored.");
 485  0
             return false;
 486  
         }
 487  
         else
 488  
         {
 489  0
             return true;
 490  
         }
 491  
     }
 492  
 }