001: /*
002: * $Id: SpringXmlConfigurationBuilder.java 10761 2008-02-10 21:43:57Z rossmason $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring;
012:
013: import org.mule.api.MuleContext;
014: import org.mule.api.config.ConfigurationException;
015: import org.mule.api.registry.Registry;
016: import org.mule.config.ConfigResource;
017: import org.mule.config.builders.AbstractResourceConfigurationBuilder;
018:
019: import org.springframework.beans.BeansException;
020: import org.springframework.context.ApplicationContext;
021:
022: /**
023: * <code>SpringXmlConfigurationBuilder</code> enables Mule to be configured from a
024: * Spring XML Configuration file used with Mule name-spaces. Multiple configuration
025: * files can be loaded from this builder (specified as a comma-separated list).
026: */
027: public class SpringXmlConfigurationBuilder extends
028: AbstractResourceConfigurationBuilder {
029: protected String defaultConfigResourceName = "default-mule-config.xml";
030:
031: protected ApplicationContext parentContext;
032:
033: public SpringXmlConfigurationBuilder(String configResources,
034: ApplicationContext parentContext)
035: throws ConfigurationException {
036: super (configResources);
037: this .parentContext = parentContext;
038: }
039:
040: public SpringXmlConfigurationBuilder(String[] configResources,
041: ApplicationContext parentContext)
042: throws ConfigurationException {
043: super (configResources);
044: this .parentContext = parentContext;
045: }
046:
047: public SpringXmlConfigurationBuilder(String[] configResources)
048: throws ConfigurationException {
049: this (configResources, null);
050: }
051:
052: public SpringXmlConfigurationBuilder(String configResources)
053: throws ConfigurationException {
054: this (configResources, null);
055: }
056:
057: public SpringXmlConfigurationBuilder(
058: ConfigResource[] configResources,
059: ApplicationContext parentContext) {
060: super (configResources);
061: this .parentContext = parentContext;
062: }
063:
064: public SpringXmlConfigurationBuilder(
065: ConfigResource[] configResources) {
066: this (configResources, null);
067: }
068:
069: protected void doConfigure(MuleContext muleContext)
070: throws Exception {
071: ConfigResource[] all = new ConfigResource[configResources.length + 1];
072: all[0] = new ConfigResource(defaultConfigResourceName);
073: System.arraycopy(configResources, 0, all, 1,
074: configResources.length);
075: createSpringParentRegistry(muleContext, muleContext
076: .getRegistry(), all);
077: }
078:
079: /**
080: * Creates a Spring ApplicationContext from the configuration resources provided
081: * and sets it as the parent Registry. This releationshio is setup with the
082: * MuleApplicationContext constructor to ensure that the Registry can be used
083: * during the initialization phase of Spring.
084: *
085: * @param muleContext
086: * @param registry
087: * @param all
088: * @see MuleApplicationContext#setupParentSpringRegistry(Registry registry
089: */
090: protected void createSpringParentRegistry(MuleContext muleContext,
091: Registry registry, ConfigResource[] all) {
092: try {
093: if (parentContext != null) {
094: new MuleApplicationContext(muleContext, registry, all,
095: parentContext);
096: } else {
097: new MuleApplicationContext(muleContext, registry, all);
098: }
099: } catch (BeansException e) {
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(
108: String defaultConfigResourceName) {
109: this .defaultConfigResourceName = defaultConfigResourceName;
110: }
111:
112: public void setParentContext(ApplicationContext parentContext) {
113: this.parentContext = parentContext;
114: }
115:
116: }
|