01: package org.objectweb.celtix.application;
02:
03: import java.util.ResourceBundle;
04: import java.util.logging.Logger;
05:
06: import org.objectweb.celtix.common.i18n.BundleUtils;
07: import org.objectweb.celtix.common.i18n.Message;
08: import org.objectweb.celtix.plugins.PluginException;
09:
10: public class PluginStateMachine {
11:
12: private static final Logger LOG = Logger
13: .getLogger(PluginStateMachine.class.getName());
14: private static final ResourceBundle BUNDLE = BundleUtils
15: .getBundle(PluginStateMachine.class);
16:
17: public enum PluginState {
18: UNLOADED, LOADING, LOADED
19: };
20:
21: private PluginState state;
22:
23: PluginStateMachine() {
24: this (PluginState.UNLOADED);
25: }
26:
27: PluginStateMachine(PluginState initialState) {
28: state = initialState;
29: }
30:
31: PluginState getCurrentState() {
32: return state;
33: }
34:
35: synchronized void setNextState(PluginState nextState)
36: throws PluginException {
37: if ((state == PluginState.UNLOADED && nextState == PluginState.LOADING)
38: || (state == PluginState.LOADING && nextState == PluginState.LOADED)
39: || (state == PluginState.LOADED && nextState == PluginState.UNLOADED)) {
40: LOG.fine("changing state from " + state + " to "
41: + nextState);
42: state = nextState;
43: } else {
44: Message msg = new Message("INVALID_STATE_TRANSITION_EXC",
45: BUNDLE, state, nextState);
46: throw new PluginException(msg);
47: }
48: notifyAll();
49: }
50:
51: synchronized void waitForState(PluginState awaitedState) {
52: while (state != awaitedState) {
53: LOG.fine("waiting for state so change from " + state
54: + " to " + awaitedState);
55: try {
56: wait();
57: } catch (InterruptedException ex) {
58: // deliberately ignore
59: }
60: }
61: }
62: }
|