001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: /**
030: * A full implementation of GenericStateModel.
031: **/
032:
033: public abstract class GenericStateModelAdapter implements
034: GenericStateModel {
035:
036: /** current reflection of Plugin run state **/
037: private int runState = UNINITIALIZED;
038:
039: /** Plugin State model accessor.
040: **/
041: public final int getModelState() {
042: return runState;
043: }
044:
045: /** simple initialize method.
046: * Transits the state to UNLOADED.
047: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
048: **/
049: public synchronized void initialize() throws StateModelException {
050: transitState("initialize()", UNINITIALIZED, UNLOADED);
051: }
052:
053: /** Notice which Cluster we are.
054: * also transit to LOADED.
055: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
056: **/
057: public synchronized void load() throws StateModelException {
058: transitState("load()", UNLOADED, LOADED);
059: }
060:
061: /** This version of start just transits to ACTIVE.
062: * Daemon subclasses may want to start threads here.
063: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
064: **/
065: public synchronized void start() throws StateModelException {
066: transitState("start()", LOADED, ACTIVE);
067: }
068:
069: /**
070: * Just change the state to IDLE.
071: * @exception org.cougaar.util.StateModelException Cannot transition to new state.
072: **/
073: public synchronized void suspend() throws StateModelException {
074: transitState("suspend()", ACTIVE, IDLE);
075: }
076:
077: /**
078: * Transit from IDLE to ACTIVE .
079: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
080: **/
081: public synchronized void resume() throws StateModelException {
082: transitState("resume()", IDLE, ACTIVE);
083: }
084:
085: /**
086: * Transit from IDLE to LOADED.
087: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
088: **/
089: public synchronized void stop() throws StateModelException {
090: transitState("stop()", IDLE, LOADED);
091: }
092:
093: /** Transit from ACTIVE to LOADED.
094: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
095: **/
096: public synchronized void halt() throws StateModelException {
097: transitState("halt()", ACTIVE, LOADED);
098: }
099:
100: /** Transit from LOADED to UNLOADED.
101: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
102: **/
103: public synchronized void unload() throws StateModelException {
104: transitState("unload()", LOADED, UNLOADED);
105: }
106:
107: /** Accomplish the state transition.
108: * Be careful and complain if we are in an inappropriate starting state.
109: * @exception org.cougaar.util.StateModelException If Cannot transition to new state.
110: **/
111: protected synchronized void transitState(String op,
112: int expectedState, int endState) throws StateModelException {
113: if (runState != expectedState) {
114: throw new StateModelException("" + this + "." + op
115: + " called in inappropriate state (" + runState
116: + ")");
117: } else {
118: runState = endState;
119: }
120: }
121: }
|