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.builders;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.config.ConfigurationBuilder;
11  import org.mule.api.config.ConfigurationException;
12  import org.mule.api.lifecycle.LifecycleManager;
13  import org.mule.config.i18n.CoreMessages;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * A support class for {@link org.mule.api.config.ConfigurationBuilder} implementations
20   * that handles the logic of creating config arrays and {@link java.util.Properties}
21   * arguments
22   * 
23   * @see org.mule.api.config.ConfigurationBuilder
24   */
25  public abstract class AbstractConfigurationBuilder implements ConfigurationBuilder
26  {
27      protected transient final Log logger = LogFactory.getLog(getClass());
28  
29      protected boolean configured = false;
30  
31      /**
32       * Will configure a MuleContext object based on the builders configuration settings.
33       * This method will delegate the actual processing to {@link #doConfigure(org.mule.api.MuleContext)}
34       *
35       * @param muleContext The current {@link org.mule.api.MuleContext}
36       * @throws ConfigurationException if the configuration fails i.e. an object cannot be created or
37       * initialised properly
38       */
39      public void configure(MuleContext muleContext) throws ConfigurationException
40      {
41          try
42          {
43              doConfigure(muleContext);
44              applyLifecycle(muleContext.getLifecycleManager());
45              configured = true;
46          }
47          catch (Exception e)
48          {
49              logger.error(CoreMessages.configurationBuilderError(this), e);
50              throw new ConfigurationException(e);
51          }
52      }
53  
54      /**
55       * Will configure a MuleContext based on the configuration provided.  The configuration will be set on the
56       * {@link org.mule.api.config.ConfigurationBuilder} implementation as bean properties before this method
57       * has been called.
58       *
59       * @param muleContext The current {@link org.mule.api.MuleContext}
60       * @throws ConfigurationException if the configuration fails i.e. an object cannot be created or
61       * initialised properly
62       */
63      protected abstract void doConfigure(MuleContext muleContext) throws Exception;
64  
65      /**
66       * Allows a configuration builder to check and customise the lifecycle of objects in the registry
67       * being used.  The ONLY time a user should implement this method is if the underlying container for
68       * the Registry is an IoC container had manages it's own lifecycle.  If this is the case the lifecycle
69       * manager can be used to call the next lifecycle method on all the objects.  For example for the Spring
70       * Registry only Initialise and Dispose phase is handled by Spring. The Start and Stop phases are handled
71       * by Mule by calling-
72       * <code>
73       * // If the MuleContext is started, start all objects in the new Registry.
74       *  if (lifecycleManager.isPhaseComplete(Startable.PHASE_NAME))
75       *  {
76       *      lifecycleManager.applyPhase(registry.lookupObjects(Object.class), Startable.PHASE_NAME);
77       *  }
78       * </code>
79       * @param lifecycleManager the lifecycleManager for the current context
80       * @throws Exception if anything goes wrong.  Usually this is an exeption bubbled up from calling
81       * a lifecycle method on an object in the registry
82       */
83      protected void applyLifecycle(LifecycleManager lifecycleManager) throws Exception
84      {
85          //by default do nothing
86      }
87  
88      /**
89       * Has this builder been configured already
90       * @return true if the {@link #configure(org.mule.api.MuleContext)} method has been called
91       */
92      public boolean isConfigured()
93      {
94          return configured;
95      }
96  }