KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > config > SpringConfigurationBuilder


1 /*
2  * $Id: SpringConfigurationBuilder.java 3798 2006-11-04 04:07:14Z 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 MuleSource MPL
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 java.io.IOException JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import org.mule.MuleManager;
17 import org.mule.config.ConfigurationBuilder;
18 import org.mule.config.ConfigurationException;
19 import org.mule.config.MuleProperties;
20 import org.mule.config.ReaderResource;
21 import org.mule.config.i18n.Message;
22 import org.mule.config.i18n.Messages;
23 import org.mule.umo.UMOException;
24 import org.mule.umo.manager.UMOManager;
25 import org.mule.util.PropertiesUtils;
26 import org.mule.util.StringUtils;
27
28 /**
29  * <code>SpringConfigurationBuilder</code> Enables Mule to be loaded from as Spring
30  * context. Multiple configuration files can be loaded from this builder (specified
31  * as a comma-separated list) the files can be String Beans documents or Mule Xml
32  * Documents or a combination of both. Any Mule Xml documents will be transformed at
33  * run-time in to Spring Bean documents before the bean definitions are loaded. Make
34  * sure that the DTD definitions for each of the document types are declared in the
35  * documents.
36  */

37 public class SpringConfigurationBuilder implements ConfigurationBuilder
38 {
39     /**
40      * Will configure a UMOManager based on the configurations made available through
41      * Readers.
42      *
43      * @param configResources an array of Readers
44      * @return A configured UMOManager
45      * @throws org.mule.config.ConfigurationException
46      */

47     public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
48     {
49         // just in case it's ever implemented
50
return configure(configResources, null);
51     }
52
53     /**
54      * Will configure a UMOManager based on the configurations made available through
55      * Readers.
56      *
57      * @param configResources an array of Readers
58      * @return A configured UMOManager
59      * @throws org.mule.config.ConfigurationException
60      */

61     public UMOManager configure(ReaderResource[] configResources, Properties JavaDoc startupProperties)
62         throws ConfigurationException
63     {
64         throw new UnsupportedOperationException JavaDoc("Not implemented");
65     }
66
67     public UMOManager configure(String JavaDoc configResources) throws ConfigurationException
68     {
69         return configure(configResources, null);
70     }
71
72     public UMOManager configure(String JavaDoc configResource, String JavaDoc startupPropertiesFile)
73         throws ConfigurationException
74     {
75         // Load startup properties if any.
76
if (StringUtils.isNotBlank(startupPropertiesFile))
77         {
78             try
79             {
80                 startupPropertiesFile = StringUtils.trimToEmpty(startupPropertiesFile);
81                 Properties JavaDoc startupProperties = PropertiesUtils.loadProperties(startupPropertiesFile,
82                     getClass());
83                 ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
84             }
85             catch (IOException JavaDoc e)
86             {
87                 throw new ConfigurationException(new Message(Messages.FAILED_TO_START_X,
88                     "Mule server from builder"), e);
89             }
90         }
91
92         if (configResource == null)
93         {
94             throw new ConfigurationException(new Message(Messages.X_IS_NULL, "Configuration Resource"));
95         }
96         String JavaDoc[] resources = org.springframework.util.StringUtils.tokenizeToStringArray(configResource, ",;",
97             true, true);
98
99         MuleManager.getConfiguration().setConfigResources(resources);
100         new MuleApplicationContext(resources);
101         try
102         {
103             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
104                 .equalsIgnoreCase("true"))
105             {
106                 MuleManager.getInstance().start();
107             }
108         }
109         catch (UMOException e)
110         {
111             throw new ConfigurationException(new Message(Messages.FAILED_TO_START_X,
112                 "Mule server from builder"), e);
113         }
114         return MuleManager.getInstance();
115     }
116
117     /**
118      * Indicate whether this ConfigurationBulder has been configured yet
119      *
120      * @return <code>true</code> if this ConfigurationBulder has been configured
121      */

122     public boolean isConfigured()
123     {
124         return MuleManager.isInstanciated();
125     }
126 }
127
Popular Tags