View Javadoc

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