View Javadoc

1   /*
2    * $Id: SpringXmlConfigurationBuilder.java 10761 2008-02-10 21:43:57Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.ConfigResource;
17  import org.mule.config.builders.AbstractResourceConfigurationBuilder;
18  
19  import org.springframework.beans.BeansException;
20  import org.springframework.context.ApplicationContext;
21  
22  /**
23   * <code>SpringXmlConfigurationBuilder</code> enables Mule to be configured from a
24   * Spring XML Configuration file used with Mule name-spaces. Multiple configuration
25   * files can be loaded from this builder (specified as a comma-separated list).
26   */
27  public class SpringXmlConfigurationBuilder extends AbstractResourceConfigurationBuilder
28  {
29      protected String defaultConfigResourceName = "default-mule-config.xml";
30  
31      protected ApplicationContext parentContext;
32  
33      public SpringXmlConfigurationBuilder(String configResources, ApplicationContext parentContext) throws ConfigurationException
34      {
35          super(configResources);
36          this.parentContext = parentContext;
37      }
38  
39      public SpringXmlConfigurationBuilder(String[] configResources, ApplicationContext parentContext) throws ConfigurationException
40      {
41          super(configResources);
42          this.parentContext = parentContext;
43      }
44  
45      public SpringXmlConfigurationBuilder(String[] configResources) throws ConfigurationException
46      {
47          this(configResources, null);
48      }
49  
50      public SpringXmlConfigurationBuilder(String configResources) throws ConfigurationException
51      {
52          this(configResources, null);
53      }
54  
55      public SpringXmlConfigurationBuilder(ConfigResource[] configResources, ApplicationContext parentContext)
56      {
57          super(configResources);
58          this.parentContext = parentContext;
59      }
60  
61      public SpringXmlConfigurationBuilder(ConfigResource[] configResources)
62      {
63          this(configResources, null);
64      }
65  
66      protected void doConfigure(MuleContext muleContext) throws Exception
67      {
68          ConfigResource[] all = new ConfigResource[configResources.length + 1];
69          all[0] = new ConfigResource(defaultConfigResourceName);
70          System.arraycopy(configResources, 0, all, 1, configResources.length);
71          createSpringParentRegistry(muleContext, muleContext.getRegistry(), all);
72      }
73  
74      /**
75       * Creates a Spring ApplicationContext from the configuration resources provided
76       * and sets it as the parent Registry. This releationshio is setup with the
77       * MuleApplicationContext constructor to ensure that the Registry can be used
78       * during the initialization phase of Spring.
79       * 
80       * @param muleContext
81       * @param registry
82       * @param all
83       * @see MuleApplicationContext#setupParentSpringRegistry(Registry registry
84       */
85      protected void createSpringParentRegistry(MuleContext muleContext, Registry registry, ConfigResource[] all)
86      {
87          try
88          {
89              if (parentContext != null)
90              {
91                  new MuleApplicationContext(muleContext, registry, all, parentContext);
92              }
93              else
94              {
95                  new MuleApplicationContext(muleContext, registry, all);
96              }
97          }
98          catch (BeansException e)
99          {
100             // If creation of MuleApplicationContext fails, remove
101             // TransientRegistry->SpringRegistry parent relationship
102             registry.setParent(null);
103             throw e;
104         }
105     }
106 
107     public void setDefaultConfigResourceName(String defaultConfigResourceName)
108     {
109         this.defaultConfigResourceName = defaultConfigResourceName;
110     }
111 
112     public void setParentContext(ApplicationContext parentContext)
113     {
114         this.parentContext = parentContext;
115     }
116 
117 }