1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
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 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| public abstract class AccumulatorMeasurement extends MeasurementBase implements CollectionMeasurement { |
61 |
| private Map<String, Collection<String>> terms = new HashMap<String, Collection<String>>(); |
62 |
| private Collection<String> values = new TreeSet<String>(); |
63 |
| |
64 |
34
| public AccumulatorMeasurement(MeasurementDescriptor descriptor, Metrics context, String initText) {
|
65 |
34
| super(descriptor, context, initText);
|
66 |
| |
67 |
34
| if (initText != null) {
|
68 |
28
| try {
|
69 |
28
| BufferedReader in = new BufferedReader(new StringReader(initText));
|
70 |
28
| String line;
|
71 |
| |
72 |
?
| while ((line = in.readLine()) != null) {
|
73 |
42
| synchronized (perl()) {
|
74 |
42
| if (perl().match("/^\\s*(\\S+)\\s*(.*)/", line)) {
|
75 |
42
| String name = perl().group(1);
|
76 |
42
| String re = perl().group(2);
|
77 |
| |
78 |
42
| Collection<String> res = terms.get(name);
|
79 |
42
| if (res == null) {
|
80 |
40
| res = new ArrayList<String>();
|
81 |
40
| terms.put(name, res);
|
82 |
| } |
83 |
| |
84 |
42
| if (re != null && re.length() > 0) {
|
85 |
21
| res.add(re);
|
86 |
| } |
87 |
| } |
88 |
| } |
89 |
| } |
90 |
| |
91 |
28
| in.close();
|
92 |
| } catch (Exception ex) { |
93 |
0
| Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex);
|
94 |
0
| terms.clear();
|
95 |
| } |
96 |
| } |
97 |
| |
98 |
34
| logTerms(initText);
|
99 |
| } |
100 |
| |
101 |
34
| private void logTerms(String initText) {
|
102 |
34
| Logger.getLogger(getClass()).debug("Initialize with\n" + initText);
|
103 |
34
| Logger.getLogger(getClass()).debug("Terms:");
|
104 |
| |
105 |
34
| for (Map.Entry<String, Collection<String>> entry : terms.entrySet()) {
|
106 |
40
| Logger.getLogger(getClass()).debug("\t" + entry.getKey());
|
107 |
| |
108 |
40
| for (String s : entry.getValue()) {
|
109 |
21
| Logger.getLogger(getClass()).debug("\t\t" + s);
|
110 |
| } |
111 |
| } |
112 |
| } |
113 |
| |
114 |
46
| public Number getValue() {
|
115 |
46
| return getValues().size();
|
116 |
| } |
117 |
| |
118 |
7
| public boolean isEmpty() {
|
119 |
7
| return getValues().isEmpty();
|
120 |
| } |
121 |
| |
122 |
0
| protected double compute() {
|
123 |
0
| return getValues().size();
|
124 |
| } |
125 |
| |
126 |
117
| public Collection<String> getValues() {
|
127 |
117
| if (!isCached()) {
|
128 |
98
| values.clear();
|
129 |
| |
130 |
98
| populateValues();
|
131 |
| |
132 |
98
| setCached(true);
|
133 |
| } |
134 |
| |
135 |
117
| return Collections.unmodifiableCollection(values);
|
136 |
| } |
137 |
| |
138 |
| protected abstract void populateValues(); |
139 |
| |
140 |
153
| protected void filterMetrics(Metrics metrics) {
|
141 |
153
| for (Map.Entry<String, Collection<String>> entry : terms.entrySet()) {
|
142 |
173
| String name = entry.getKey();
|
143 |
173
| Collection<String> res = entry.getValue();
|
144 |
| |
145 |
173
| Measurement measurement = metrics.getMeasurement(name);
|
146 |
173
| if (measurement instanceof CollectionMeasurement) {
|
147 |
126
| filterMeasurement((CollectionMeasurement) measurement, res);
|
148 |
| } |
149 |
| } |
150 |
| } |
151 |
| |
152 |
126
| private void filterMeasurement(CollectionMeasurement measurement, Collection<String> res) {
|
153 |
126
| if (res.isEmpty()) {
|
154 |
41
| values.addAll(measurement.getValues());
|
155 |
| } else { |
156 |
85
| for (String member : measurement.getValues()) {
|
157 |
171
| filterElement(member, res);
|
158 |
| } |
159 |
| } |
160 |
| } |
161 |
| |
162 |
171
| private void filterElement(String element, Collection<String> res) {
|
163 |
171
| boolean found = false;
|
164 |
171
| Iterator<String> i = res.iterator();
|
165 |
171
| while (!found && i.hasNext()) {
|
166 |
191
| found = evaluateRE(i.next(), element);
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
191
| private boolean evaluateRE(String re, String element) {
|
171 |
191
| boolean result = false;
|
172 |
| |
173 |
191
| synchronized (perl()) {
|
174 |
191
| if (perl().match(re, element)) {
|
175 |
85
| result = true;
|
176 |
85
| if (perl().group(1) != null) {
|
177 |
13
| values.add(perl().group(1));
|
178 |
| } else { |
179 |
72
| values.add(element);
|
180 |
| } |
181 |
| } |
182 |
| } |
183 |
| |
184 |
191
| return result;
|
185 |
| } |
186 |
| } |