View Javadoc

1   /*
2    * $Id: SpringConfigurationBuilder.java 9381 2007-10-25 20:36:15Z aperepel $
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.extras.spring.config;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.ConfigurationBuilder;
15  import org.mule.config.ConfigurationException;
16  import org.mule.config.MuleProperties;
17  import org.mule.config.ReaderResource;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.umo.UMOException;
20  import org.mule.umo.manager.UMOManager;
21  import org.mule.util.ArrayUtils;
22  import org.mule.util.PropertiesUtils;
23  import org.mule.util.StringUtils;
24  
25  import java.io.IOException;
26  import java.util.Properties;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * <code>SpringConfigurationBuilder</code> Enables Mule to be loaded from as Spring
33   * context. Multiple configuration files can be loaded from this builder (specified
34   * as a comma-separated list) the files can be String Beans documents or Mule Xml
35   * Documents or a combination of both. Any Mule Xml documents will be transformed at
36   * run-time in to Spring Bean documents before the bean definitions are loaded. Make
37   * sure that the DTD definitions for each of the document types are declared in the
38   * documents.
39   */
40  public class SpringConfigurationBuilder implements ConfigurationBuilder
41  {
42      protected transient final Log logger = LogFactory.getLog(getClass());
43      private MuleApplicationContext muleApplicationContext;
44  
45      /**
46       * Will configure a UMOManager based on the configurations made available through
47       * Readers.
48       * 
49       * @param configResources an array of Readers
50       * @return A configured UMOManager
51       * @throws org.mule.config.ConfigurationException
52       */
53      public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
54      {
55          // just in case it's ever implemented
56          return configure(configResources, null);
57      }
58  
59      /**
60       * Will configure a UMOManager based on the configurations made available through
61       * Readers.
62       * 
63       * @param configResources an array of Readers
64       * @return A configured UMOManager
65       * @throws org.mule.config.ConfigurationException
66       */
67      public UMOManager configure(ReaderResource[] configResources, Properties startupProperties)
68          throws ConfigurationException
69      {
70          throw new UnsupportedOperationException("Not implemented");
71      }
72  
73      public UMOManager configure(String configResources) throws ConfigurationException
74      {
75          return configure(configResources, null);
76      }
77  
78      public UMOManager configure(String configResources, String startupPropertiesFile)
79          throws ConfigurationException
80      {
81          // Load startup properties if any.
82          if (StringUtils.isNotBlank(startupPropertiesFile))
83          {
84              try
85              {
86                  startupPropertiesFile = StringUtils.trimToEmpty(startupPropertiesFile);
87                  Properties startupProperties = PropertiesUtils.loadProperties(startupPropertiesFile,
88                      getClass());
89                  ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
90              }
91              catch (IOException e)
92              {
93                  throw new ConfigurationException(
94                      CoreMessages.failedToStart("Mule server from builder"), e);
95              }
96          }
97  
98          String[] resources = StringUtils.splitAndTrim(configResources, ",");
99          if (logger.isDebugEnabled())
100         {
101             logger.debug("There is/are " + resources.length + " configuration resource(s): " + ArrayUtils.toString(resources));
102         }
103         MuleManager.getConfiguration().setConfigResources(resources);
104 
105         muleApplicationContext = new MuleApplicationContext(resources);
106 
107         try
108         {
109             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
110                 .equalsIgnoreCase("true"))
111             {
112                 MuleManager.getInstance().start();
113             }
114         }
115         catch (UMOException e)
116         {
117             throw new ConfigurationException(CoreMessages.failedToStart("Mule server from builder"), e);
118         }
119         return MuleManager.getInstance();
120     }
121 
122     /**
123      * Indicate whether this ConfigurationBulder has been configured yet
124      * 
125      * @return <code>true</code> if this ConfigurationBulder has been configured
126      */
127     public boolean isConfigured()
128     {
129         return MuleManager.isInstanciated();
130     }
131 
132     public MuleApplicationContext getMuleApplicationContext()
133     {
134         return muleApplicationContext;
135     }
136 
137 }