| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
package com.sun.grizzly.util; |
| 40 | |
|
| 41 | |
import com.sun.grizzly.Controller; |
| 42 | |
import java.util.Iterator; |
| 43 | |
import java.util.Map; |
| 44 | |
import java.util.concurrent.Callable; |
| 45 | |
import java.util.concurrent.ConcurrentHashMap; |
| 46 | |
import java.util.concurrent.CountDownLatch; |
| 47 | |
import java.util.concurrent.atomic.AtomicReference; |
| 48 | |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 49 | |
import java.util.logging.Level; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public class StateHolder<E> { |
| 58 | |
private AtomicReference<E> stateRef; |
| 59 | |
|
| 60 | |
private ReentrantReadWriteLock readWriteLock; |
| 61 | |
private volatile boolean isLockEnabled; |
| 62 | |
|
| 63 | |
private Map<ConditionListener<E>, Object> conditionListeners; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public StateHolder() { |
| 70 | 0 | this(false); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 229 | public StateHolder(boolean isLockEnabled) { |
| 78 | 229 | stateRef = new AtomicReference<E>(); |
| 79 | 229 | readWriteLock = new ReentrantReadWriteLock(); |
| 80 | 229 | conditionListeners = new ConcurrentHashMap<ConditionListener<E>, Object>(); |
| 81 | 229 | this.isLockEnabled = isLockEnabled; |
| 82 | 229 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public E getState() { |
| 90 | 261 | return getState(isLockEnabled); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public E getState(boolean locked) { |
| 99 | 1598385 | if (locked) { |
| 100 | 261 | readWriteLock.readLock().lock(); |
| 101 | |
} |
| 102 | |
|
| 103 | 1598385 | E retState = stateRef.get(); |
| 104 | |
|
| 105 | 1598385 | if (locked) { |
| 106 | 261 | readWriteLock.readLock().unlock(); |
| 107 | |
} |
| 108 | 1598385 | return retState; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public void setState(E state) { |
| 117 | 1284 | setState(state, isLockEnabled); |
| 118 | 1284 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public void setState(E state, boolean locked) { |
| 126 | 1422 | if (locked) { |
| 127 | 1284 | readWriteLock.writeLock().lock(); |
| 128 | |
} |
| 129 | |
|
| 130 | 1422 | stateRef.set(state); |
| 131 | |
|
| 132 | |
|
| 133 | 1422 | if (locked) { |
| 134 | 1284 | readWriteLock.readLock().lock(); |
| 135 | 1284 | readWriteLock.writeLock().unlock(); |
| 136 | |
} |
| 137 | |
|
| 138 | 1422 | checkConditionListeners(state); |
| 139 | |
|
| 140 | 1422 | if (locked) { |
| 141 | 1284 | readWriteLock.readLock().unlock(); |
| 142 | |
} |
| 143 | 1422 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public ReentrantReadWriteLock getStateLocker() { |
| 150 | 394 | return readWriteLock; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public boolean isLockEnabled() { |
| 158 | 0 | return isLockEnabled; |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public void setLockEnabled(boolean isLockEnabled) { |
| 166 | 0 | this.isLockEnabled = isLockEnabled; |
| 167 | 0 | } |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public ConditionListener<E> notifyWhenStateIsEqual(E state, Object listener) { |
| 182 | 6 | boolean isLockEnabledLocal = isLockEnabled; |
| 183 | 6 | if (isLockEnabledLocal) { |
| 184 | 6 | getStateLocker().writeLock().lock(); |
| 185 | |
} |
| 186 | |
|
| 187 | 6 | ConditionListener<E> conditionListener = null; |
| 188 | |
|
| 189 | 6 | if (stateRef.get().equals(state)) { |
| 190 | 1 | EventListener.notifyListener(listener); |
| 191 | |
} else { |
| 192 | 5 | conditionListener = new EqualConditionListener<E>(); |
| 193 | 5 | EventListener eventListener = new EventListener(); |
| 194 | 5 | eventListener.set(listener); |
| 195 | 5 | conditionListener.set(state, eventListener); |
| 196 | |
|
| 197 | 5 | conditionListeners.put(conditionListener, this); |
| 198 | |
} |
| 199 | |
|
| 200 | 6 | if (isLockEnabledLocal) { |
| 201 | 6 | getStateLocker().writeLock().unlock(); |
| 202 | |
} |
| 203 | |
|
| 204 | 6 | return conditionListener; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public ConditionListener<E> notifyWhenStateIsNotEqual(E state, Object listener) { |
| 220 | 4 | boolean isLockEnabledLocal = isLockEnabled; |
| 221 | 4 | if (isLockEnabledLocal) { |
| 222 | 4 | getStateLocker().writeLock().lock(); |
| 223 | |
} |
| 224 | |
|
| 225 | 4 | ConditionListener<E> conditionListener = null; |
| 226 | |
|
| 227 | 4 | if (!stateRef.get().equals(state)) { |
| 228 | 1 | EventListener.notifyListener(listener); |
| 229 | |
} else { |
| 230 | 3 | conditionListener = new NotEqualConditionListener<E>(); |
| 231 | 3 | EventListener eventListener = new EventListener(); |
| 232 | 3 | eventListener.set(listener); |
| 233 | 3 | conditionListener.set(state, eventListener); |
| 234 | |
|
| 235 | 3 | conditionListeners.put(conditionListener, this); |
| 236 | |
} |
| 237 | |
|
| 238 | 4 | if (isLockEnabledLocal) { |
| 239 | 4 | getStateLocker().writeLock().unlock(); |
| 240 | |
} |
| 241 | |
|
| 242 | 4 | return conditionListener; |
| 243 | |
} |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public void notifyWhenConditionMatchState(ConditionListener<E> conditionListener) { |
| 253 | 0 | boolean isLockEnabledLocal = isLockEnabled; |
| 254 | 0 | if (isLockEnabledLocal) { |
| 255 | 0 | getStateLocker().writeLock().lock(); |
| 256 | |
} |
| 257 | |
|
| 258 | 0 | E currentState = getState(); |
| 259 | |
|
| 260 | 0 | if (conditionListener.check(currentState)) { |
| 261 | 0 | conditionListener.notifyListener(); |
| 262 | |
} else { |
| 263 | 0 | conditionListeners.put(conditionListener, this); |
| 264 | |
} |
| 265 | |
|
| 266 | 0 | if (isLockEnabledLocal) { |
| 267 | 0 | getStateLocker().writeLock().unlock(); |
| 268 | |
} |
| 269 | 0 | } |
| 270 | |
|
| 271 | |
public void removeConditionListener(ConditionListener<E> conditionListener) { |
| 272 | 6 | if (conditionListener == null) return; |
| 273 | |
|
| 274 | 6 | conditionListeners.remove(conditionListener); |
| 275 | 6 | } |
| 276 | |
|
| 277 | |
protected void checkConditionListeners(E state) { |
| 278 | 1422 | Iterator<ConditionListener<E>> it = conditionListeners.keySet().iterator(); |
| 279 | 1426 | while(it.hasNext()) { |
| 280 | 4 | ConditionListener<E> listener = it.next(); |
| 281 | |
try { |
| 282 | 4 | if (listener.check(state)) { |
| 283 | 3 | it.remove(); |
| 284 | 3 | listener.notifyListener(); |
| 285 | |
} |
| 286 | 0 | } catch(Exception e) { |
| 287 | 0 | Controller.logger().log(Level.WARNING, "Error calling ConditionListener", e); |
| 288 | 4 | } |
| 289 | 4 | } |
| 290 | 1422 | } |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | 8 | public static abstract class ConditionListener<E> { |
| 300 | |
public E state; |
| 301 | |
public EventListener listener; |
| 302 | |
|
| 303 | |
protected void set(E state, EventListener listener) { |
| 304 | 8 | this.state = state; |
| 305 | 8 | this.listener = listener; |
| 306 | 8 | } |
| 307 | |
|
| 308 | |
public void notifyListener() { |
| 309 | 3 | listener.notifyEvent(); |
| 310 | 3 | } |
| 311 | |
|
| 312 | |
public abstract boolean check(E state); |
| 313 | |
} |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | 5 | public static class EqualConditionListener<E> extends ConditionListener<E> { |
| 320 | |
public boolean check(E state) { |
| 321 | 3 | return state.equals(this.state); |
| 322 | |
} |
| 323 | |
} |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | 3 | public static class NotEqualConditionListener<E> extends ConditionListener<E> { |
| 330 | |
public boolean check(E state) { |
| 331 | 1 | return !state.equals(this.state); |
| 332 | |
} |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | 8 | public static class EventListener { |
| 341 | |
public Object notificationObject; |
| 342 | |
|
| 343 | |
public void set(Object notificationObject) { |
| 344 | 8 | this.notificationObject = notificationObject; |
| 345 | 8 | } |
| 346 | |
|
| 347 | |
public void notifyEvent() { |
| 348 | 3 | notifyListener(notificationObject); |
| 349 | 3 | } |
| 350 | |
|
| 351 | |
protected static void notifyListener(Object listener) { |
| 352 | 5 | if (listener instanceof CountDownLatch) { |
| 353 | 5 | ((CountDownLatch) listener).countDown(); |
| 354 | 0 | } else if (listener instanceof Callable) { |
| 355 | |
try { |
| 356 | 0 | ((Callable) listener).call(); |
| 357 | 0 | } catch(Exception e) { |
| 358 | 0 | throw new RuntimeException(e); |
| 359 | 0 | } |
| 360 | 0 | } else if (listener instanceof Runnable) { |
| 361 | 0 | ((Runnable) listener).run(); |
| 362 | |
} else { |
| 363 | 0 | synchronized(listener) { |
| 364 | 0 | listener.notify(); |
| 365 | 0 | } |
| 366 | |
} |
| 367 | 5 | } |
| 368 | |
} |
| 369 | |
} |