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.v10;
015    
016    import org.springframework.beans.BeansException;
017    import org.springframework.beans.factory.BeanInitializationException;
018    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
019    import org.springframework.beans.factory.config.BeanDefinition;
020    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
021    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
022    
023    import com.mtgi.analytics.BehaviorTrackingManager;
024    import com.mtgi.analytics.BehaviorTrackingManagerImpl;
025    
026    /** intelligently assigns a matching BehaviorTrackingManager instance to a BehaviorTrackingAdvice bean, creating one if necessary */
027    public class BehaviorTrackingBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
028    
029            private String application;
030            
031            public void setApplication(String application) {
032                    this.application = application;
033            }
034    
035            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
036    
037                    try {
038                            BeanDefinition def = beanFactory.getBeanDefinition("defaultTrackingManager");
039                            if (def != null)
040                                    return; //nothing to do.
041                    } catch (NoSuchBeanDefinitionException notFound) {
042                    }
043    
044                    //no default BehaviorTrackingManager instances defined, choose an available instance or create one.
045                    String[] matches = beanFactory.getBeanNamesForType(BehaviorTrackingManager.class, false, false);
046                    if (matches.length > 0) {
047                            beanFactory.registerAlias(matches[0], "defaultTrackingManager");
048                    } else {
049                            if (application == null)
050                                    throw new BeanInitializationException("'application' is required on bt:advice when not specified anywhere else in the configuration");
051    
052                            BehaviorTrackingManagerImpl inst = new BehaviorTrackingManagerImpl();
053                            inst.setApplication(application);
054                            beanFactory.autowireBeanProperties(inst, ConfigurableListableBeanFactory.AUTOWIRE_BY_TYPE, false);
055                            try {
056                                    inst.afterPropertiesSet();
057                            } catch (Exception e) {
058                                    throw new BeanInitializationException("Error initializing default tracking manager instance", e);
059                            }
060                            beanFactory.registerSingleton("defaultTrackingManager", inst);
061                    }
062                    
063            }
064    
065    }