KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > AccumulatorMeasurement


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 /**
41  * <p>Base class that accumulates entries, filtering with regular
42  * expressions. If no regular expressions are given, matches
43  * everything for the given measurement, which must implement
44  * the <code>CollectionMeasurement</code> interface. Regular
45  * expressions matching using <code>Perl5Util</code> from
46  * Jakarta-ORO. This measurement will use
47  * <code>Perl5Util.group(1)</code> if not null, otherwise the
48  * full string.</p>
49  *
50  * <p>This is the syntax for initializing this type of
51  * measurement:</p>
52  *
53  * <pre>
54  * &lt;init&gt;
55  * measurement name [perl regular expression]
56  * ...
57  * &lt;/init&gt;
58  * </pre>
59  */

60 public abstract class AccumulatorMeasurement extends MeasurementBase implements CollectionMeasurement {
61     private Map terms = new HashMap();
62     private Collection values = new TreeSet();
63
64     public AccumulatorMeasurement(MeasurementDescriptor descriptor, Metrics context, String JavaDoc initText) {
65         super(descriptor, context, initText);
66
67         if (initText != null) {
68             try {
69                 BufferedReader in = new BufferedReader(new StringReader(initText));
70                 String JavaDoc line;
71                 
72                 while ((line = in.readLine()) != null) {
73                     synchronized (perl()) {
74                         if (perl().match("/^\\s*(\\S+)\\s*(.*)/", line)) {
75                             String JavaDoc name = perl().group(1);
76                             String JavaDoc re = perl().group(2);
77
78                             Collection res = (Collection) terms.get(name);
79                             if (res == null) {
80                                 res = new ArrayList();
81                                 terms.put(name, res);
82                             }
83
84                             if (re != null && re.length() > 0) {
85                                 res.add(re);
86                             }
87                         }
88                     }
89                 }
90                 
91                 in.close();
92             } catch (Exception JavaDoc ex) {
93                 Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex);
94                 terms.clear();
95             }
96         }
97
98         logTerms(initText);
99     }
100
101     private void logTerms(String JavaDoc initText) {
102         Logger.getLogger(getClass()).debug("Initialize with\n" + initText);
103         Logger.getLogger(getClass()).debug("Terms:");
104
105         Iterator i = terms.entrySet().iterator();
106         while (i.hasNext()) {
107             Map.Entry entry = (Map.Entry) i.next();
108             Logger.getLogger(getClass()).debug("\t" + entry.getKey());
109
110             Iterator j = ((Collection) entry.getValue()).iterator();
111             while (j.hasNext()) {
112                 Logger.getLogger(getClass()).debug("\t\t" + j.next());
113             }
114         }
115     }
116
117     public Number JavaDoc getValue() {
118         return new Integer JavaDoc(getValues().size());
119     }
120
121     public boolean isEmpty() {
122         return getValues().isEmpty();
123     }
124     
125     protected double compute() {
126         return getValues().size();
127     }
128
129     public Collection getValues() {
130         if (!isCached()) {
131             values.clear();
132             
133             populateValues();
134
135             setCached(true);
136         }
137         
138         return Collections.unmodifiableCollection(values);
139     }
140
141     protected abstract void populateValues();
142
143     protected void filterMetrics(Metrics metrics) {
144         Iterator i = terms.entrySet().iterator();
145         while (i.hasNext()) {
146             Map.Entry entry = (Map.Entry) i.next();
147             String JavaDoc name = (String JavaDoc) entry.getKey();
148             Collection res = (Collection) entry.getValue();
149         
150             Measurement measurement = metrics.getMeasurement(name);
151             if (measurement instanceof CollectionMeasurement) {
152                 filterMeasurement((CollectionMeasurement) measurement, res);
153             }
154         }
155     }
156     
157     private void filterMeasurement(CollectionMeasurement measurement, Collection res) {
158         if (res.isEmpty()) {
159             values.addAll(measurement.getValues());
160         } else {
161             Iterator i = measurement.getValues().iterator();
162             while (i.hasNext()) {
163                 filterElement((String JavaDoc) i.next(), res);
164             }
165         }
166     }
167     
168     private void filterElement(String JavaDoc element, Collection res) {
169         boolean found = false;
170         Iterator i = res.iterator();
171         while (!found && i.hasNext()) {
172             found = evaluateRE((String JavaDoc) i.next(), element);
173         }
174     }
175     
176     private boolean evaluateRE(String JavaDoc re, String JavaDoc element) {
177         boolean result = false;
178
179         synchronized (perl()) {
180             if (perl().match(re, element)) {
181                 result = true;
182                 if (perl().group(1) != null) {
183                     values.add(perl().group(1));
184                 } else {
185                     values.add(element);
186                 }
187             }
188         }
189
190         return result;
191     }
192 }
193
Popular Tags