View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.factories;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.processor.MessageProcessor;
13  
14  import org.springframework.beans.BeansException;
15  import org.springframework.beans.factory.FactoryBean;
16  import org.springframework.context.ApplicationContext;
17  import org.springframework.context.ApplicationContextAware;
18  
19  public class FlowRefFactoryBean implements FactoryBean<MessageProcessor>, ApplicationContextAware
20  {
21  
22      private String refName;;
23      private ApplicationContext applicationContext;
24  
25      public void setName(String name)
26      {
27          this.refName = name;
28      }
29  
30      public MessageProcessor getObject() throws Exception
31      {
32          final MessageProcessor processor = ((MessageProcessor) applicationContext.getBean(refName));
33          if (processor instanceof FlowConstruct)
34          {
35              // If a FlowConstuct is reference then decouple lifcycle/injection
36              return new MessageProcessor()
37              {
38                  public MuleEvent process(MuleEvent event) throws MuleException
39                  {
40                      return processor.process(event);
41                  }
42              };
43          }
44          else
45          {
46              return processor;
47          }
48      }
49  
50      public boolean isSingleton()
51      {
52          return false;
53      }
54  
55      public Class<?> getObjectType()
56      {
57          return MessageProcessor.class;
58      }
59  
60      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
61      {
62          this.applicationContext = applicationContext;
63      }
64  }