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