View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.config.ConfigurationException;
11  import org.mule.api.config.MuleConfiguration;
12  import org.mule.api.context.MuleContextAware;
13  import org.mule.config.DefaultMuleConfiguration;
14  import org.mule.config.i18n.MessageFactory;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.springframework.beans.factory.SmartFactoryBean;
19  
20  /**
21   * This class is a "SmartFactoryBean" which allows a few XML attributes to be set on the 
22   * otherwise read-only MuleConfiguration.  It looks up the MuleConfiguration from the 
23   * MuleContext and does some class-casting to be able to modify it.  Note that this will
24   * only work if the MuleContext has not yet been started, otherwise the modifications 
25   * will be ignored (and warnings logged).
26   */
27  public class MuleConfigurationConfigurator implements MuleContextAware, SmartFactoryBean
28  {
29      private MuleContext muleContext;
30      
31      // We instantiate DefaultMuleConfiguration to make sure we get the default values for 
32      // any properties not set by the user.
33      private DefaultMuleConfiguration config = new DefaultMuleConfiguration();
34  
35      protected transient Log logger = LogFactory.getLog(MuleConfigurationConfigurator.class);
36  
37      public void setMuleContext(MuleContext context)
38      {
39          this.muleContext = context;
40      }
41  
42      public boolean isEagerInit()
43      {
44          return true;
45      }
46  
47      public boolean isPrototype()
48      {
49          return false;
50      }
51  
52      public Object getObject() throws Exception
53      {
54          MuleConfiguration configuration = muleContext.getConfiguration();
55          if (configuration instanceof DefaultMuleConfiguration)
56          {
57              DefaultMuleConfiguration defaultConfig = (DefaultMuleConfiguration) configuration;
58              defaultConfig.setDefaultResponseTimeout(config.getDefaultResponseTimeout());
59              defaultConfig.setDefaultTransactionTimeout(config.getDefaultTransactionTimeout());
60              defaultConfig.setShutdownTimeout(config.getShutdownTimeout());
61              return configuration;
62          }
63          else
64          {
65              throw new ConfigurationException(MessageFactory.createStaticMessage("Unable to set properties on read-only MuleConfiguration: " + configuration.getClass()));
66          }
67      }
68  
69      public Class<?> getObjectType()
70      {
71          return MuleConfiguration.class;
72      }
73  
74      public boolean isSingleton()
75      {
76          return true;
77      }
78  
79      public void setDefaultSynchronousEndpoints(boolean synchronous)
80      {
81          config.setDefaultSynchronousEndpoints(synchronous);
82      }
83      
84      public void setDefaultResponseTimeout(int responseTimeout)
85      {
86          config.setDefaultResponseTimeout(responseTimeout);
87      }
88      
89      
90      public void setDefaultTransactionTimeout(int defaultTransactionTimeout)
91      {
92          config.setDefaultTransactionTimeout(defaultTransactionTimeout);
93      }
94  
95      public void setShutdownTimeout(int shutdownTimeout)
96      {
97          config.setShutdownTimeout(shutdownTimeout);
98      }
99  
100 }