001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.metrics;
024:
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import javax.xml.transform.TransformerException;
030:
031: import org.apache.xpath.CachedXPathAPI;
032: import org.apache.xpath.XPathAPI;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.traversal.NodeIterator;
036:
037: import biz.hammurapi.config.Component;
038: import biz.hammurapi.config.ConfigurationException;
039: import biz.hammurapi.config.Context;
040: import biz.hammurapi.config.DomConfigFactory;
041: import biz.hammurapi.config.DomConfigurable;
042: import biz.hammurapi.xml.dom.AbstractDomObject;
043: import biz.hammurapi.xml.dom.DOMUtils;
044:
045: /**
046: * @author Pavel Vlasov
047: * @revision $Revision$
048: */
049: public class SlicingMeasurementCategoryFactory extends
050: MeasurementCategoryFactory implements Component,
051: DomConfigurable {
052: private SlicingMeasurementConsumer consumer;
053:
054: public void start() throws ConfigurationException {
055: try {
056: CachedXPathAPI cxpa = new CachedXPathAPI();
057: NodeIterator nit = cxpa.selectNodeIterator(configElement,
058: "category");
059: Element ce;
060: while ((ce = (Element) nit.nextNode()) != null) {
061: categories.add(DOMUtils.getElementText(ce));
062: }
063:
064: String tickValue = AbstractDomObject.getElementText(
065: configElement, "tick", cxpa);
066: long tick = tickValue == null ? 60000 : Long
067: .parseLong(tickValue);
068:
069: String kmValue = AbstractDomObject.getElementText(
070: configElement, "keep-measurements", cxpa);
071: boolean keepMeasurements = kmValue == null ? false : "yes"
072: .equalsIgnoreCase(kmValue);
073:
074: String maxQueueValue = AbstractDomObject.getElementText(
075: configElement, "max-queue", cxpa);
076: int maxQueue = maxQueueValue == null ? 1000 : Integer
077: .parseInt(maxQueueValue);
078:
079: consumer = createMeasurementConsumer(tick,
080: keepMeasurements, maxQueue);
081: consumer.start();
082: } catch (TransformerException e) {
083: throw new ConfigurationException(e);
084: }
085: }
086:
087: /**
088: * Creates a consumer using DomConfigFactory
089: * @param cxpa
090: * @param factory
091: * @return
092: * @throws ConfigurationException
093: * @throws TransformerException
094: */
095: protected SliceConsumer createSliceConsumer()
096: throws ConfigurationException {
097: try {
098: return (SliceConsumer) new DomConfigFactory(getClass()
099: .getClassLoader()).create(XPathAPI
100: .selectSingleNode(configElement, "slice-consumer"));
101: } catch (TransformerException e) {
102: throw new ConfigurationException(e);
103: }
104: }
105:
106: /**
107: * Override this method to create a custom consumer.
108: * @param tick
109: * @param keepMeasurements
110: * @param maxQueue
111: * @param sliceConsumer
112: * @return
113: * @throws ConfigurationException
114: */
115: protected SlicingMeasurementConsumer createMeasurementConsumer(
116: long tick, boolean keepMeasurements, int maxQueue)
117: throws ConfigurationException {
118: return new SlicingMeasurementConsumer(tick, keepMeasurements,
119: maxQueue, createSliceConsumer());
120: }
121:
122: public void stop() throws ConfigurationException {
123: if (consumer != null) {
124: consumer.shutdown();
125: }
126: }
127:
128: private List categories = new ArrayList();
129: protected Element configElement;
130:
131: public void configure(Node configNode, Context context)
132: throws ConfigurationException {
133: configElement = (Element) configNode;
134: }
135:
136: public MeasurementConsumer getMeasurementConsumer(
137: String categoryName) {
138: if (categories.isEmpty() || categories.contains(categoryName)) {
139: return consumer.getCategoryInstance(categoryName);
140: }
141:
142: Iterator it = categories.iterator();
143: while (it.hasNext()) {
144: if (categoryName.startsWith(it.next() + ".")) {
145: return consumer.getCategoryInstance(categoryName);
146: }
147: }
148:
149: return null;
150: }
151:
152: public void setOwner(Object owner) {
153: // Ignore
154: }
155: }
|