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