KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > SpringContainerContext


1 /*
2  * $Id: SpringContainerContext.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;
12
13 import java.io.Reader JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.springframework.beans.BeansException;
19 import org.springframework.beans.factory.BeanFactory;
20 import org.springframework.beans.factory.BeanFactoryAware;
21 import org.springframework.beans.factory.xml.XmlBeanFactory;
22 import org.springframework.context.ConfigurableApplicationContext;
23 import org.springframework.context.support.ClassPathXmlApplicationContext;
24 import org.springframework.context.support.FileSystemXmlApplicationContext;
25 import org.springframework.core.io.InputStreamResource;
26
27 import org.mule.MuleManager;
28 import org.mule.config.ConfigurationException;
29 import org.mule.config.i18n.CoreMessageConstants;
30 import org.mule.config.i18n.Message;
31 import org.mule.config.i18n.Messages;
32 import org.mule.extras.spring.config.CachedResource;
33 import org.mule.extras.spring.config.ReaderInputStream;
34 import org.mule.impl.container.AbstractContainerContext;
35 import org.mule.umo.lifecycle.InitialisationException;
36 import org.mule.umo.manager.ContainerException;
37 import org.mule.umo.manager.ObjectNotFoundException;
38 import org.mule.util.ClassUtils;
39
40 /**
41  * <code>SpringContainerContext</code> is a Spring Context that can expose
42  * spring-managed components for use in the Mule framework.
43  */

44 public class SpringContainerContext extends AbstractContainerContext implements BeanFactoryAware
45 {
46     public static final String JavaDoc 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";
47
48     /**
49      * logger used by this class
50      */

51     protected static Log logger = LogFactory.getLog(SpringContainerContext.class);
52
53     /**
54      * the application contect to use when resolving components
55      */

56     protected BeanFactory beanFactory;
57
58     protected BeanFactory externalBeanFactory;
59
60     protected String JavaDoc configFile;
61
62     protected String JavaDoc configuration = null;
63
64     public SpringContainerContext()
65     {
66         super("spring");
67     }
68
69     /**
70      * Sets the spring application context used to build components
71      *
72      * @param beanFactory the context to use
73      */

74     public void setBeanFactory(BeanFactory beanFactory)
75     {
76         this.beanFactory = beanFactory;
77     }
78
79     public void setExternalBeanFactory(BeanFactory factory)
80     {
81         this.externalBeanFactory = factory;
82     }
83
84     /**
85      * The spring application context used to build components
86      *
87      * @return spring application context
88      */

89     public BeanFactory getBeanFactory()
90     {
91         if (externalBeanFactory != null)
92         {
93             return externalBeanFactory;
94         }
95         return beanFactory;
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.mule.umo.model.UMOContainerContext#getComponent(java.lang.Object)
102      */

103     public Object JavaDoc getComponent(Object JavaDoc key) throws ObjectNotFoundException
104     {
105         if (getBeanFactory() == null)
106         {
107             throw new IllegalStateException JavaDoc("Spring Application context has not been set");
108         }
109         if (key == null)
110         {
111             throw new ObjectNotFoundException("Component not found for null key");
112         }
113
114         if (key instanceof Class JavaDoc)
115         {
116             // We will assume that there should only be one object of
117
// this class in the container for now
118
// String[] names = getBeanFactory().getBeanDefinitionNames((Class)
119
// key);
120
// if (names == null || names.length == 0 || names.length > 1)
121
// {
122
throw new ObjectNotFoundException("The container is unable to build single instance of "
123                                               + ((Class JavaDoc)key).getName() + " number of instances found was: 0");
124             // }
125
// else
126
// {
127
// key = names[0];
128
// }
129
}
130         try
131         {
132             return getBeanFactory().getBean(key.toString());
133         }
134         catch (BeansException e)
135         {
136             throw new ObjectNotFoundException("Component not found for key: " + key.toString(), e);
137         }
138     }
139
140     public String JavaDoc getConfigFile()
141     {
142         return configFile;
143     }
144
145     /**
146      * @param configFile The configFile to set.
147      */

148     public void setConfigFile(String JavaDoc configFile) throws ConfigurationException
149     {
150         this.configFile = configFile;
151     }
152
153     public void configure(Reader JavaDoc configuration) throws ContainerException
154     {
155         BeanFactory bf = new XmlBeanFactory(new InputStreamResource(new ReaderInputStream(configuration)));
156         setExternalBeanFactory(bf);
157     }
158
159     /**
160      * Configure Spring by passing an in-memory XML Spring config.
161      *
162      * @param configurationXmlAsString XML config contents
163      * @throws ContainerException in case of any error
164      */

165     public void configure(String JavaDoc configurationXmlAsString) throws ContainerException
166     {
167         final String JavaDoc encoding = MuleManager.getConfiguration().getEncoding();
168         try
169         {
170             BeanFactory bf = new XmlBeanFactory(new CachedResource(configurationXmlAsString, encoding));
171             setExternalBeanFactory(bf);
172         }
173         catch (UnsupportedEncodingException JavaDoc e)
174         {
175             final Message message = new Message("core",
176                 CoreMessageConstants.FAILED_TO_CONVERT_STRING_USING_X_ENCODING, encoding);
177             throw new ContainerException(message, e);
178         }
179     }
180
181     public void initialise() throws InitialisationException
182     {
183         if (configFile == null)
184         {
185             if (configuration != null)
186             {
187                 try
188                 {
189                     configure(configuration);
190                     return;
191                 }
192                 catch (ContainerException e)
193                 {
194                     throw new InitialisationException(e, this);
195                 }
196             }
197             else
198             {
199                 return;
200             }
201         }
202
203         try
204         {
205             if (ClassUtils.getResource(configFile, getClass()) == null)
206             {
207                 logger.warn("Spring config resource: " + configFile
208                             + " not found on class path, attempting to load it from local file");
209                 setExternalBeanFactory(new FileSystemXmlApplicationContext(configFile));
210             }
211             else
212             {
213                 logger.info("Loading Spring config from classpath, resource is: " + configFile);
214                 setExternalBeanFactory(new ClassPathXmlApplicationContext(configFile));
215             }
216         }
217         catch (BeansException e)
218         {
219             throw new InitialisationException(new ConfigurationException(new Message(Messages.FAILED_LOAD_X,
220                 "Application Context: " + configFile), e), this);
221         }
222     }
223
224     public void dispose()
225     {
226         if (externalBeanFactory instanceof ConfigurableApplicationContext)
227         {
228             ((ConfigurableApplicationContext)externalBeanFactory).close();
229         }
230         super.dispose();
231
232     }
233
234     public String JavaDoc getConfiguration()
235     {
236         return configuration;
237     }
238
239     public void setConfiguration(String JavaDoc configuration)
240     {
241         this.configuration = configuration;
242     }
243
244 }
245
Popular Tags