View Javadoc

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