1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.eclipse.jetty.monitor.triggers;
21
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import javax.management.MBeanServerConnection;
26 import javax.management.MalformedObjectNameException;
27 import javax.management.ObjectName;
28 import javax.management.openmbean.CompositeData;
29
30 import org.eclipse.jetty.monitor.JMXMonitor;
31 import org.eclipse.jetty.monitor.jmx.EventState;
32 import org.eclipse.jetty.monitor.jmx.EventTrigger;
33 import org.eclipse.jetty.util.log.Log;
34 import org.eclipse.jetty.util.log.Logger;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class AttrEventTrigger<TYPE extends Comparable<TYPE>>
50 extends EventTrigger
51 {
52 private static final Logger LOG = Log.getLogger(AttrEventTrigger.class);
53
54 private final ObjectName _nameObject;
55
56 protected final String _objectName;
57 protected final String _attributeName;
58 protected Map<Long, EventState<TYPE>> _states;
59
60
61
62
63
64
65
66
67
68
69
70
71 public AttrEventTrigger(String objectName, String attributeName)
72 throws MalformedObjectNameException, IllegalArgumentException
73 {
74 if (objectName == null)
75 throw new IllegalArgumentException("Object name cannot be null");
76 if (attributeName == null)
77 throw new IllegalArgumentException("Attribute name cannot be null");
78
79 _states = new ConcurrentHashMap<Long,EventState<TYPE>>();
80
81 _objectName = objectName;
82 _attributeName = attributeName;
83
84 _nameObject = new ObjectName(_objectName);
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public AttrEventTrigger(ObjectName nameObject, String attributeName)
98 throws IllegalArgumentException
99 {
100 if (nameObject == null)
101 throw new IllegalArgumentException("Object name cannot be null");
102 if (attributeName == null)
103 throw new IllegalArgumentException("Attribute name cannot be null");
104
105 _states = new ConcurrentHashMap<Long,EventState<TYPE>>();
106
107 _objectName = nameObject.toString();
108 _attributeName = attributeName;
109
110 _nameObject = nameObject;
111 }
112
113
114
115
116
117
118
119
120
121
122
123 @SuppressWarnings("unchecked")
124 public final boolean match(long timestamp)
125 throws Exception
126 {
127 MBeanServerConnection serverConnection = JMXMonitor.getServiceConnection();
128
129 TYPE value = null;
130 try
131 {
132 int pos = _attributeName.indexOf('.');
133 if (pos < 0)
134 value = (TYPE)serverConnection.getAttribute(_nameObject,_attributeName);
135 else
136 value = getValue((CompositeData)serverConnection.getAttribute(_nameObject, _attributeName.substring(0, pos)),
137 _attributeName.substring(pos+1));
138 }
139 catch (Exception ex)
140 {
141 LOG.debug(ex);
142 }
143
144 boolean result = false;
145 if (value != null)
146 {
147 result = match(value);
148
149 if (result || getSaveAll())
150 {
151 _states.put(timestamp,
152 new EventState<TYPE>(this.getID(), this.getNameString(), value));
153 }
154 }
155
156 return result;
157 }
158
159
160
161
162
163
164
165
166
167 public boolean match(Comparable<TYPE> value)
168 {
169 return true;
170 }
171
172
173
174
175
176
177
178
179
180
181
182 @Override
183 public final EventState<TYPE> getState(long timestamp)
184 {
185 return _states.get(timestamp);
186 }
187
188
189
190
191
192
193
194
195
196
197 public String toString()
198 {
199 return getNameString();
200 }
201
202
203
204
205
206
207
208
209
210
211
212 protected String getNameString()
213 {
214 StringBuilder result = new StringBuilder();
215
216 result.append('[');
217 result.append(_objectName);
218 result.append(":");
219 result.append(_attributeName);
220 result.append("]");
221
222 return result.toString();
223 }
224
225 protected boolean getSaveAll()
226 {
227 return true;
228 }
229
230 protected TYPE getValue(CompositeData compValue, String fieldName)
231 {
232 int pos = fieldName.indexOf('.');
233 if (pos < 0)
234 return (TYPE)compValue.get(fieldName);
235 else
236 return getValue((CompositeData)compValue.get(fieldName.substring(0, pos)),
237 fieldName.substring(pos+1));
238
239 }
240 }