001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.management.beans;
006:
007: import com.tc.l2.context.StateChangedEvent;
008: import com.tc.l2.state.StateChangeListener;
009: import com.tc.l2.state.StateManager;
010: import com.tc.util.State;
011:
012: public class L2State implements StateChangeListener {
013: private static final boolean DEBUG = false;
014: private State serverState = StateManager.START_STATE;
015: private StateChangeListener changeListener;
016:
017: public synchronized void setState(State state) {
018: if (!validateState(state)) {
019: throw new AssertionError("Unrecognized server state: ["
020: + state.getName() + "]");
021: }
022:
023: // TODO: need to deal with checking for state validity at some point
024: // if (serverState.equals(state)) { throw new AssertionError("Re-setting L2 state to the same state:
025: // existing=["+serverState.getName()+"] passedIn=["+state.getName()+"]"); }
026:
027: if (changeListener != null) {
028: debugPrintln("******* L2State is notifying listener of state change: oldState=["
029: + serverState.getName()
030: + "] newState=["
031: + state.getName() + "]");
032: changeListener.l2StateChanged(new StateChangedEvent(
033: serverState, state));
034: }
035:
036: serverState = state;
037: }
038:
039: private void debugPrintln(String s) {
040: if (DEBUG) {
041: System.err.println(s);
042: }
043: }
044:
045: public synchronized State getState() {
046: return serverState;
047: }
048:
049: private boolean validateState(State state) {
050: for (int i = 0; i < StateManager.validStates.length; i++) {
051: if (StateManager.validStates[i].equals(state)) {
052: return true;
053: }
054: }
055: return false;
056: }
057:
058: public void l2StateChanged(StateChangedEvent sce) {
059: setState(sce.getCurrentState());
060: }
061:
062: public boolean isActiveCoordinator() {
063: if (getState().equals(StateManager.ACTIVE_COORDINATOR)) {
064: return true;
065: }
066: return false;
067: }
068:
069: public boolean isPassiveUninitialized() {
070: if (getState().equals(StateManager.PASSIVE_UNINTIALIZED)) {
071: return true;
072: }
073: return false;
074: }
075:
076: public boolean isPassiveStandby() {
077: if (getState().equals(StateManager.PASSIVE_STANDBY)) {
078: return true;
079: }
080: return false;
081: }
082:
083: public boolean isStartState() {
084: if (getState().equals(StateManager.START_STATE)) {
085: return true;
086: }
087: return false;
088: }
089:
090: public boolean isStopState() {
091: if (getState().equals(StateManager.STOP_STATE)) {
092: return true;
093: }
094: return false;
095: }
096:
097: public void registerStateChangeListener(StateChangeListener listener) {
098: if (changeListener != null) {
099: throw new AssertionError(
100: "State change listerer is already set.");
101: }
102: changeListener = listener;
103: }
104:
105: }
|