View Javadoc

1   /*
2    * $Id: SpringBeanLookup.java 11517 2008-03-31 21:34:19Z dirk.olmes $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.util;
12  
13  import org.mule.api.lifecycle.InitialisationException;
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      public void initialise() throws InitialisationException
57      {
58          if (bean == null)
59          {
60              throw new InitialisationException(MessageFactory.createStaticMessage("Bean name has not been set."), this);
61          }
62          if (applicationContext == null)
63          {
64              throw new InitialisationException(
65                  MessageFactory.createStaticMessage("ApplicationContext has not been injected."), this);
66          }
67      }
68  
69      public void dispose()
70      {
71          // Not implemented for Spring Beans
72      }
73  
74      public Class getObjectClass()
75      {
76          return applicationContext.getType(bean);
77      }
78  
79      public Object getInstance() throws Exception
80      {
81          Object instance = applicationContext.getBean(bean);
82          fireInitialisationCallbacks(instance);
83          return instance;
84      }
85  
86      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
87      {
88          this.applicationContext = applicationContext;
89      }
90  
91      public String getBean()
92      {
93          return bean;
94      }
95  
96      public void setBean(String bean)
97      {
98          this.bean = bean;
99      }
100     
101     // @Override
102     public boolean isSingleton()
103     {
104         return applicationContext.isSingleton(bean);
105     }
106 
107 }