001    /*
002    @license.text@
003     */
004    package biz.hammurapi.metrics;
005    
006    import java.util.ArrayList;
007    import java.util.Iterator;
008    import java.util.List;
009    
010    import javax.xml.transform.TransformerException;
011    
012    import org.w3c.dom.Element;
013    import org.w3c.dom.Node;
014    import org.w3c.dom.NodeList;
015    
016    import biz.hammurapi.config.Component;
017    import biz.hammurapi.config.ConfigurationException;
018    import biz.hammurapi.config.Context;
019    import biz.hammurapi.config.DomConfigFactory;
020    import biz.hammurapi.config.DomConfigurable;
021    import biz.hammurapi.xml.dom.AbstractDomObject;
022    import biz.hammurapi.xml.dom.DOMUtils;
023    
024    
025    /**
026     * @author Pavel Vlasov
027     * @revision $Revision$
028     */
029    public class SlicingMeasurementCategoryFactory extends MeasurementCategoryFactory implements Component, DomConfigurable {
030        private SlicingMeasurementConsumer consumer;
031        
032        public void start() throws ConfigurationException {        
033            try {           
034                NodeList nl = DOMUtils.selectNodeList(configElement, "category");
035                            for (int i=0, l=nl.getLength(); i<l; ++i) {
036                                    Element ce = (Element) nl.item(i);
037                    categories.add(DOMUtils.getElementText(ce));
038                }
039                
040                String tickValue=AbstractDomObject.getElementText(configElement, "tick");
041                long tick = tickValue==null ? 60000 : Long.parseLong(tickValue);
042                
043                String kmValue=AbstractDomObject.getElementText(configElement, "keep-measurements");
044                boolean keepMeasurements = kmValue==null ? false : "yes".equalsIgnoreCase(kmValue);
045                
046                String maxQueueValue=AbstractDomObject.getElementText(configElement, "max-queue");
047                int maxQueue = maxQueueValue==null ? 1000 : Integer.parseInt(maxQueueValue);
048                
049                            consumer=createMeasurementConsumer(tick, keepMeasurements, maxQueue);
050                consumer.start();
051            } catch (Exception e) {
052                throw new ConfigurationException(e);
053                    }
054        }
055    
056        /**
057         * Creates a consumer using DomConfigFactory 
058             * @param cxpa
059             * @param factory
060             * @return
061             * @throws ConfigurationException
062             * @throws TransformerException
063             */
064            protected SliceConsumer createSliceConsumer() throws ConfigurationException {
065                    try {
066                            return (SliceConsumer) new DomConfigFactory(getClass().getClassLoader()).create(DOMUtils.selectSingleNode(configElement, "slice-consumer"));
067                    } catch (Exception e) {
068                            throw new ConfigurationException(e);
069                    }
070            }
071    
072            /**
073         * Override this method to create a custom consumer.
074         * @param tick
075         * @param keepMeasurements
076         * @param maxQueue
077         * @param sliceConsumer
078         * @return
079             * @throws ConfigurationException
080         */
081            protected SlicingMeasurementConsumer createMeasurementConsumer(long tick, boolean keepMeasurements, int maxQueue) throws ConfigurationException {
082                    return new SlicingMeasurementConsumer (tick, keepMeasurements, maxQueue, createSliceConsumer());
083            }
084    
085            public void stop() throws ConfigurationException {
086            if (consumer!=null) {
087                consumer.shutdown();
088            }
089        }
090        
091        private List categories=new ArrayList();
092            protected Element configElement;
093        
094            public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
095                    configElement=(Element) configNode;
096            }
097    
098            public MeasurementConsumer getMeasurementConsumer(String categoryName) {
099                if (categories.isEmpty() || categories.contains(categoryName)) {
100                    return consumer.getCategoryInstance(categoryName);
101                }
102                
103                Iterator it=categories.iterator();
104                while (it.hasNext()) {
105                    if (categoryName.startsWith(it.next()+".")) {
106                            return consumer.getCategoryInstance(categoryName);                                      
107                    }
108                }
109                
110                return null;
111            }
112    
113            public void setOwner(Object owner) {
114                    // Ignore               
115            }       
116    }