Coverage Report - org.mule.config.spring.util.SpringBeanLookup
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringBeanLookup
0%
0/23
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: SpringBeanLookup.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.MuleContext;
 14  
 import org.mule.api.construct.FlowConstructAware;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.api.service.Service;
 17  
 import org.mule.api.service.ServiceAware;
 18  
 import org.mule.config.i18n.MessageFactory;
 19  
 import org.mule.object.AbstractObjectFactory;
 20  
 
 21  
 import org.springframework.beans.BeansException;
 22  
 import org.springframework.context.ApplicationContext;
 23  
 import org.springframework.context.ApplicationContextAware;
 24  
 
 25  
 /**
 26  
  * This is an implementation of the ObjectFactory interface which simply delegates to 
 27  
  * the Spring ApplicationContext.  Since the delegation happens each time a call to 
 28  
  * getOrCreate() is made, this will correctly handle Spring beans which are 
 29  
  * non-singletons (factory beans, etc.)
 30  
  * 
 31  
  * Singleton usage:
 32  
  * 
 33  
  *   <model>
 34  
  *       <service name="myOrangeService">
 35  
  *           <service>
 36  
  *               <spring-object bean="myBean"/>
 37  
  *           </service>
 38  
  *       </service>
 39  
  *   </model>
 40  
  *
 41  
  *   <spring:bean id="myBean" class="com.foo.Bar"/>
 42  
  *   
 43  
  * Non-singleton usage:
 44  
  * 
 45  
  *   <model>
 46  
  *       <service name="myOrangeService">
 47  
  *           <service>
 48  
  *               <spring-object bean="myFactoryBean"/>
 49  
  *           </service>
 50  
  *       </service>
 51  
  *   </model>
 52  
  *
 53  
  *   <spring:bean id="myFactoryBean" class="com.foo.BarFactory" factory-method="getNewBar"/>
 54  
  */
 55  0
 public class SpringBeanLookup extends AbstractObjectFactory implements ApplicationContextAware
 56  
 {
 57  
     private ApplicationContext applicationContext;
 58  
     private String bean;
 59  
 
 60  
     @Override
 61  
     public void initialise() throws InitialisationException
 62  
     {
 63  0
         if (bean == null)
 64  
         {
 65  0
             throw new InitialisationException(MessageFactory.createStaticMessage("Bean name has not been set."), this);
 66  
         }
 67  0
         if (applicationContext == null)
 68  
         {
 69  0
             throw new InitialisationException(
 70  
                 MessageFactory.createStaticMessage("ApplicationContext has not been injected."), this);
 71  
         }
 72  0
     }
 73  
 
 74  
     @Override
 75  
     public void dispose()
 76  
     {
 77  
         // Not implemented for Spring Beans
 78  0
     }
 79  
 
 80  
     @Override
 81  
     public Class<?> getObjectClass()
 82  
     {
 83  0
         return applicationContext.getType(bean);
 84  
     }
 85  
 
 86  
     @Override
 87  
     public Object getInstance(MuleContext muleContext) throws Exception
 88  
     {
 89  0
         Object instance = applicationContext.getBean(bean);
 90  0
         if(instance instanceof FlowConstructAware)
 91  
         {
 92  
             //The servie cannot be autowired from within Spring, so we do it here
 93  0
             ((FlowConstructAware)instance).setFlowConstruct(flowConstruct);
 94  
         }
 95  0
         if(instance instanceof ServiceAware  && flowConstruct instanceof Service)
 96  
         {
 97  
             //The servie cannot be autowired from within Spring, so we do it here
 98  0
             ((ServiceAware)instance).setService((Service) flowConstruct);
 99  
         }
 100  0
         fireInitialisationCallbacks(instance);
 101  0
         return instance;
 102  
     }
 103  
 
 104  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
 105  
     {
 106  0
         this.applicationContext = applicationContext;
 107  0
     }
 108  
 
 109  
     public String getBean()
 110  
     {
 111  0
         return bean;
 112  
     }
 113  
 
 114  
     public void setBean(String bean)
 115  
     {
 116  0
         this.bean = bean;
 117  0
     }
 118  
     
 119  
     @Override
 120  
     public boolean isSingleton()
 121  
     {
 122  0
         return applicationContext.isSingleton(bean);
 123  
     }
 124  
 
 125  
     @Override
 126  
     public boolean isExternallyManagedLifecycle()
 127  
     {
 128  0
         return true;
 129  
     }
 130  
 
 131  
     public boolean isAutoWireObject()
 132  
     {
 133  
         //Spring does the wiring
 134  0
         return false;
 135  
     }
 136  
 }