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