001    /* 
002     * Copyright 2008-2009 the original author or authors.
003     * The contents of this file are subject to the Mozilla Public License
004     * Version 1.1 (the "License"); you may not use this file except in
005     * compliance with the License. You may obtain a copy of the License at
006     * http://www.mozilla.org/MPL/
007     *
008     * Software distributed under the License is distributed on an "AS IS"
009     * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
010     * License for the specific language governing rights and limitations
011     * under the License.
012     */
013     
014    package com.mtgi.analytics.aop.config;
015    
016    import org.springframework.beans.factory.BeanFactory;
017    import org.springframework.beans.factory.DisposableBean;
018    import org.springframework.beans.factory.FactoryBean;
019    import org.springframework.beans.factory.FactoryBeanNotInitializedException;
020    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
021    
022    /**
023     * <p>Bootstraps a bean from one {@link BeanFactory} into another.  The intent is that we can
024     * "embed" one Spring bean factory inside another, and use instances of this class to promote
025     * public beans out of the embedded factory.</p>
026     * 
027     * <p>Not intended to be used directly in spring configuration files, but rather indirectly via
028     * {@link TemplateBeanDefinitionParser} subclasses.</p>
029     * 
030     * @see TemplateBeanDefinitionParser
031     */
032    public class TemplateBeanDefinitionFactory implements FactoryBean, DisposableBean {
033    
034            private BeanFactory beanFactory;
035            private String beanName;
036            
037            public Object getObject() throws Exception {
038                    if (beanFactory == null || beanName == null)
039                            throw new FactoryBeanNotInitializedException();
040                    return beanFactory.getBean(beanName);
041            }
042    
043            public Class<?> getObjectType() {
044                    return beanFactory == null ? null : beanFactory.getType(beanName);
045            }
046    
047            public boolean isSingleton() {
048                    return beanFactory.isSingleton(beanName);
049            }
050    
051            public void setBeanFactory(BeanFactory beanFactory) {
052                    this.beanFactory = beanFactory;
053            }
054    
055            public void setBeanName(String beanName) {
056                    this.beanName = beanName;
057            }
058    
059            public void destroy() throws Exception {
060                    if (beanFactory instanceof DisposableBean)
061                            ((DisposableBean)beanFactory).destroy();
062                    else if (beanFactory instanceof ConfigurableBeanFactory)
063                            ((ConfigurableBeanFactory)beanFactory).destroySingletons();
064            }
065    
066    }