KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > kernel > state > StateMachine


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.kernel.state;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.portal.server.kernel.TransitionNotPossibleException;
13
14 /**
15  * Keep state and take care of transitions.
16  *
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.2 $
19  */

20 public class StateMachine
21 {
22
23    private final Logger log;
24    private State state;
25
26    public StateMachine(Logger log)
27    {
28       if (log == null)
29       {
30          throw new IllegalArgumentException JavaDoc("log must not be null");
31       }
32       this.log = log;
33       this.state = State.UNREGISTERED;
34    }
35
36    public State getState()
37    {
38       return state;
39    }
40
41    public void register(boolean test) throws TransitionNotPossibleException
42    {
43       if (state == State.UNREGISTERED)
44       {
45          if (!test)
46          {
47             updateState(State.REGISTERED);
48             log.debug("registered");
49          }
50       }
51       else
52       {
53          throw new TransitionNotPossibleException("Cannot call register() on " + state);
54       }
55    }
56
57    public void create(boolean test) throws TransitionNotPossibleException
58    {
59       if (state == State.REGISTERED)
60       {
61          if (!test)
62          {
63             updateState(State.STOPPED);
64             log.debug("created");
65          }
66       }
67       else
68       {
69          throw new TransitionNotPossibleException("Cannot call create() on " + state);
70       }
71    }
72
73    public void start(boolean test) throws TransitionNotPossibleException
74    {
75       if (state == State.STOPPED)
76       {
77          if (!test)
78          {
79             state = State.STARTED;
80             updateState(State.STARTED);
81             log.debug("started");
82          }
83       }
84       else
85       {
86          throw new TransitionNotPossibleException("Cannot call start() on " + state);
87       }
88    }
89
90    public void stop(boolean test) throws TransitionNotPossibleException
91    {
92       if (state == State.STARTED)
93       {
94          if (!test)
95          {
96             updateState(State.STOPPED);
97             log.debug("stopped");
98          }
99       }
100       else
101       {
102          throw new TransitionNotPossibleException("Cannot call stop() on " + state);
103       }
104    }
105
106    public void destroy(boolean test) throws TransitionNotPossibleException
107    {
108       if (state == State.STOPPED || state == State.FAILED)
109       {
110          if (!test)
111          {
112             updateState(State.REGISTERED);
113             log.debug("destroyed");
114          }
115       }
116       else
117       {
118          throw new TransitionNotPossibleException("Cannot call destroy() on " + state);
119       }
120    }
121
122    public void unregister(boolean test) throws TransitionNotPossibleException
123    {
124       if (state == State.REGISTERED || state == State.FAILED)
125       {
126          if (!test)
127          {
128             updateState(State.UNREGISTERED);
129             log.debug("unregistered");
130          }
131       }
132       else
133       {
134          throw new TransitionNotPossibleException("Cannot call removeIDependOn() on " + state);
135       }
136    }
137
138    public void fail(boolean test) throws TransitionNotPossibleException
139    {
140       if (!test)
141       {
142          updateState(State.FAILED);
143          log.debug("failed");
144
145       }
146    }
147
148    private void updateState(State newState)
149    {
150       State oldState = state;
151       state = newState;
152       if (oldState != newState)
153       {
154          stateChanged(oldState, newState);
155       }
156    }
157
158    protected void stateChanged(State oldState, State newState)
159    {
160    }
161 }
162
Popular Tags