View Javadoc

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