001    
002    /*
003     * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: ContextApplicationFactoryBean.java 283 2012-02-16 23:44:38Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.vaadin;
009    
010    import org.springframework.beans.factory.BeanFactory;
011    import org.springframework.beans.factory.BeanInitializationException;
012    import org.springframework.beans.factory.config.AbstractFactoryBean;
013    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
014    
015    /**
016     * Spring singleton factory bean that produces the currently running {@link ContextApplication} Vaadin application instance.
017     *
018     * <p>
019     * This bean will only work if there is a current {@link ContextApplication} running at the time this class
020     * is instantiated (see {@link ContextApplication#get} for details). Therefore, it is most appropriate for use
021     * within a {@link SpringContextApplication}'s appplication context.
022     * </p>
023     *
024     * <p>
025     * <b>This class is deprecated</b> because there is a simpler and better way to expose and configure your Vaadin application
026     * in the Spring application context is using {@link ContextApplication#get} as a factory method:
027     * <blockquote><pre>
028     *  &lt;bean id="myVaadinApplication" class="org.dellroad.stuff.vaadin.ContextApplication" factory-method="get"/&gt;
029     * </pre></blockquote>
030     *
031     * @see ContextApplication#get
032     * @see SpringContextApplication
033     * @deprecated Just use a regular bean definition with a factory-method invoking ContextApplication.get
034     */
035    @Deprecated
036    public class ContextApplicationFactoryBean extends AbstractFactoryBean {
037    
038        private ContextApplication application;
039    
040        private boolean autowire;
041    
042        /**
043         * Should this instance autowire the application instance be autowired using the containing bean factory?
044         * Requires the containing bean factory to be an {@link AutowireCapableBeanFactory}.
045         * Default is false.
046         */
047        public boolean isAutowire() {
048            return this.autowire;
049        }
050        public void setAutowire(boolean autowire) {
051            this.autowire = autowire;
052        }
053    
054        @Override
055        public Class<? extends ContextApplication> getObjectType() {
056            return this.getContextApplication().getClass();
057        }
058    
059        @Override
060        protected ContextApplication createInstance() {
061            return this.getContextApplication();
062        }
063    
064        @Override
065        public void afterPropertiesSet() throws Exception {
066    
067            // Invoke superclass
068            super.afterPropertiesSet();
069    
070            // Should we autowire?
071            if (!this.autowire)
072                return;
073    
074            // Yes, autowire
075            BeanFactory beanFactory = this.getBeanFactory();
076            AutowireCapableBeanFactory autowireFactory;
077            try {
078                autowireFactory = (AutowireCapableBeanFactory)beanFactory;
079            } catch (ClassCastException e) {
080                throw new BeanInitializationException("containing bean factory is not autowire-capable");
081            }
082            autowireFactory.autowireBean(this.getContextApplication());
083        }
084    
085        private ContextApplication getContextApplication() {
086            if (this.application == null)
087                this.application = ContextApplication.get();
088            return this.application;
089        }
090    }
091