1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.monitor.triggers;
20
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import javax.management.MBeanServerConnection;
25 import javax.management.MalformedObjectNameException;
26 import javax.management.ObjectName;
27 import javax.management.openmbean.CompositeData;
28
29 import org.eclipse.jetty.monitor.JMXMonitor;
30 import org.eclipse.jetty.monitor.jmx.EventState;
31 import org.eclipse.jetty.monitor.jmx.EventTrigger;
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class AttrEventTrigger<TYPE extends Comparable<TYPE>>
48 extends EventTrigger
49 {
50 private static final Logger LOG = Log.getLogger(AttrEventTrigger.class);
51
52 private final ObjectName _nameObject;
53
54 protected final String _objectName;
55 protected final String _attributeName;
56 protected Map<Long, EventState<TYPE>> _states;
57
58
59
60
61
62
63
64
65
66
67
68
69 public AttrEventTrigger(String objectName, String attributeName)
70 throws MalformedObjectNameException, IllegalArgumentException
71 {
72 if (objectName == null)
73 throw new IllegalArgumentException("Object name cannot be null");
74 if (attributeName == null)
75 throw new IllegalArgumentException("Attribute name cannot be null");
76
77 _states = new ConcurrentHashMap<Long,EventState<TYPE>>();
78
79 _objectName = objectName;
80 _attributeName = attributeName;
81
82 _nameObject = new ObjectName(_objectName);
83 }
84
85
86
87
88
89
90
91
92
93
94
95 public AttrEventTrigger(ObjectName nameObject, String attributeName)
96 throws IllegalArgumentException
97 {
98 if (nameObject == null)
99 throw new IllegalArgumentException("Object name cannot be null");
100 if (attributeName == null)
101 throw new IllegalArgumentException("Attribute name cannot be null");
102
103 _states = new ConcurrentHashMap<Long,EventState<TYPE>>();
104
105 _objectName = nameObject.toString();
106 _attributeName = attributeName;
107
108 _nameObject = nameObject;
109 }
110
111
112
113
114
115
116
117
118
119
120
121 @SuppressWarnings("unchecked")
122 public final boolean match(long timestamp)
123 throws Exception
124 {
125 MBeanServerConnection serverConnection = JMXMonitor.getServiceConnection();
126
127 TYPE value = null;
128 try
129 {
130 int pos = _attributeName.indexOf('.');
131 if (pos < 0)
132 value = (TYPE)serverConnection.getAttribute(_nameObject,_attributeName);
133 else
134 value = getValue((CompositeData)serverConnection.getAttribute(_nameObject, _attributeName.substring(0, pos)),
135 _attributeName.substring(pos+1));
136 }
137 catch (Exception ex)
138 {
139 LOG.debug(ex);
140 }
141
142 boolean result = false;
143 if (value != null)
144 {
145 result = match(value);
146
147 if (result || getSaveAll())
148 {
149 _states.put(timestamp,
150 new EventState<TYPE>(this.getID(), this.getNameString(), value));
151 }
152 }
153
154 return result;
155 }
156
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 }