J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.management.monitor; |
| 27 | |
| 28 | import static com.sun.jmx.defaults.JmxProperties.MONITOR_LOGGER; |
| 29 | import java.util.logging.Level; |
| 30 | import javax.management.ObjectName; |
| 31 | import javax.management.MBeanNotificationInfo; |
| 32 | import static javax.management.monitor.MonitorNotification.*; |
| 33 | |
| 34 | /** |
| 35 | * Defines a monitor MBean designed to observe the values of a string |
| 36 | * attribute. |
| 37 | * <P> |
| 38 | * A string monitor sends notifications as follows: |
| 39 | * <UL> |
| 40 | * <LI> if the attribute value matches the string to compare value, |
| 41 | * a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_MATCHED |
| 42 | * match notification} is sent. |
| 43 | * The notify match flag must be set to <CODE>true</CODE>. |
| 44 | * <BR>Subsequent matchings of the string to compare values do not |
| 45 | * cause further notifications unless |
| 46 | * the attribute value differs from the string to compare value. |
| 47 | * <LI> if the attribute value differs from the string to compare value, |
| 48 | * a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_DIFFERED |
| 49 | * differ notification} is sent. |
| 50 | * The notify differ flag must be set to <CODE>true</CODE>. |
| 51 | * <BR>Subsequent differences from the string to compare value do |
| 52 | * not cause further notifications unless |
| 53 | * the attribute value matches the string to compare value. |
| 54 | * </UL> |
| 55 | * |
| 56 | * |
| 57 | * @since 1.5 |
| 58 | */ |
| 59 | public class StringMonitor extends Monitor implements StringMonitorMBean { |
| 60 | |
| 61 | /* |
| 62 | * ------------------------------------------ |
| 63 | * PACKAGE CLASSES |
| 64 | * ------------------------------------------ |
| 65 | */ |
| 66 | |
| 67 | static class StringMonitorObservedObject extends ObservedObject { |
| 68 | |
| 69 | public StringMonitorObservedObject(ObjectName observedObject) { |
| 70 | super(observedObject); |
| 71 | } |
| 72 | |
| 73 | public final synchronized int getStatus() { |
| 74 | return status; |
| 75 | } |
| 76 | public final synchronized void setStatus(int status) { |
| 77 | this.status = status; |
| 78 | } |
| 79 | |
| 80 | private int status; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * ------------------------------------------ |
| 85 | * PRIVATE VARIABLES |
| 86 | * ------------------------------------------ |
| 87 | */ |
| 88 | |
| 89 | /** |
| 90 | * String to compare with the observed attribute. |
| 91 | * <BR>The default value is an empty character sequence. |
| 92 | */ |
| 93 | private String stringToCompare = ""; |
| 94 | |
| 95 | /** |
| 96 | * Flag indicating if the string monitor notifies when matching |
| 97 | * the string to compare. |
| 98 | * <BR>The default value is set to <CODE>false</CODE>. |
| 99 | */ |
| 100 | private boolean notifyMatch = false; |
| 101 | |
| 102 | /** |
| 103 | * Flag indicating if the string monitor notifies when differing |
| 104 | * from the string to compare. |
| 105 | * <BR>The default value is set to <CODE>false</CODE>. |
| 106 | */ |
| 107 | private boolean notifyDiffer = false; |
| 108 | |
| 109 | private static final String[] types = { |
| 110 | RUNTIME_ERROR, |
| 111 | OBSERVED_OBJECT_ERROR, |
| 112 | OBSERVED_ATTRIBUTE_ERROR, |
| 113 | OBSERVED_ATTRIBUTE_TYPE_ERROR, |
| 114 | STRING_TO_COMPARE_VALUE_MATCHED, |
| 115 | STRING_TO_COMPARE_VALUE_DIFFERED |
| 116 | }; |
| 117 | |
| 118 | private static final MBeanNotificationInfo[] notifsInfo = { |
| 119 | new MBeanNotificationInfo( |
| 120 | types, |
| 121 | "javax.management.monitor.MonitorNotification", |
| 122 | "Notifications sent by the StringMonitor MBean") |
| 123 | }; |
| 124 | |
| 125 | // Flags needed to implement the matching/differing mechanism. |
| 126 | // |
| 127 | private static final int MATCHING = 0; |
| 128 | private static final int DIFFERING = 1; |
| 129 | private static final int MATCHING_OR_DIFFERING = 2; |
| 130 | |
| 131 | /* |
| 132 | * ------------------------------------------ |
| 133 | * CONSTRUCTORS |
| 134 | * ------------------------------------------ |
| 135 | */ |
| 136 | |
| 137 | /** |
| 138 | * Default constructor. |
| 139 | */ |
| 140 | public StringMonitor() { |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * ------------------------------------------ |
| 145 | * PUBLIC METHODS |
| 146 | * ------------------------------------------ |
| 147 | */ |
| 148 | |
| 149 | /** |
| 150 | * Starts the string monitor. |
| 151 | */ |
| 152 | public synchronized void start() { |
| 153 | if (isActive()) { |
| 154 | MONITOR_LOGGER.logp(Level.FINER, StringMonitor.class.getName(), |
| 155 | "start", "the monitor is already active"); |
| 156 | return; |
| 157 | } |
| 158 | // Reset values. |
| 159 | // |
| 160 | for (ObservedObject o : observedObjects) { |
| 161 | final StringMonitorObservedObject smo = |
| 162 | (StringMonitorObservedObject) o; |
| 163 | smo.setStatus(MATCHING_OR_DIFFERING); |
| 164 | } |
| 165 | doStart(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Stops the string monitor. |
| 170 | */ |
| 171 | public synchronized void stop() { |
| 172 | doStop(); |
| 173 | } |
| 174 | |
| 175 | // GETTERS AND SETTERS |
| 176 | //-------------------- |
| 177 | |
| 178 | /** |
| 179 | * Gets the derived gauge of the specified object, if this object is |
| 180 | * contained in the set of observed MBeans, or <code>null</code> otherwise. |
| 181 | * |
| 182 | * @param object the name of the MBean whose derived gauge is required. |
| 183 | * |
| 184 | * @return The derived gauge of the specified object. |
| 185 | * |
| 186 | */ |
| 187 | public synchronized String getDerivedGauge(ObjectName object) { |
| 188 | return (String) super.getDerivedGauge(object); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets the derived gauge timestamp of the specified object, if |
| 193 | * this object is contained in the set of observed MBeans, or |
| 194 | * <code>0</code> otherwise. |
| 195 | * |
| 196 | * @param object the name of the object whose derived gauge |
| 197 | * timestamp is to be returned. |
| 198 | * |
| 199 | * @return The derived gauge timestamp of the specified object. |
| 200 | * |
| 201 | */ |
| 202 | public synchronized long getDerivedGaugeTimeStamp(ObjectName object) { |
| 203 | return super.getDerivedGaugeTimeStamp(object); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the derived gauge of the first object in the set of |
| 208 | * observed MBeans. |
| 209 | * |
| 210 | * @return The derived gauge. |
| 211 | * |
| 212 | * @deprecated As of JMX 1.2, replaced by |
| 213 | * {@link #getDerivedGauge(ObjectName)} |
| 214 | */ |
| 215 | @Deprecated |
| 216 | public synchronized String getDerivedGauge() { |
| 217 | if (observedObjects.isEmpty()) { |
| 218 | return null; |
| 219 | } else { |
| 220 | return (String) observedObjects.get(0).getDerivedGauge(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Gets the derived gauge timestamp of the first object in the set |
| 226 | * of observed MBeans. |
| 227 | * |
| 228 | * @return The derived gauge timestamp. |
| 229 | * |
| 230 | * @deprecated As of JMX 1.2, replaced by |
| 231 | * {@link #getDerivedGaugeTimeStamp(ObjectName)} |
| 232 | */ |
| 233 | @Deprecated |
| 234 | public synchronized long getDerivedGaugeTimeStamp() { |
| 235 | if (observedObjects.isEmpty()) { |
| 236 | return 0; |
| 237 | } else { |
| 238 | return observedObjects.get(0).getDerivedGaugeTimeStamp(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Gets the string to compare with the observed attribute common |
| 244 | * to all observed MBeans. |
| 245 | * |
| 246 | * @return The string value. |
| 247 | * |
| 248 | * @see #setStringToCompare |
| 249 | */ |
| 250 | public synchronized String getStringToCompare() { |
| 251 | return stringToCompare; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Sets the string to compare with the observed attribute common |
| 256 | * to all observed MBeans. |
| 257 | * |
| 258 | * @param value The string value. |
| 259 | * |
| 260 | * @exception IllegalArgumentException The specified |
| 261 | * string to compare is null. |
| 262 | * |
| 263 | * @see #getStringToCompare |
| 264 | */ |
| 265 | public synchronized void setStringToCompare(String value) |
| 266 | throws IllegalArgumentException { |
| 267 | |
| 268 | if (value == null) { |
| 269 | throw new IllegalArgumentException("Null string to compare"); |
| 270 | } |
| 271 | |
| 272 | if (stringToCompare.equals(value)) |
| 273 | return; |
| 274 | stringToCompare = value; |
| 275 | |
| 276 | // Reset values. |
| 277 | // |
| 278 | for (ObservedObject o : observedObjects) { |
| 279 | final StringMonitorObservedObject smo = |
| 280 | (StringMonitorObservedObject) o; |
| 281 | smo.setStatus(MATCHING_OR_DIFFERING); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Gets the matching notification's on/off switch value common to |
| 287 | * all observed MBeans. |
| 288 | * |
| 289 | * @return <CODE>true</CODE> if the string monitor notifies when |
| 290 | * matching the string to compare, <CODE>false</CODE> otherwise. |
| 291 | * |
| 292 | * @see #setNotifyMatch |
| 293 | */ |
| 294 | public synchronized boolean getNotifyMatch() { |
| 295 | return notifyMatch; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sets the matching notification's on/off switch value common to |
| 300 | * all observed MBeans. |
| 301 | * |
| 302 | * @param value The matching notification's on/off switch value. |
| 303 | * |
| 304 | * @see #getNotifyMatch |
| 305 | */ |
| 306 | public synchronized void setNotifyMatch(boolean value) { |
| 307 | if (notifyMatch == value) |
| 308 | return; |
| 309 | notifyMatch = value; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Gets the differing notification's on/off switch value common to |
| 314 | * all observed MBeans. |
| 315 | * |
| 316 | * @return <CODE>true</CODE> if the string monitor notifies when |
| 317 | * differing from the string to compare, <CODE>false</CODE> otherwise. |
| 318 | * |
| 319 | * @see #setNotifyDiffer |
| 320 | */ |
| 321 | public synchronized boolean getNotifyDiffer() { |
| 322 | return notifyDiffer; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Sets the differing notification's on/off switch value common to |
| 327 | * all observed MBeans. |
| 328 | * |
| 329 | * @param value The differing notification's on/off switch value. |
| 330 | * |
| 331 | * @see #getNotifyDiffer |
| 332 | */ |
| 333 | public synchronized void setNotifyDiffer(boolean value) { |
| 334 | if (notifyDiffer == value) |
| 335 | return; |
| 336 | notifyDiffer = value; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns a <CODE>NotificationInfo</CODE> object containing the name of |
| 341 | * the Java class of the notification and the notification types sent by |
| 342 | * the string monitor. |
| 343 | */ |
| 344 | public MBeanNotificationInfo[] getNotificationInfo() { |
| 345 | return notifsInfo; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * ------------------------------------------ |
| 350 | * PACKAGE METHODS |
| 351 | * ------------------------------------------ |
| 352 | */ |
| 353 | |
| 354 | /** |
| 355 | * Factory method for ObservedObject creation. |
| 356 | * |
| 357 | * @since 1.6 |
| 358 | */ |
| 359 | @Override |
| 360 | ObservedObject createObservedObject(ObjectName object) { |
| 361 | final StringMonitorObservedObject smo = |
| 362 | new StringMonitorObservedObject(object); |
| 363 | smo.setStatus(MATCHING_OR_DIFFERING); |
| 364 | return smo; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Check that the type of the supplied observed attribute |
| 369 | * value is one of the value types supported by this monitor. |
| 370 | */ |
| 371 | @Override |
| 372 | synchronized boolean isComparableTypeValid(ObjectName object, |
| 373 | String attribute, |
| 374 | Comparable<?> value) { |
| 375 | // Check that the observed attribute is of type "String". |
| 376 | // |
| 377 | if (value instanceof String) { |
| 378 | return true; |
| 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | synchronized void onErrorNotification(MonitorNotification notification) { |
| 385 | final StringMonitorObservedObject o = (StringMonitorObservedObject) |
| 386 | getObservedObject(notification.getObservedObject()); |
| 387 | if (o == null) |
| 388 | return; |
| 389 | |
| 390 | // Reset values. |
| 391 | // |
| 392 | o.setStatus(MATCHING_OR_DIFFERING); |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | synchronized MonitorNotification buildAlarmNotification( |
| 397 | ObjectName object, |
| 398 | String attribute, |
| 399 | Comparable<?> value) { |
| 400 | String type = null; |
| 401 | String msg = null; |
| 402 | Object trigger = null; |
| 403 | |
| 404 | final StringMonitorObservedObject o = |
| 405 | (StringMonitorObservedObject) getObservedObject(object); |
| 406 | if (o == null) |
| 407 | return null; |
| 408 | |
| 409 | // Send matching notification if notifyMatch is true. |
| 410 | // Send differing notification if notifyDiffer is true. |
| 411 | // |
| 412 | if (o.getStatus() == MATCHING_OR_DIFFERING) { |
| 413 | if (o.getDerivedGauge().equals(stringToCompare)) { |
| 414 | if (notifyMatch) { |
| 415 | type = STRING_TO_COMPARE_VALUE_MATCHED; |
| 416 | msg = ""; |
| 417 | trigger = stringToCompare; |
| 418 | } |
| 419 | o.setStatus(DIFFERING); |
| 420 | } else { |
| 421 | if (notifyDiffer) { |
| 422 | type = STRING_TO_COMPARE_VALUE_DIFFERED; |
| 423 | msg = ""; |
| 424 | trigger = stringToCompare; |
| 425 | } |
| 426 | o.setStatus(MATCHING); |
| 427 | } |
| 428 | } else { |
| 429 | if (o.getStatus() == MATCHING) { |
| 430 | if (o.getDerivedGauge().equals(stringToCompare)) { |
| 431 | if (notifyMatch) { |
| 432 | type = STRING_TO_COMPARE_VALUE_MATCHED; |
| 433 | msg = ""; |
| 434 | trigger = stringToCompare; |
| 435 | } |
| 436 | o.setStatus(DIFFERING); |
| 437 | } |
| 438 | } else if (o.getStatus() == DIFFERING) { |
| 439 | if (!o.getDerivedGauge().equals(stringToCompare)) { |
| 440 | if (notifyDiffer) { |
| 441 | type = STRING_TO_COMPARE_VALUE_DIFFERED; |
| 442 | msg = ""; |
| 443 | trigger = stringToCompare; |
| 444 | } |
| 445 | o.setStatus(MATCHING); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return new MonitorNotification(type, |
| 451 | this, |
| 452 | 0, |
| 453 | 0, |
| 454 | msg, |
| 455 | null, |
| 456 | null, |
| 457 | null, |
| 458 | trigger); |
| 459 | } |
| 460 | } |