View Javadoc

1   /*
2    * $Id: SpringContainerContext.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;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.ConfigurationException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.config.i18n.MessageFactory;
17  import org.mule.extras.spring.config.CachedResource;
18  import org.mule.extras.spring.config.MuleApplicationContext;
19  import org.mule.impl.container.AbstractContainerContext;
20  import org.mule.umo.lifecycle.InitialisationException;
21  import org.mule.umo.manager.ContainerException;
22  import org.mule.umo.manager.ObjectNotFoundException;
23  import org.mule.util.ArrayUtils;
24  import org.mule.util.StringUtils;
25  
26  import java.io.IOException;
27  import java.io.Reader;
28  import java.io.UnsupportedEncodingException;
29  
30  import org.springframework.beans.BeansException;
31  import org.springframework.beans.factory.BeanFactory;
32  import org.springframework.beans.factory.BeanFactoryAware;
33  import org.springframework.context.ConfigurableApplicationContext;
34  import org.springframework.core.io.Resource;
35  
36  /**
37   * <code>SpringContainerContext</code> is a Spring Context that can expose
38   * spring-managed components for use in the Mule framework.
39   */
40  public class SpringContainerContext extends AbstractContainerContext implements BeanFactoryAware
41  {
42      public static final String SPRING_DOCTYPE_REF = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">\n";
43  
44      /**
45       * the application context to use when resolving components
46       */
47      protected BeanFactory beanFactory;
48  
49      protected BeanFactory externalBeanFactory;
50  
51      /** One or more Spring XML config files */
52      protected String configResources;
53  
54      /** The Spring XML itself */
55      protected String configXml;
56  
57      public SpringContainerContext()
58      {
59          super("spring");
60      }
61  
62      public Object getComponent(Object key) throws ObjectNotFoundException
63      {
64          if (getBeanFactory() == null)
65          {
66              throw new IllegalStateException("Spring Application context has not been set");
67          }
68          if (key == null)
69          {
70              throw new ObjectNotFoundException("Component not found for null key");
71          }
72  
73          if (key instanceof Class)
74          {
75              // We will assume that there should only be one object of
76              // this class in the container for now
77              // String[] names = getBeanFactory().getBeanDefinitionNames((Class)
78              // key);
79              // if (names == null || names.length == 0 || names.length > 1)
80              // {
81              throw new ObjectNotFoundException("The container is unable to build single instance of "
82                                                + ((Class)key).getName() + " number of instances found was: 0");
83              // }
84              // else
85              // {
86              // key = names[0];
87              // }
88          }
89          try
90          {
91              return getBeanFactory().getBean(key.toString());
92          }
93          catch (BeansException e)
94          {
95              throw new ObjectNotFoundException("Component not found for key: " + key.toString(), e);
96          }
97      }
98  
99      //@Override
100     public void configure(Reader reader) throws ContainerException
101     {
102         Resource[] resources;
103         try 
104         {
105             resources = new Resource[]{new CachedResource(reader, MuleManager.getConfiguration().getEncoding())};
106         }
107         catch (IOException e)
108         {
109             throw new ContainerException(MessageFactory.createStaticMessage("Unable to read resource"), e);
110         }
111         setExternalBeanFactory(new MuleApplicationContext(resources));
112     }
113 
114     public void initialise() throws InitialisationException
115     {
116         // Load Spring XML in-memory
117         if (configXml != null)
118         {
119             final String encoding = MuleManager.getConfiguration().getEncoding();
120             Resource[] resources;
121             try 
122             {
123                 resources = new Resource[]{new CachedResource(configXml, MuleManager.getConfiguration().getEncoding())};
124             }
125             catch (UnsupportedEncodingException e)
126             {
127                 throw new InitialisationException(CoreMessages.failedToConvertStringUsingEncoding(encoding), e);
128             }
129             setExternalBeanFactory(new MuleApplicationContext(resources));
130         }
131 
132         // Load Spring XML from one or more config files
133         else if (configResources != null)
134         {        
135             String[] resources = StringUtils.splitAndTrim(configResources, ",");
136             if (logger.isDebugEnabled())
137             {
138                 logger.debug("There is/are " + resources.length + " configuration resource(s): " + ArrayUtils.toString(resources));
139             }
140             setExternalBeanFactory(new MuleApplicationContext(resources));
141         }
142     }
143 
144     public void dispose()
145     {
146         if (externalBeanFactory instanceof ConfigurableApplicationContext)
147         {
148             ((ConfigurableApplicationContext)externalBeanFactory).close();
149         }
150         super.dispose();
151     }
152 
153     ///////////////////////////////////////////////////////////////////////////////////////////
154     // Getters and Setters
155     ///////////////////////////////////////////////////////////////////////////////////////////
156     
157     /**
158      * The spring application context used to build components
159      * 
160      * @return spring application context
161      */
162     public BeanFactory getBeanFactory()
163     {
164         if (externalBeanFactory != null)
165         {
166             return externalBeanFactory;
167         }
168         return beanFactory;
169     }
170 
171     /**
172      * Sets the spring application context used to build components
173      * 
174      * @param beanFactory the context to use
175      */
176     public void setBeanFactory(BeanFactory beanFactory)
177     {
178         this.beanFactory = beanFactory;
179     }
180 
181     public void setExternalBeanFactory(BeanFactory factory)
182     {
183         this.externalBeanFactory = factory;
184     }
185 
186     /** The Spring XML itself */
187     public String getConfigXml()
188     {
189         return configXml;
190     }
191 
192     /** The Spring XML itself */
193     public void setConfigXml(String configXml)
194     {
195         this.configXml = configXml;
196     }
197 
198     /** 
199      * The Spring XML itself.
200      * @deprecated use getConfigXml() instead 
201      */
202     public String getConfiguration()
203     {
204         return configXml;
205     }
206 
207     /** 
208      * The Spring XML itself.
209      * @deprecated use setConfigXml() instead 
210      */
211     public void setConfiguration(String configuration)
212     {
213         this.configXml = configuration;
214     }
215 
216     /** One or more Spring XML config files */
217     public String getConfigResources()
218     {
219         return configResources;
220     }
221 
222     /** One or more Spring XML config files */
223     public void setConfigResources(String configResources)
224     {
225         this.configResources = configResources;
226     }
227 
228     /**
229      * @deprecated use getConfigResources() instead
230      */
231     public String getConfigFile()
232     {
233         return configResources;
234     }
235 
236     /**
237      * @deprecated use setConfigResources() instead
238      */
239     public void setConfigFile(String configFile) throws ConfigurationException
240     {
241         this.configResources = configFile;
242     }
243 }