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 javax.management.MalformedObjectNameException;
18
19
20 /* ------------------------------------------------------------ */
21 /**
22 * RangeInclAttrEventTrigger
23 *
24 * Event trigger that polls a value of an MXBean attribute and
25 * checks if it is in a range from specified min value to
26 * specified max value including the range bounds.
27 */
28 public class RangeInclAttrEventTrigger<TYPE extends Comparable<TYPE>> extends AttrEventTrigger<TYPE>
29 {
30 protected final TYPE _min;
31 protected final TYPE _max;
32
33 /* ------------------------------------------------------------ */
34 /**
35 * Construct event trigger and specify the MXBean attribute
36 * that will be polled by this event trigger as well as min
37 * and max value of the attribute.
38 *
39 * @param objectName object name of an MBean to be polled
40 * @param attributeName name of an MBean attribute to be polled
41 * @param min minimum value of the attribute
42 * @param max maximum value of the attribute
43 *
44 * @throws MalformedObjectNameException
45 * @throws IllegalArgumentException
46 */
47 public RangeInclAttrEventTrigger(String objectName, String attributeName,TYPE min, TYPE max)
48 throws MalformedObjectNameException, IllegalArgumentException
49 {
50 super(objectName,attributeName);
51
52 if (min == null)
53 throw new IllegalArgumentException("Value cannot be null");
54 if (max == null)
55 throw new IllegalArgumentException("Value cannot be null");
56
57 _min = min;
58 _max = max;
59 }
60
61 /* ------------------------------------------------------------ */
62 /**
63 * Compare the value of the MXBean attribute being polling
64 * to check if it is in a range from specified min value to
65 * specified max value including the range bounds.
66 *
67 * @see org.eclipse.jetty.monitor.triggers.AttrEventTrigger#match(java.lang.Comparable)
68 */
69 @Override
70 public boolean match(Comparable<TYPE> value)
71 {
72 return (value.compareTo(_min) >= 0) &&(value.compareTo(_max) <= 0);
73 }
74
75 /* ------------------------------------------------------------ */
76 /**
77 * Returns the string representation of this event trigger
78 * in the format "min<=name<=max".
79 *
80 * @return string representation of the event trigger
81 *
82 * @see java.lang.Object#toString()
83 */
84 public String toString()
85 {
86 StringBuilder result = new StringBuilder();
87
88 result.append(_min);
89 result.append("<=");
90 result.append(getNameString());
91 result.append("<=");
92 result.append(_max);
93
94 return result.toString();
95 }
96 }