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