View Javadoc

1   /*
2    * $Id: SpringConfigurationBuilder.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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      
44      /**
45       * Will configure a UMOManager based on the configurations made available through
46       * Readers.
47       * 
48       * @param configResources an array of Readers
49       * @return A configured UMOManager
50       * @throws org.mule.config.ConfigurationException
51       */
52      public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
53      {
54          // just in case it's ever implemented
55          return configure(configResources, null);
56      }
57  
58      /**
59       * Will configure a UMOManager based on the configurations made available through
60       * Readers.
61       * 
62       * @param configResources an array of Readers
63       * @return A configured UMOManager
64       * @throws org.mule.config.ConfigurationException
65       */
66      public UMOManager configure(ReaderResource[] configResources, Properties startupProperties)
67          throws ConfigurationException
68      {
69          throw new UnsupportedOperationException("Not implemented");
70      }
71  
72      public UMOManager configure(String configResources) throws ConfigurationException
73      {
74          return configure(configResources, null);
75      }
76  
77      public UMOManager configure(String configResources, String startupPropertiesFile)
78          throws ConfigurationException
79      {
80          // Load startup properties if any.
81          if (StringUtils.isNotBlank(startupPropertiesFile))
82          {
83              try
84              {
85                  startupPropertiesFile = StringUtils.trimToEmpty(startupPropertiesFile);
86                  Properties startupProperties = PropertiesUtils.loadProperties(startupPropertiesFile,
87                      getClass());
88                  ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
89              }
90              catch (IOException e)
91              {
92                  throw new ConfigurationException(
93                      CoreMessages.failedToStart("Mule server from builder"), e);
94              }
95          }
96  
97          String[] resources = StringUtils.splitAndTrim(configResources, ",");
98          if (logger.isDebugEnabled())
99          {
100             logger.debug("There is/are " + resources.length + " configuration resource(s): " + ArrayUtils.toString(resources));
101         }
102         MuleManager.getConfiguration().setConfigResources(resources);
103 
104         new MuleApplicationContext(resources);
105         try
106         {
107             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
108                 .equalsIgnoreCase("true"))
109             {
110                 MuleManager.getInstance().start();
111             }
112         }
113         catch (UMOException e)
114         {
115             throw new ConfigurationException(CoreMessages.failedToStart("Mule server from builder"), e);
116         }
117         return MuleManager.getInstance();
118     }
119 
120     /**
121      * Indicate whether this ConfigurationBulder has been configured yet
122      * 
123      * @return <code>true</code> if this ConfigurationBulder has been configured
124      */
125     public boolean isConfigured()
126     {
127         return MuleManager.isInstanciated();
128     }
129 }