View Javadoc

1   /*
2    * $Id: SpringXmlConfigurationBuilder.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.lifecycle.LifecycleManager;
16  import org.mule.api.lifecycle.Startable;
17  import org.mule.api.registry.Registry;
18  import org.mule.config.ConfigResource;
19  import org.mule.config.builders.AbstractResourceConfigurationBuilder;
20  import org.mule.config.i18n.MessageFactory;
21  import org.springframework.context.ApplicationContext;
22  import org.springframework.context.ConfigurableApplicationContext;
23  
24  /**
25   * <code>SpringXmlConfigurationBuilder</code> enables Mule to be configured from a
26   * Spring XML Configuration file used with Mule name-spaces. Multiple configuration
27   * files can be loaded from this builder (specified as a comma-separated list).
28   */
29  public class SpringXmlConfigurationBuilder extends AbstractResourceConfigurationBuilder
30  {
31      public static final String MULE_DEFAULTS_CONFIG = "default-mule-config.xml";
32      public static final String MULE_SPRING_CONFIG = "mule-spring-config.xml";
33  
34      /** Prepend "default-mule-config.xml" to the list of config resources. */
35      protected boolean useDefaultConfigResource = true;
36  
37      protected Registry registry;
38  
39      protected ApplicationContext parentContext;
40  
41      public SpringXmlConfigurationBuilder(String[] configResources) throws ConfigurationException
42      {
43          super(configResources);
44      }
45  
46      public SpringXmlConfigurationBuilder(String configResources) throws ConfigurationException
47      {
48          super(configResources);
49      }
50  
51      public SpringXmlConfigurationBuilder(ConfigResource[] configResources)
52      {
53          super(configResources);
54      }
55  
56      @Override
57      protected void doConfigure(MuleContext muleContext) throws Exception
58      {
59          ConfigResource[] allResources;
60          if (useDefaultConfigResource)
61          {
62              allResources = new ConfigResource[configResources.length + 2];
63              allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
64              allResources[1] = new ConfigResource(MULE_DEFAULTS_CONFIG);
65              System.arraycopy(configResources, 0, allResources, 2, configResources.length);
66          }
67          else
68          {
69              allResources = new ConfigResource[configResources.length + 1];
70              allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
71              System.arraycopy(configResources, 0, allResources, 1, configResources.length);
72          }
73          createSpringRegistry(muleContext, createApplicationContext(muleContext, allResources));
74      }
75  
76      public void unconfigure(MuleContext muleContext)
77      {
78          registry.dispose();
79          muleContext.removeRegistry(registry);
80          registry = null;
81          configured = false;
82      }
83  
84      protected ApplicationContext createApplicationContext(MuleContext muleContext,
85                                                            ConfigResource[] configResources) throws Exception
86      {
87          return new MuleApplicationContext(muleContext, configResources);
88      }
89  
90      protected void createSpringRegistry(MuleContext muleContext, ApplicationContext applicationContext)
91          throws Exception
92      {
93          if (parentContext != null)
94          {
95              if (applicationContext instanceof ConfigurableApplicationContext)
96              {
97                  registry = new SpringRegistry((ConfigurableApplicationContext) applicationContext,
98                      parentContext, muleContext);
99              }
100             else
101             {
102                 throw new ConfigurationException(
103                     MessageFactory.createStaticMessage("Cannot set a parent context if the ApplicationContext does not implement ConfigurableApplicationContext"));
104             }
105         }
106         else
107         {
108             registry = new SpringRegistry(applicationContext, muleContext);
109         }
110 
111         // Note: The SpringRegistry must be created before
112         // applicationContext.refresh() gets called because
113         // some beans may try to look up other beans via the Registry during
114         // preInstantiateSingletons().
115         muleContext.addRegistry(registry);
116         registry.initialise();
117     }
118 
119     @Override
120     protected void applyLifecycle(LifecycleManager lifecycleManager) throws Exception
121     {
122         // If the MuleContext is started, start all objects in the new Registry.
123         if (lifecycleManager.isPhaseComplete(Startable.PHASE_NAME))
124         {
125             lifecycleManager.fireLifecycle(Startable.PHASE_NAME);
126         }
127     }
128 
129     public boolean isUseDefaultConfigResource()
130     {
131         return useDefaultConfigResource;
132     }
133 
134     public void setUseDefaultConfigResource(boolean useDefaultConfigResource)
135     {
136         this.useDefaultConfigResource = useDefaultConfigResource;
137     }
138 
139     protected ApplicationContext getParentContext()
140     {
141         return parentContext;
142     }
143 
144     public void setParentContext(ApplicationContext parentContext)
145     {
146         this.parentContext = parentContext;
147     }
148 
149 }