Showing the changes to java/pkg/javax/management/monitor/StringMonitor.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,463 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1999, 2007, Oracle and/or its affiliates. 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * 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 | @Override | ||
| 188 | public synchronized String getDerivedGauge(ObjectName object) { | ||
| 189 | return (String) super.getDerivedGauge(object); | ||
| 190 | } | ||
| 191 | |||
| 192 | /** | ||
| 193 | * Gets the derived gauge timestamp of the specified object, if | ||
| 194 | * this object is contained in the set of observed MBeans, or | ||
| 195 | * <code>0</code> otherwise. | ||
| 196 | * | ||
| 197 | * @param object the name of the object whose derived gauge | ||
| 198 | * timestamp is to be returned. | ||
| 199 | * | ||
| 200 | * @return The derived gauge timestamp of the specified object. | ||
| 201 | * | ||
| 202 | */ | ||
| 203 | @Override | ||
| 204 | public synchronized long getDerivedGaugeTimeStamp(ObjectName object) { | ||
| 205 | return super.getDerivedGaugeTimeStamp(object); | ||
| 206 | } | ||
| 207 | |||
| 208 | /** | ||
| 209 | * Returns the derived gauge of the first object in the set of | ||
| 210 | * observed MBeans. | ||
| 211 | * | ||
| 212 | * @return The derived gauge. | ||
| 213 | * | ||
| 214 | * @deprecated As of JMX 1.2, replaced by | ||
| 215 | * {@link #getDerivedGauge(ObjectName)} | ||
| 216 | */ | ||
| 217 | @Deprecated | ||
| 218 | public synchronized String getDerivedGauge() { | ||
| 219 | if (observedObjects.isEmpty()) { | ||
| 220 | return null; | ||
| 221 | } else { | ||
| 222 | return (String) observedObjects.get(0).getDerivedGauge(); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Gets the derived gauge timestamp of the first object in the set | ||
| 228 | * of observed MBeans. | ||
| 229 | * | ||
| 230 | * @return The derived gauge timestamp. | ||
| 231 | * | ||
| 232 | * @deprecated As of JMX 1.2, replaced by | ||
| 233 | * {@link #getDerivedGaugeTimeStamp(ObjectName)} | ||
| 234 | */ | ||
| 235 | @Deprecated | ||
| 236 | public synchronized long getDerivedGaugeTimeStamp() { | ||
| 237 | if (observedObjects.isEmpty()) { | ||
| 238 | return 0; | ||
| 239 | } else { | ||
| 240 | return observedObjects.get(0).getDerivedGaugeTimeStamp(); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | /** | ||
| 245 | * Gets the string to compare with the observed attribute common | ||
| 246 | * to all observed MBeans. | ||
| 247 | * | ||
| 248 | * @return The string value. | ||
| 249 | * | ||
| 250 | * @see #setStringToCompare | ||
| 251 | */ | ||
| 252 | public synchronized String getStringToCompare() { | ||
| 253 | return stringToCompare; | ||
| 254 | } | ||
| 255 | |||
| 256 | /** | ||
| 257 | * Sets the string to compare with the observed attribute common | ||
| 258 | * to all observed MBeans. | ||
| 259 | * | ||
| 260 | * @param value The string value. | ||
| 261 | * | ||
| 262 | * @exception IllegalArgumentException The specified | ||
| 263 | * string to compare is null. | ||
| 264 | * | ||
| 265 | * @see #getStringToCompare | ||
| 266 | */ | ||
| 267 | public synchronized void setStringToCompare(String value) | ||
| 268 | throws IllegalArgumentException { | ||
| 269 | |||
| 270 | if (value == null) { | ||
| 271 | throw new IllegalArgumentException("Null string to compare"); | ||
| 272 | } | ||
| 273 | |||
| 274 | if (stringToCompare.equals(value)) | ||
| 275 | return; | ||
| 276 | stringToCompare = value; | ||
| 277 | |||
| 278 | // Reset values. | ||
| 279 | // | ||
| 280 | for (ObservedObject o : observedObjects) { | ||
| 281 | final StringMonitorObservedObject smo = | ||
| 282 | (StringMonitorObservedObject) o; | ||
| 283 | smo.setStatus(MATCHING_OR_DIFFERING); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Gets the matching notification's on/off switch value common to | ||
| 289 | * all observed MBeans. | ||
| 290 | * | ||
| 291 | * @return <CODE>true</CODE> if the string monitor notifies when | ||
| 292 | * matching the string to compare, <CODE>false</CODE> otherwise. | ||
| 293 | * | ||
| 294 | * @see #setNotifyMatch | ||
| 295 | */ | ||
| 296 | public synchronized boolean getNotifyMatch() { | ||
| 297 | return notifyMatch; | ||
| 298 | } | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Sets the matching notification's on/off switch value common to | ||
| 302 | * all observed MBeans. | ||
| 303 | * | ||
| 304 | * @param value The matching notification's on/off switch value. | ||
| 305 | * | ||
| 306 | * @see #getNotifyMatch | ||
| 307 | */ | ||
| 308 | public synchronized void setNotifyMatch(boolean value) { | ||
| 309 | if (notifyMatch == value) | ||
| 310 | return; | ||
| 311 | notifyMatch = value; | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Gets the differing notification's on/off switch value common to | ||
| 316 | * all observed MBeans. | ||
| 317 | * | ||
| 318 | * @return <CODE>true</CODE> if the string monitor notifies when | ||
| 319 | * differing from the string to compare, <CODE>false</CODE> otherwise. | ||
| 320 | * | ||
| 321 | * @see #setNotifyDiffer | ||
| 322 | */ | ||
| 323 | public synchronized boolean getNotifyDiffer() { | ||
| 324 | return notifyDiffer; | ||
| 325 | } | ||
| 326 | |||
| 327 | /** | ||
| 328 | * Sets the differing notification's on/off switch value common to | ||
| 329 | * all observed MBeans. | ||
| 330 | * | ||
| 331 | * @param value The differing notification's on/off switch value. | ||
| 332 | * | ||
| 333 | * @see #getNotifyDiffer | ||
| 334 | */ | ||
| 335 | public synchronized void setNotifyDiffer(boolean value) { | ||
| 336 | if (notifyDiffer == value) | ||
| 337 | return; | ||
| 338 | notifyDiffer = value; | ||
| 339 | } | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Returns a <CODE>NotificationInfo</CODE> object containing the name of | ||
| 343 | * the Java class of the notification and the notification types sent by | ||
| 344 | * the string monitor. | ||
| 345 | */ | ||
| 346 | @Override | ||
| 347 | public MBeanNotificationInfo[] getNotificationInfo() { | ||
| 348 | return notifsInfo.clone(); | ||
| 349 | } | ||
| 350 | |||
| 351 | /* | ||
| 352 | * ------------------------------------------ | ||
| 353 | * PACKAGE METHODS | ||
| 354 | * ------------------------------------------ | ||
| 355 | */ | ||
| 356 | |||
| 357 | /** | ||
| 358 | * Factory method for ObservedObject creation. | ||
| 359 | * | ||
| 360 | * @since 1.6 | ||
| 361 | */ | ||
| 362 | @Override | ||
| 363 | ObservedObject createObservedObject(ObjectName object) { | ||
| 364 | final StringMonitorObservedObject smo = | ||
| 365 | new StringMonitorObservedObject(object); | ||
| 366 | smo.setStatus(MATCHING_OR_DIFFERING); | ||
| 367 | return smo; | ||
| 368 | } | ||
| 369 | |||
| 370 | /** | ||
| 371 | * Check that the type of the supplied observed attribute | ||
| 372 | * value is one of the value types supported by this monitor. | ||
| 373 | */ | ||
| 374 | @Override | ||
| 375 | synchronized boolean isComparableTypeValid(ObjectName object, | ||
| 376 | String attribute, | ||
| 377 | Comparable<?> value) { | ||
| 378 | // Check that the observed attribute is of type "String". | ||
| 379 | // | ||
| 380 | if (value instanceof String) { | ||
| 381 | return true; | ||
| 382 | } | ||
| 383 | return false; | ||
| 384 | } | ||
| 385 | |||
| 386 | @Override | ||
| 387 | synchronized void onErrorNotification(MonitorNotification notification) { | ||
| 388 | final StringMonitorObservedObject o = (StringMonitorObservedObject) | ||
| 389 | getObservedObject(notification.getObservedObject()); | ||
| 390 | if (o == null) | ||
| 391 | return; | ||
| 392 | |||
| 393 | // Reset values. | ||
| 394 | // | ||
| 395 | o.setStatus(MATCHING_OR_DIFFERING); | ||
| 396 | } | ||
| 397 | |||
| 398 | @Override | ||
| 399 | synchronized MonitorNotification buildAlarmNotification( | ||
| 400 | ObjectName object, | ||
| 401 | String attribute, | ||
| 402 | Comparable<?> value) { | ||
| 403 | String type = null; | ||
| 404 | String msg = null; | ||
| 405 | Object trigger = null; | ||
| 406 | |||
| 407 | final StringMonitorObservedObject o = | ||
| 408 | (StringMonitorObservedObject) getObservedObject(object); | ||
| 409 | if (o == null) | ||
| 410 | return null; | ||
| 411 | |||
| 412 | // Send matching notification if notifyMatch is true. | ||
| 413 | // Send differing notification if notifyDiffer is true. | ||
| 414 | // | ||
| 415 | if (o.getStatus() == MATCHING_OR_DIFFERING) { | ||
| 416 | if (o.getDerivedGauge().equals(stringToCompare)) { | ||
| 417 | if (notifyMatch) { | ||
| 418 | type = STRING_TO_COMPARE_VALUE_MATCHED; | ||
| 419 | msg = ""; | ||
| 420 | trigger = stringToCompare; | ||
| 421 | } | ||
| 422 | o.setStatus(DIFFERING); | ||
| 423 | } else { | ||
| 424 | if (notifyDiffer) { | ||
| 425 | type = STRING_TO_COMPARE_VALUE_DIFFERED; | ||
| 426 | msg = ""; | ||
| 427 | trigger = stringToCompare; | ||
| 428 | } | ||
| 429 | o.setStatus(MATCHING); | ||
| 430 | } | ||
| 431 | } else { | ||
| 432 | if (o.getStatus() == MATCHING) { | ||
| 433 | if (o.getDerivedGauge().equals(stringToCompare)) { | ||
| 434 | if (notifyMatch) { | ||
| 435 | type = STRING_TO_COMPARE_VALUE_MATCHED; | ||
| 436 | msg = ""; | ||
| 437 | trigger = stringToCompare; | ||
| 438 | } | ||
| 439 | o.setStatus(DIFFERING); | ||
| 440 | } | ||
| 441 | } else if (o.getStatus() == DIFFERING) { | ||
| 442 | if (!o.getDerivedGauge().equals(stringToCompare)) { | ||
| 443 | if (notifyDiffer) { | ||
| 444 | type = STRING_TO_COMPARE_VALUE_DIFFERED; | ||
| 445 | msg = ""; | ||
| 446 | trigger = stringToCompare; | ||
| 447 | } | ||
| 448 | o.setStatus(MATCHING); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | return new MonitorNotification(type, | ||
| 454 | this, | ||
| 455 | 0, | ||
| 456 | 0, | ||
| 457 | msg, | ||
| 458 | null, | ||
| 459 | null, | ||
| 460 | null, | ||
| 461 | trigger); | ||
| 462 | } | ||
| 463 | } | ||