View Javadoc

1   /*
2    * $Id: DefaultMuleContextFactory.java 21877 2011-05-11 13:45:17Z 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.context;
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.config.MuleConfiguration;
17  import org.mule.api.context.MuleContextBuilder;
18  import org.mule.api.context.MuleContextFactory;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.config.DefaultMuleConfiguration;
21  import org.mule.config.builders.AutoConfigurationBuilder;
22  import org.mule.config.builders.DefaultsConfigurationBuilder;
23  import org.mule.config.builders.SimpleConfigurationBuilder;
24  
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * Default implementation that uses {@link DefaultMuleContextBuilder} to build new 
33   * {@link MuleContext} instances.
34   */
35  public class DefaultMuleContextFactory implements MuleContextFactory
36  {
37      protected static final Log logger = LogFactory.getLog(DefaultMuleContextBuilder.class);
38  
39      /**
40       * Use default ConfigurationBuilder, default MuleContextBuilder
41       */
42      public MuleContext createMuleContext() throws InitialisationException, ConfigurationException
43      {
44          // Configure with defaults needed for a feasible/startable MuleContext
45          return createMuleContext(new DefaultsConfigurationBuilder(), new DefaultMuleContextBuilder());
46      }
47  
48      /**
49       * Use default MuleContextBuilder
50       */
51      public MuleContext createMuleContext(ConfigurationBuilder configurationBuilder)
52          throws InitialisationException, ConfigurationException
53      {
54          // Create MuleContext using default MuleContextBuilder
55          return createMuleContext(configurationBuilder, new DefaultMuleContextBuilder());
56      }
57  
58      /**
59       * Use default ConfigurationBuilder
60       */
61      public MuleContext createMuleContext(MuleContextBuilder muleContextBuilder)
62          throws InitialisationException, ConfigurationException
63      {
64          // Configure with defaults needed for a feasible/startable MuleContext
65          return createMuleContext(new DefaultsConfigurationBuilder(), muleContextBuilder);
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public MuleContext createMuleContext(List<ConfigurationBuilder> configurationBuilders, 
72          MuleContextBuilder muleContextBuilder) throws InitialisationException, ConfigurationException
73      {
74          // Create MuleContext
75          MuleContext muleContext = doCreateMuleContext(muleContextBuilder);
76  
77          // Configure
78          for (ConfigurationBuilder configBuilder : configurationBuilders)
79          {
80              configBuilder.configure(muleContext);
81          }
82  
83          return muleContext;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public MuleContext createMuleContext(ConfigurationBuilder configurationBuilder,
90                                           MuleContextBuilder muleContextBuilder)
91          throws InitialisationException, ConfigurationException
92      {
93          // Create MuleContext
94          MuleContext muleContext = doCreateMuleContext(muleContextBuilder);
95  
96          // Configure
97          configurationBuilder.configure(muleContext);
98  
99          return muleContext;
100     }
101 
102     // Additional Factory methods provided by this implementation.
103 
104     /**
105      * Creates a new {@link MuleContext} instance from the resource provided.
106      * Implementations of {@link MuleContextFactory} can either use a default
107      * {@link ConfigurationBuilder} to implement this, or do some auto-detection to
108      * determine the {@link ConfigurationBuilder} that should be used.
109      * 
110      * @param resource comma seperated list of configuration resources.
111      * @throws InitialisationException
112      * @throws ConfigurationException
113      */
114     public MuleContext createMuleContext(String resource)
115         throws InitialisationException, ConfigurationException
116     {
117         return createMuleContext(resource, null);
118     }
119 
120     /**
121      * Creates a new {@link MuleContext} instance from the resource provided.
122      * Implementations of {@link MuleContextFactory} can either use a default
123      * {@link ConfigurationBuilder} to implement this, or do some auto-detection to
124      * determine the {@link ConfigurationBuilder} that should be used. Properties if
125      * provided are used to replace "property placeholder" value in configuration
126      * files.
127      */
128     public MuleContext createMuleContext(String configResources, Properties properties)
129         throws InitialisationException, ConfigurationException
130     {
131         // Create MuleContext
132         MuleContext muleContext = doCreateMuleContext(new DefaultMuleContextBuilder());
133 
134         // Configure with startup properties
135         if (properties != null && !properties.isEmpty())
136         {
137             new SimpleConfigurationBuilder(properties).configure(muleContext);
138         }
139 
140         // Automatically resolve Configuration to be used and delegate configuration
141         // to it.
142         new AutoConfigurationBuilder(configResources).configure(muleContext);
143 
144         return muleContext;
145     }
146 
147     /**
148      * Creates a new MuleContext using the given configurationBuilder. Properties if
149      * provided are used to replace "property placeholder" value in configuration
150      * files.
151      */
152     public MuleContext createMuleContext(ConfigurationBuilder configurationBuilder, Properties properties)
153         throws InitialisationException, ConfigurationException
154     {
155         return createMuleContext(configurationBuilder, properties, new DefaultMuleConfiguration());
156     }
157     
158     /**
159      * Creates a new MuleContext using the given configurationBuilder and configuration. Properties if
160      * provided are used to replace "property placeholder" value in configuration
161      * files.
162      */
163     public MuleContext createMuleContext(ConfigurationBuilder configurationBuilder,
164                                          Properties properties,
165                                          MuleConfiguration configuration)
166         throws InitialisationException, ConfigurationException
167     {
168         // Create MuleContext
169         DefaultMuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
170         contextBuilder.setMuleConfiguration(configuration);
171         MuleContext muleContext = doCreateMuleContext(contextBuilder);
172 
173         // Configure with startup properties
174         if (properties != null && !properties.isEmpty())
175         {
176             new SimpleConfigurationBuilder(properties).configure(muleContext);
177         }
178 
179         // Configure with cconfigurationBuilder
180         configurationBuilder.configure(muleContext);
181 
182         return muleContext;
183     }
184 
185     protected MuleContext doCreateMuleContext(MuleContextBuilder muleContextBuilder)
186         throws InitialisationException
187     {
188         // Create muleContext instance and set it in MuleServer
189         MuleContext muleContext = buildMuleContext(muleContextBuilder);
190 
191         // Initialiase MuleContext
192         muleContext.initialise();
193 
194         return muleContext;
195     }
196 
197     protected MuleContext buildMuleContext(MuleContextBuilder muleContextBuilder)
198     {
199         return muleContextBuilder.buildMuleContext();
200     }
201 }