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.v11;
015    
016    import static com.mtgi.analytics.aop.config.v11.ConfigurationConstants.CONFIG_NAMESPACE;
017    import static com.mtgi.analytics.aop.config.v11.ConfigurationConstants.CONFIG_SCHEDULER;
018    import static com.mtgi.analytics.aop.config.v11.ConfigurationConstants.CONFIG_TEMPLATE;
019    import static com.mtgi.analytics.aop.config.v11.SchedulerActivationPostProcessor.configureTriggerDefinition;
020    import static com.mtgi.analytics.aop.config.v11.SchedulerActivationPostProcessor.registerPostProcessor;
021    
022    import org.springframework.beans.factory.config.BeanDefinition;
023    import org.springframework.beans.factory.config.BeanDefinitionHolder;
024    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
025    import org.springframework.beans.factory.xml.ParserContext;
026    import org.w3c.dom.Element;
027    
028    import com.mtgi.analytics.XmlBehaviorEventPersisterImpl;
029    import com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser;
030    
031    /** 
032     * Parses the <code>bt:xml-persister</code> tag to produce an {@link XmlBehaviorEventPersisterImpl} bean,
033     * for inclusion in an enclosing <code>bt:manager</code> tag or as a standalone managed bean.
034     */
035    public class BtXmlPersisterBeanDefinitionParser extends TemplateBeanDefinitionParser 
036    {
037            private static final String[] PROPS = { "file", "binary", "compress" };
038            
039            public static final String CONFIG_PERSISTER = CONFIG_NAMESPACE + ".btPersister";
040            public static final String CONFIG_ROTATE_TRIGGER = CONFIG_NAMESPACE + ".btRotateTrigger";
041            
042            public BtXmlPersisterBeanDefinitionParser() {
043                    super(CONFIG_TEMPLATE, CONFIG_PERSISTER);
044            }
045    
046            @Override
047            protected void transform(ConfigurableListableBeanFactory factory, BeanDefinition template, Element element, ParserContext parserContext) {
048                    String id = overrideAttribute("id", template, element);
049                    for (String p : PROPS)
050                            overrideProperty(p, template, element, false);
051                    //schedule periodic log rotation with Quartz
052                    String rotateSchedule = element.getAttribute("rotate-schedule");
053                    if (rotateSchedule != null)
054                            configureLogRotation(parserContext, factory, rotateSchedule);
055                    if (parserContext.isNested()) {
056                            if (id == null)
057                                    id = parserContext.getReaderContext().generateBeanName(template);
058                            BtManagerBeanDefinitionParser.registerNestedBean(new BeanDefinitionHolder(template, id), "persister", parserContext);
059                    }
060            }
061    
062            public static void configureLogRotation(ParserContext parserContext, ConfigurableListableBeanFactory factory, String schedule) {
063                    BeanDefinition trigger = factory.getBeanDefinition(CONFIG_ROTATE_TRIGGER);
064                    configureTriggerDefinition(trigger, schedule, parserContext.getReaderContext().generateBeanName(trigger) + "_rotate");
065                    registerPostProcessor(parserContext, factory, CONFIG_SCHEDULER, CONFIG_ROTATE_TRIGGER);
066            }
067    }