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.util;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.construct.FlowConstructAware;
11  import org.mule.api.lifecycle.InitialisationException;
12  import org.mule.api.service.Service;
13  import org.mule.api.service.ServiceAware;
14  import org.mule.config.i18n.MessageFactory;
15  import org.mule.object.AbstractObjectFactory;
16  
17  import org.springframework.beans.BeansException;
18  import org.springframework.context.ApplicationContext;
19  import org.springframework.context.ApplicationContextAware;
20  
21  /**
22   * This is an implementation of the ObjectFactory interface which simply delegates to 
23   * the Spring ApplicationContext.  Since the delegation happens each time a call to 
24   * getOrCreate() is made, this will correctly handle Spring beans which are 
25   * non-singletons (factory beans, etc.)
26   * 
27   * Singleton usage:
28   * 
29   *   <model>
30   *       <service name="myOrangeService">
31   *           <service>
32   *               <spring-object bean="myBean"/>
33   *           </service>
34   *       </service>
35   *   </model>
36   *
37   *   <spring:bean id="myBean" class="com.foo.Bar"/>
38   *   
39   * Non-singleton usage:
40   * 
41   *   <model>
42   *       <service name="myOrangeService">
43   *           <service>
44   *               <spring-object bean="myFactoryBean"/>
45   *           </service>
46   *       </service>
47   *   </model>
48   *
49   *   <spring:bean id="myFactoryBean" class="com.foo.BarFactory" factory-method="getNewBar"/>
50   */
51  public class SpringBeanLookup extends AbstractObjectFactory implements ApplicationContextAware
52  {
53      private ApplicationContext applicationContext;
54      private String bean;
55  
56      @Override
57      public void initialise() throws InitialisationException
58      {
59          if (bean == null)
60          {
61              throw new InitialisationException(MessageFactory.createStaticMessage("Bean name has not been set."), this);
62          }
63          if (applicationContext == null)
64          {
65              throw new InitialisationException(
66                  MessageFactory.createStaticMessage("ApplicationContext has not been injected."), this);
67          }
68  
69          // Get instance of spring bean to determine bean type.
70          // We do this because the result of org.springframework.beans.factory.BeanFactory.getType(String) when
71          // used before bean initialization does not always return the same type as afterwards. One specific
72          // case when AOP is used, and the actual bean class is returned before initialization but a proxy
73          // afterwards. This affects both prototype beans and lazy-init singletons.
74          objectClass = applicationContext.getBean(bean).getClass();
75      }
76  
77      @Override
78      public void dispose()
79      {
80          // Not implemented for Spring Beans
81      }
82  
83      @Override
84      public Object getInstance(MuleContext muleContext) throws Exception
85      {
86          Object instance = applicationContext.getBean(bean);
87          if(instance instanceof FlowConstructAware)
88          {
89              //The servie cannot be autowired from within Spring, so we do it here
90              ((FlowConstructAware)instance).setFlowConstruct(flowConstruct);
91          }
92          if(instance instanceof ServiceAware  && flowConstruct instanceof Service)
93          {
94              //The servie cannot be autowired from within Spring, so we do it here
95              ((ServiceAware)instance).setService((Service) flowConstruct);
96          }
97          fireInitialisationCallbacks(instance);
98          return instance;
99      }
100 
101     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
102     {
103         this.applicationContext = applicationContext;
104     }
105 
106     public String getBean()
107     {
108         return bean;
109     }
110 
111     public void setBean(String bean)
112     {
113         this.bean = bean;
114     }
115     
116     @Override
117     public boolean isSingleton()
118     {
119         return applicationContext.isSingleton(bean);
120     }
121 
122     @Override
123     public boolean isExternallyManagedLifecycle()
124     {
125         return true;
126     }
127 
128     public boolean isAutoWireObject()
129     {
130         //Spring does the wiring
131         return false;
132     }
133 }