View Javadoc

1   /*
2    * $Id: SpringConfigurationBuilder.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.spring;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ConfigurationException;
15  import org.mule.api.registry.Registry;
16  import org.mule.config.builders.AbstractConfigurationBuilder;
17  import org.mule.config.i18n.MessageFactory;
18  
19  import org.springframework.context.ApplicationContext;
20  import org.springframework.context.ConfigurableApplicationContext;
21  
22  /**
23   * Adds an existing Spring ApplicationContext to Mule's internal collection of Registries.
24   */
25  public class SpringConfigurationBuilder extends AbstractConfigurationBuilder
26  {
27      private ApplicationContext appContext;
28  
29      private ApplicationContext parentContext;
30      
31      public SpringConfigurationBuilder(ApplicationContext appContext)
32      {
33          this.appContext = appContext;
34      }
35  
36      public SpringConfigurationBuilder(ConfigurableApplicationContext appContext, ApplicationContext parentContext)
37      {
38          this.appContext = appContext;
39          this.parentContext = parentContext;
40      }
41  
42      protected void doConfigure(MuleContext muleContext) throws Exception
43      {
44          Registry registry;
45          
46          if (parentContext != null)
47          {
48              if (appContext instanceof ConfigurableApplicationContext)
49              {
50                  registry = new SpringRegistry((ConfigurableApplicationContext) appContext, parentContext, muleContext);
51              }
52              else
53              {
54                  throw new ConfigurationException(MessageFactory.createStaticMessage("Cannot set a parent context if the ApplicationContext does not implement ConfigurableApplicationContext"));
55              }
56          }
57          else
58          {
59              registry = new SpringRegistry(appContext, muleContext);
60          }
61  
62          // Note: The SpringRegistry must be created before applicationContext.refresh() gets called because
63          // some beans may try to look up other beans via the Registry during preInstantiateSingletons().
64          muleContext.addRegistry(registry);
65          registry.initialise();
66      }
67  
68  }