001: /*
002: * $Id: SpringBeanLookup.java 11376 2008-03-16 17:44:10Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring.util;
012:
013: import org.mule.api.lifecycle.InitialisationException;
014: import org.mule.api.lifecycle.LifecycleTransitionResult;
015: import org.mule.config.i18n.MessageFactory;
016: import org.mule.object.AbstractObjectFactory;
017:
018: import org.springframework.beans.BeansException;
019: import org.springframework.context.ApplicationContext;
020: import org.springframework.context.ApplicationContextAware;
021:
022: /**
023: * This is an implementation of the ObjectFactory interface which simply delegates to
024: * the Spring ApplicationContext. Since the delegation happens each time a call to
025: * getOrCreate() is made, this will correctly handle Spring beans which are
026: * non-singletons (factory beans, etc.)
027: *
028: * Singleton usage:
029: *
030: * <model>
031: * <service name="myOrangeService">
032: * <service>
033: * <spring-object bean="myBean"/>
034: * </service>
035: * </service>
036: * </model>
037: *
038: * <spring:bean id="myBean" class="com.foo.Bar"/>
039: *
040: * Non-singleton usage:
041: *
042: * <model>
043: * <service name="myOrangeService">
044: * <service>
045: * <spring-object bean="myFactoryBean"/>
046: * </service>
047: * </service>
048: * </model>
049: *
050: * <spring:bean id="myFactoryBean" class="com.foo.BarFactory" factory-method="getNewBar"/>
051: */
052: public class SpringBeanLookup extends AbstractObjectFactory implements
053: ApplicationContextAware {
054: private ApplicationContext applicationContext;
055: private String bean;
056:
057: public LifecycleTransitionResult initialise()
058: throws InitialisationException {
059: if (bean == null) {
060: throw new InitialisationException(
061: MessageFactory
062: .createStaticMessage("Bean name has not been set."),
063: this );
064: }
065: if (applicationContext == null) {
066: throw new InitialisationException(
067: MessageFactory
068: .createStaticMessage("ApplicationContext has not been injected."),
069: this );
070: }
071: return LifecycleTransitionResult.OK;
072: }
073:
074: public void dispose() {
075: // Not implemented for Spring Beans
076: }
077:
078: public Class getObjectClass() {
079: return applicationContext.getType(bean);
080: }
081:
082: public Object getInstance() throws Exception {
083: Object instance = applicationContext.getBean(bean);
084: fireInitialisationCallbacks(instance);
085: return instance;
086: }
087:
088: public void setApplicationContext(
089: ApplicationContext applicationContext)
090: throws BeansException {
091: this .applicationContext = applicationContext;
092: }
093:
094: public String getBean() {
095: return bean;
096: }
097:
098: public void setBean(String bean) {
099: this .bean = bean;
100: }
101:
102: // @Override
103: public boolean isSingleton() {
104: return applicationContext.isSingleton(bean);
105: }
106:
107: }
|