01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.restart;
05:
06: import EDU.oswego.cs.dl.util.concurrent.FutureResult;
07:
08: import com.tc.exception.TCRuntimeException;
09:
10: public abstract class AbstractRestartTestApp implements RestartTestApp {
11:
12: protected static final State INIT = new State(TestAppState.INIT);
13: protected static final State START = new State(TestAppState.START);
14: protected static final State HOLDER = new State(TestAppState.HOLDER);
15: protected static final State WAITER = new State(TestAppState.WAITER);
16: protected static final State END = new State(TestAppState.END);
17: protected final FutureResult state = new FutureResult();
18: private Integer id;
19: protected final ThreadGroup threadGroup;
20:
21: protected AbstractRestartTestApp(ThreadGroup threadGroup) {
22: super ();
23: this .threadGroup = threadGroup;
24: }
25:
26: public String getStateName() {
27: return getState().getName();
28: }
29:
30: public synchronized void setID(int i) {
31: this .id = new Integer(i);
32: }
33:
34: public synchronized int getID() {
35: if (this .id == null)
36: throw new AssertionError("ID is null.");
37: return this .id.intValue();
38: }
39:
40: public synchronized boolean isInit() {
41: return getState().getName().equals(INIT.getName());
42: }
43:
44: public synchronized boolean isStart() {
45: return getState().getName().equals(START.getName());
46: }
47:
48: public synchronized boolean isHolder() {
49: return getState().getName().equals(HOLDER.getName());
50: }
51:
52: public synchronized boolean isWaiter() {
53: return getState().getName().equals(WAITER.getName());
54: }
55:
56: public synchronized boolean isEnd() {
57: return getState().getName().equals(END.getName());
58: }
59:
60: private State getState() {
61: try {
62: return (State) this .state.get();
63: } catch (Exception e) {
64: throw new TCRuntimeException(e);
65: }
66: }
67:
68: protected synchronized void changeState(State newState) {
69: this .state.set(newState);
70: notifyAll();
71: }
72:
73: static protected final class State {
74: private final String name;
75:
76: private State(String name) {
77: this .name = name;
78: }
79:
80: public String getName() {
81: return this .name;
82: }
83:
84: public String toString() {
85: return this.name;
86: }
87: }
88:
89: }
|