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