View Javadoc

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