1 10 11 package org.mule.extras.spring; 12 13 import java.io.Reader ; 14 import java.io.UnsupportedEncodingException ; 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 44 public class SpringContainerContext extends AbstractContainerContext implements BeanFactoryAware 45 { 46 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"; 47 48 51 protected static Log logger = LogFactory.getLog(SpringContainerContext.class); 52 53 56 protected BeanFactory beanFactory; 57 58 protected BeanFactory externalBeanFactory; 59 60 protected String configFile; 61 62 protected String configuration = null; 63 64 public SpringContainerContext() 65 { 66 super("spring"); 67 } 68 69 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 89 public BeanFactory getBeanFactory() 90 { 91 if (externalBeanFactory != null) 92 { 93 return externalBeanFactory; 94 } 95 return beanFactory; 96 } 97 98 103 public Object getComponent(Object key) throws ObjectNotFoundException 104 { 105 if (getBeanFactory() == null) 106 { 107 throw new IllegalStateException ("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 ) 115 { 116 throw new ObjectNotFoundException("The container is unable to build single instance of " 123 + ((Class )key).getName() + " number of instances found was: 0"); 124 } 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 getConfigFile() 141 { 142 return configFile; 143 } 144 145 148 public void setConfigFile(String configFile) throws ConfigurationException 149 { 150 this.configFile = configFile; 151 } 152 153 public void configure(Reader configuration) throws ContainerException 154 { 155 BeanFactory bf = new XmlBeanFactory(new InputStreamResource(new ReaderInputStream(configuration))); 156 setExternalBeanFactory(bf); 157 } 158 159 165 public void configure(String configurationXmlAsString) throws ContainerException 166 { 167 final String encoding = MuleManager.getConfiguration().getEncoding(); 168 try 169 { 170 BeanFactory bf = new XmlBeanFactory(new CachedResource(configurationXmlAsString, encoding)); 171 setExternalBeanFactory(bf); 172 } 173 catch (UnsupportedEncodingException 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 getConfiguration() 235 { 236 return configuration; 237 } 238 239 public void setConfiguration(String configuration) 240 { 241 this.configuration = configuration; 242 } 243 244 } 245 | Popular Tags |