1 10 11 package org.mule.extras.spring.config; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.MuleManager; 16 import org.mule.config.MuleConfiguration; 17 import org.mule.extras.spring.SpringContainerContext; 18 import org.mule.impl.model.ModelFactory; 19 import org.mule.umo.UMODescriptor; 20 import org.mule.umo.UMOException; 21 import org.mule.umo.UMOInterceptorStack; 22 import org.mule.umo.endpoint.UMOEndpoint; 23 import org.mule.umo.lifecycle.InitialisationException; 24 import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory; 25 import org.mule.umo.manager.UMOAgent; 26 import org.mule.umo.manager.UMOContainerContext; 27 import org.mule.umo.manager.UMOManager; 28 import org.mule.umo.manager.UMOTransactionManagerFactory; 29 import org.mule.umo.model.UMOEntryPointResolver; 30 import org.mule.umo.model.UMOModel; 31 import org.mule.umo.provider.UMOConnector; 32 import org.mule.umo.security.UMOSecurityManager; 33 import org.mule.umo.transformer.UMOTransformer; 34 import org.springframework.beans.BeansException; 35 import org.springframework.beans.factory.BeanInitializationException; 36 import org.springframework.beans.factory.DisposableBean; 37 import org.springframework.beans.factory.FactoryBean; 38 import org.springframework.beans.factory.InitializingBean; 39 import org.springframework.context.ApplicationContext; 40 import org.springframework.context.ApplicationContextAware; 41 import org.springframework.context.support.AbstractApplicationContext; 42 43 import java.beans.ExceptionListener ; 44 import java.util.Collection ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 48 75 public class AutowireUMOManagerFactoryBean 76 implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware 77 { 78 81 protected static Log logger = LogFactory.getLog(AutowireUMOManagerFactoryBean.class); 82 83 public static final String MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME = "muleEnvironmentProperties"; 84 public static final String MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME = "muleEndpointIdentifiers"; 85 public static final String MULE_INTERCEPTOR_STACK_BEAN_NAME = "muleInterceptorStacks"; 86 public static final String MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME = "muleModelExceptionStrategy"; 87 88 private UMOManager manager; 89 90 private AbstractApplicationContext context; 91 92 public AutowireUMOManagerFactoryBean() throws Exception 93 { 94 this.manager = MuleManager.getInstance(); 95 } 96 97 public Object getObject() throws Exception 98 { 99 return manager; 100 } 101 102 public Class getObjectType() 103 { 104 return UMOManager.class; 105 } 106 107 public boolean isSingleton() 108 { 109 return true; 110 } 111 112 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 113 { 114 context = (AbstractApplicationContext)applicationContext; 115 try 116 { 117 Map temp = context.getBeansOfType(MuleConfiguration.class, true, true); 119 if (temp.size() > 0) 120 { 121 MuleManager.setConfiguration((MuleConfiguration)temp.values().iterator().next()); 122 } 123 124 setProperties((Map )getBean(MULE_ENVIRONMENT_PROPERTIES_BEAN_NAME, Map .class)); 126 127 Map connectors = context.getBeansOfType(UMOConnector.class, true, true); 129 setConnectors(connectors.values()); 130 131 setMessageEndpointIdentifiers((Map )getBean(MULE_ENDPOINT_IDENTIFIERS_BEAN_NAME, Map .class)); 133 134 temp = context.getBeansOfType(UMOTransactionManagerFactory.class, true, true); 136 if (temp.size() > 0) 137 { 138 manager.setTransactionManager(((UMOTransactionManagerFactory)temp.values().iterator().next()).create()); 139 } 140 141 temp = context.getBeansOfType(UMOSecurityManager.class, true, true); 143 if (temp.size() > 0) 144 { 145 manager.setSecurityManager((UMOSecurityManager)temp.values().iterator().next()); 146 } 147 148 Map transformers = context.getBeansOfType(UMOTransformer.class, true, true); 150 setTransformers(transformers.values()); 151 152 Map endpoints = context.getBeansOfType(UMOEndpoint.class, true, true); 154 setEndpoints(endpoints.values()); 155 156 Map agents = context.getBeansOfType(UMOAgent.class, true, true); 158 setAgents(agents.values()); 159 160 Map containers = context.getBeansOfType(UMOContainerContext.class, true, true); 162 setContainerContext(containers); 163 164 Map interceptors = context.getBeansOfType(UMOInterceptorStack.class, true, true); 166 setInterceptorStacks(interceptors); 167 createModel(); 169 170 Map components = context.getBeansOfType(UMODescriptor.class, true, true); 172 setComponents(components.values()); 173 } 174 catch (Exception e) 175 { 176 throw new BeanInitializationException("Failed to wire MuleManager together: " + e.getMessage(), e); 177 } 178 } 179 180 public void setManagerId(String managerId) 181 { 182 manager.setId(managerId); 183 } 184 185 protected void createModel() throws UMOException 186 { 187 Map temp = context.getBeansOfType(UMOModel.class, true, true); 189 UMOModel model; 190 if (temp.size() > 0) 191 { 192 Map.Entry entry = (Map.Entry )temp.entrySet().iterator().next(); 193 model = (UMOModel)entry.getValue(); 194 model.setName(entry.getKey().toString()); 195 } 196 else 197 { 198 model = ModelFactory.createModel(MuleManager.getConfiguration().getModelType()); 200 } 201 202 209 Map epr = context.getBeansOfType(UMOEntryPointResolver.class, true, true); 211 if (epr.size() > 0) 212 { 213 model.setEntryPointResolver((UMOEntryPointResolver)epr.values().iterator().next()); 214 } 215 216 Map lcaf = context.getBeansOfType(UMOLifecycleAdapterFactory.class, true, true); 218 if (lcaf.size() > 0) 219 { 220 model.setLifecycleAdapterFactory((UMOLifecycleAdapterFactory)lcaf.values().iterator().next()); 221 } 222 223 Object listener = getBean(MULE_MODEL_EXCEPTION_STRATEGY_BEAN_NAME, ExceptionListener .class); 225 if (listener != null) 226 { 227 model.setExceptionListener((ExceptionListener )listener); 228 } 229 230 manager.setModel(model); 231 232 } 233 234 private Object getBean(String name, Class clazz) 235 { 236 try 237 { 238 return context.getBean(name, clazz); 239 } 240 catch (BeansException e) 241 { 242 return null; 243 } 244 245 } 246 247 protected void setContainerContext(Map containers) throws UMOException 248 { 249 if (containers.size() == 0) 250 { 251 SpringContainerContext container = new SpringContainerContext(); 253 container.setBeanFactory(context); 254 manager.setContainerContext(container); 255 } 256 else if (containers.size() == 1) 257 { 258 manager.setContainerContext((UMOContainerContext)containers.values().iterator().next()); 259 } 260 else 261 { 262 UMOContainerContext ctx = (UMOContainerContext)containers.values().iterator().next(); 263 logger.warn("There are " + containers.size() 264 + " container contexts in the spring context. Using the first one: " 265 + ctx.getClass().getName()); 266 manager.setContainerContext(ctx); 267 } 268 } 269 270 protected void setMessageEndpointIdentifiers(Map endpoints) throws InitialisationException 271 { 272 if (endpoints == null) 273 { 274 return; 275 } 276 Map.Entry entry; 277 for (Iterator iterator = endpoints.entrySet().iterator(); iterator.hasNext();) 278 { 279 entry = (Map.Entry )iterator.next(); 280 manager.registerEndpointIdentifier(entry.getKey().toString(), entry.getValue().toString()); 281 282 } 283 } 284 285 protected void setAgents(Collection agents) throws UMOException 286 { 287 for (Iterator iterator = agents.iterator(); iterator.hasNext();) 288 { 289 manager.registerAgent((UMOAgent)iterator.next()); 290 } 291 } 292 293 protected void setProperties(Map props) 294 { 295 if (props == null) 296 { 297 return; 298 } 299 Map.Entry entry; 300 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) 301 { 302 entry = (Map.Entry )iterator.next(); 303 manager.setProperty(entry.getKey(), entry.getValue()); 304 } 305 } 306 307 protected void setConnectors(Collection connectors) throws UMOException 308 { 309 for (Iterator iterator = connectors.iterator(); iterator.hasNext();) 310 { 311 manager.registerConnector((UMOConnector)iterator.next()); 312 } 313 } 314 315 protected void setTransformers(Collection transformers) throws InitialisationException 316 { 317 for (Iterator iterator = transformers.iterator(); iterator.hasNext();) 318 { 319 manager.registerTransformer((UMOTransformer)iterator.next()); 320 } 321 } 322 323 protected void setEndpoints(Collection endpoints) throws InitialisationException 324 { 325 for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) 326 { 327 manager.registerEndpoint((UMOEndpoint)iterator.next()); 328 } 329 } 330 331 protected void setComponents(Collection components) throws UMOException 332 { 333 UMODescriptor d; 334 for (Iterator iterator = components.iterator(); iterator.hasNext();) 335 { 336 d = (UMODescriptor)iterator.next(); 337 if (!manager.getModel().isComponentRegistered(d.getName())) 338 { 339 manager.getModel().registerComponent(d); 340 } 341 } 342 } 343 344 protected void setInterceptorStacks(Map stacks) 345 { 346 if (stacks == null) 347 { 348 return; 349 } 350 for (Iterator iterator = stacks.entrySet().iterator(); iterator.hasNext();) 351 { 352 Map.Entry entry = (Map.Entry )iterator.next(); 353 String name = entry.getKey().toString(); 354 manager.registerInterceptorStack(name, (UMOInterceptorStack)entry.getValue()); 355 } 356 } 357 358 public void afterPropertiesSet() throws Exception 359 { 360 manager.start(); 361 } 362 363 public void destroy() throws Exception 364 { 365 manager.dispose(); 366 } 367 } 368 | Popular Tags |