1 // ========================================================================
2 // Copyright (c) Webtide LLC
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 * AttrEventTrigger
35 *
36 * Event trigger that polls a value of an MXBean attribute
37 * and matches every invocation of this trigger. It can be
38 * used to send notifications of the value of an attribute
39 * of the MXBean being polled at a certain interval, or as
40 * a base class for the event triggers that match the
41 * value of an attribute of the MXBean being polled against
42 * some specified criteria.
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 * Construct event trigger and specify the MXBean attribute
58 * that will be polled by this event trigger.
59 *
60 * @param objectName object name of an MBean to be polled
61 * @param attributeName name of an MBean attribute to be polled
62 *
63 * @throws MalformedObjectNameException
64 * @throws IllegalArgumentException
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 * Construct event trigger and specify the MXBean attribute
85 * that will be polled by this event trigger.
86 *
87 * @param nameObject object name of an MBean to be polled
88 * @param attributeName name of an MBean attribute to be polled
89 *
90 * @throws IllegalArgumentException
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 * Verify if the event trigger conditions are in the
111 * appropriate state for an event to be triggered.
112 * This event trigger uses the match(Comparable<TYPE>)
113 * method to compare the value of the MXBean attribute
114 * to the conditions specified by the subclasses.
115 *
116 * @see org.eclipse.jetty.monitor.jmx.EventTrigger#match(long)
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 * Verify if the event trigger conditions are in the
158 * appropriate state for an event to be triggered.
159 * Allows subclasses to override the default behavior
160 * that matches every invocation of this trigger
161 */
162 public boolean match(Comparable<TYPE> value)
163 {
164 return true;
165 }
166
167 /* ------------------------------------------------------------ */
168 /**
169 * Retrieve the event state associated with specified invocation
170 * of the event trigger match method.
171 *
172 * @param timestamp time stamp associated with invocation
173 * @return event state or null if not found
174 *
175 * @see org.eclipse.jetty.monitor.jmx.EventTrigger#getState(long)
176 */
177 @Override
178 public final EventState<TYPE> getState(long timestamp)
179 {
180 return _states.get(timestamp);
181 }
182
183 /* ------------------------------------------------------------ */
184 /**
185 * Returns the string representation of this event trigger
186 * in the format "[object_name:attribute_name]".
187 *
188 * @return string representation of the event trigger
189 *
190 * @see java.lang.Object#toString()
191 */
192 public String toString()
193 {
194 return getNameString();
195 }
196
197 /* ------------------------------------------------------------ */
198 /**
199 * Returns the string representation of this event trigger
200 * in the format "[object_name:attribute_name]". Allows
201 * subclasses to override the name string used to identify
202 * this event trigger in the event state object as well as
203 * string representation of the subclasses.
204 *
205 * @return string representation of the event trigger
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 }