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.jmx;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  
27  /* ------------------------------------------------------------ */
28  /**
29   * EventState
30   * 
31   * Holds the state of one or more {@link org.eclipse.jetty.monitor.jmx.EventTrigger event trigger}
32   * instances to be used when sending notifications as well as executing the actions
33   * @param <TYPE> the event trigger type
34   */
35  public class EventState<TYPE>
36  {
37      
38      /* ------------------------------------------------------------ */
39      /**
40       * State
41       * 
42       * Holds the state of a single {@link org.eclipse.jetty.monitor.jmx.EventTrigger event trigger}
43       * @param <TYPE> the event trigger type
44       */
45      public static class TriggerState<TYPE>
46      {
47          private final String _id;
48          private final String _desc;
49          private final TYPE _value;
50          
51          /* ------------------------------------------------------------ */
52          /**
53           * Construct a trigger state 
54           * 
55           * @param id unique identification string of the associated event trigger
56           * @param desc description of the associated event trigger
57           * @param value effective value of the MXBean attribute (if applicable)
58           */
59          public TriggerState(String id, String desc, TYPE value)
60          {
61              _id = id;
62              _desc = desc;
63              _value = value;
64          }
65          
66          /* ------------------------------------------------------------ */
67          /**
68           * Retrieve the identification string of associated event trigger
69           * 
70           * @return unique identification string
71           */
72          public String getID()
73          {
74              return _id;
75          }
76          
77          /* ------------------------------------------------------------ */
78          /**
79           * Retrieve the description string set by event trigger
80           * 
81           * @return description string
82           */
83          public String getDescription()
84          {
85              return _desc;
86          }
87          
88          /* ------------------------------------------------------------ */
89          /**
90           * Retrieve the effective value of the MXBean attribute (if applicable)
91           * 
92           * @return attribute value
93           */
94          public TYPE getValue()
95          {
96              return _value;
97          }
98          
99          /* ------------------------------------------------------------ */
100         /**
101          * @return string representation of the state
102          */
103         public String toString()
104         {
105             StringBuilder result = new StringBuilder();
106            
107             result.append(_desc);
108             result.append('=');
109             result.append(_value);
110             
111             return result.toString();
112         }
113     }
114     
115     protected Map<String, TriggerState<TYPE>> _states;
116     
117     /* ------------------------------------------------------------ */
118     /**
119      * Constructs an empty event state
120      */
121     public EventState()
122     {
123         _states = new ConcurrentHashMap<String, TriggerState<TYPE>>();
124     }
125     
126 
127     /* ------------------------------------------------------------ */
128     /**
129      * Constructs an event state and adds a specified trigger state to it
130      * 
131      * @param id unique identification string of the associated event trigger
132      * @param desc description of the associated event trigger
133      * @param value effective value of the MXBean attribute (if applicable)
134      */
135     public EventState(String id, String desc, TYPE value)
136     {
137         this();
138         
139         add(new TriggerState<TYPE>(id, desc, value));
140     }
141 
142     /* ------------------------------------------------------------ */
143     /**
144      * Adds a trigger state to the event state
145      * 
146      * @param state trigger state to add
147      */
148     public void add(TriggerState<TYPE> state)
149     {
150         _states.put(state.getID(), state);
151     }
152     
153     /* ------------------------------------------------------------ */
154     /**
155      * Adds a collection of trigger states to the event state
156      * 
157      * @param entries collection of trigger states to add
158      */
159     public void addAll(Collection<TriggerState<TYPE>> entries)
160     {
161         for (TriggerState<TYPE> entry : entries)
162         {
163             add(entry);
164         }
165     }
166 
167     /* ------------------------------------------------------------ */
168     /**
169      * Retrieves a single trigger state
170      * 
171      * @param id unique identification string of the event trigger
172      * @return requested trigger state or null if not found
173      */
174     public TriggerState<TYPE> get(String id)
175     {
176         return _states.get(id);
177     }
178 
179     /* ------------------------------------------------------------ */
180     /**
181      * Retrieves a collection of all trigger states of the event state
182      * 
183      * @return collection of the trigger states
184      */
185     public Collection<TriggerState<TYPE>> values()
186     {
187         return Collections.unmodifiableCollection(_states.values());
188     }
189     
190     /* ------------------------------------------------------------ */
191     /**
192      * Returns a string representation of the event state
193      * 
194      * @return string representation of the event state
195      */
196     public String toString()
197     {
198         int cnt = 0;
199         StringBuilder result = new StringBuilder();
200         
201         for (TriggerState<TYPE> value : _states.values())
202         {
203             result.append(cnt++>0?"#":"");
204             result.append(value.toString());
205         }
206         
207         return result.toString();
208     }
209 }