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.factory.support.BeanDefinitionBuilder;
017    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
018    import org.springframework.beans.factory.xml.ParserContext;
019    import org.w3c.dom.Element;
020    
021    import com.mtgi.analytics.aop.BehaviorTrackingAdvice;
022    
023    public class BtAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
024    
025            @Override
026            protected Class<?> getBeanClass(Element element) {
027                    return BehaviorTrackingAdvice.class;
028            }
029    
030            @Override
031            protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
032    
033                    if (element.hasAttribute(BtNamespaceUtils.TRACKING_MANAGER_ATTRIBUTE)) {
034                            if (element.hasAttribute(BtNamespaceUtils.TRACKING_MANAGER_APPLICATION_ATTRIBUTE))
035                                    throw new IllegalArgumentException("Cannot specify both 'tracking-manager' and 'application' at the same time");
036                            builder.addPropertyReference(BtNamespaceUtils.TRACKING_MANAGER_PROPERTY, element.getAttribute(BtNamespaceUtils.TRACKING_MANAGER_ATTRIBUTE));
037                            //if there is an explicit tracking manager reference, there's no need to register our post-processor.
038                            return;
039                    } 
040                    
041                    String application = null;
042                    if (element.hasAttribute(BtNamespaceUtils.TRACKING_MANAGER_APPLICATION_ATTRIBUTE)) {
043                            application = element.getAttribute(BtNamespaceUtils.TRACKING_MANAGER_APPLICATION_ATTRIBUTE);
044                            builder.addPropertyValue(BtNamespaceUtils.TRACKING_MANAGER_APPLICATION_PROPERTY, application);
045                    }
046                    //add reference to default tracking manager alias, which will be configured by our bean factory post-processor.
047                    builder.addPropertyReference(BtNamespaceUtils.TRACKING_MANAGER_PROPERTY, "defaultTrackingManager");
048    
049                    //add post-processor to check manager configuration.
050                    if (!parserContext.getRegistry().containsBeanDefinition("behaviorTrackingProcessor")) {
051                            BeanDefinitionBuilder processor = BeanDefinitionBuilder.rootBeanDefinition(BehaviorTrackingBeanFactoryPostProcessor.class);
052                            if (application != null)
053                                    processor.addPropertyValue("application", application);
054                            parserContext.getRegistry().registerBeanDefinition("behaviorTrackingProcessor", processor.getBeanDefinition());
055                    }
056            }
057    
058    }