View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.monitor.triggers;
20  
21  import javax.management.MalformedObjectNameException;
22  
23  /**
24   * RangeInclAttrEventTrigger
25   * <p>
26   * Event trigger that polls a value of an MXBean attribute and
27   * checks if it is in a range from specified min value to
28   * specified max value including the range bounds. 
29   * @param <TYPE> event trigger type
30   */
31  public class RangeInclAttrEventTrigger<TYPE extends Comparable<TYPE>> extends AttrEventTrigger<TYPE>
32  {
33      protected final TYPE _min;
34      protected final TYPE _max;
35      
36      /* ------------------------------------------------------------ */
37      /**
38       * Construct event trigger and specify the MXBean attribute
39       * that will be polled by this event trigger as well as min
40       * and max value of the attribute.
41       * 
42       * @param objectName object name of an MBean to be polled
43       * @param attributeName name of an MBean attribute to be polled
44       * @param min minimum value of the attribute
45       * @param max maximum value of the attribute
46       * 
47       * @throws MalformedObjectNameException on bad object name
48       * @throws IllegalArgumentException on bad parameters
49       */
50      public RangeInclAttrEventTrigger(String objectName, String attributeName,TYPE min, TYPE max)
51          throws MalformedObjectNameException, IllegalArgumentException
52      {
53          super(objectName,attributeName);
54          
55          if (min == null)
56              throw new IllegalArgumentException("Value cannot be null");
57          if (max == null)
58              throw new IllegalArgumentException("Value cannot be null");
59  
60          _min = min;
61          _max = max;
62      }
63  
64      /* ------------------------------------------------------------ */
65      /**
66       * Compare the value of the MXBean attribute being polling
67       * to check if it is in a range from specified min value to
68       * specified max value including the range bounds. 
69       * 
70       * @see org.eclipse.jetty.monitor.triggers.AttrEventTrigger#match(java.lang.Comparable)
71       */
72      @Override
73      public boolean match(Comparable<TYPE> value)
74      {
75          return (value.compareTo(_min) >= 0) &&(value.compareTo(_max) <= 0);
76      }
77  
78      /* ------------------------------------------------------------ */
79      /**
80       * Returns the string representation of this event trigger
81       * in the format "min&lt;=name&lt;=max". 
82       * 
83       * @return string representation of the event trigger
84       * 
85       * @see java.lang.Object#toString()
86       */
87      public String toString()
88      {
89          StringBuilder result = new StringBuilder();
90          
91          result.append(_min);
92          result.append("<=");
93          result.append(getNameString());
94          result.append("<=");
95          result.append(_max);
96          
97          return result.toString();
98      }
99  }