1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.kernel.management;
19
20 import java.io.Serializable;
21
22
23 /**
24 * This class contains a type safe enumeration of the states from the J2EE Management specification.
25 *
26 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
27 */
28 public final class State implements Serializable {
29 public static final int STARTING_INDEX = 0;
30 public static final int RUNNING_INDEX = 1;
31 public static final int STOPPING_INDEX = 2;
32 public static final int STOPPED_INDEX = 3;
33 public static final int FAILED_INDEX = 4;
34
35 public static final State STARTING = new State("starting", STARTING_INDEX, NotificationType.STATE_STARTING);
36 public static final State RUNNING = new State("running", RUNNING_INDEX, NotificationType.STATE_RUNNING);
37 public static final State STOPPING = new State("stopping", STOPPING_INDEX, NotificationType.STATE_STOPPING);
38 public static final State STOPPED = new State("stopped", STOPPED_INDEX, NotificationType.STATE_STOPPED);
39 public static final State FAILED = new State("failed", FAILED_INDEX, NotificationType.STATE_FAILED);
40
41 private static final State[] fromInt = {STARTING, RUNNING, STOPPING, STOPPED, FAILED};
42
43 /**
44 * Get a State from an int index
45 *
46 * @param index int index of the state
47 * @return The State instance or null if no such State.
48 */
49 public static State fromInt(int index) {
50 if (index < 0 || index >= fromInt.length) {
51 return null;
52 }
53 return fromInt[index];
54 }
55
56 /**
57 * Get a State from an Integer index
58 *
59 * @param index Integer index of the state
60 * @return The State instance or null if no such State.
61 */
62 public static State fromInteger(Integer index) {
63 return fromInt(index.intValue());
64 }
65
66 public static String toString(int state) {
67 if (state < 0 || state >= fromInt.length) {
68 throw new IllegalArgumentException("State must be between 0 and " + fromInt.length);
69 }
70 return fromInt[state].name;
71 }
72
73 /**
74 * The user readable name of this state from the J2EE Management specification
75 */
76 private final String name;
77
78 /**
79 * The state index from the J2EE Management specification
80 */
81 private final int index;
82
83 /**
84 * Type value to be broadcasted on entering this state.
85 */
86 private final String eventTypeValue;
87
88 private State(String name, int index, String anEventTypeValue) {
89 this.name = name;
90 this.index = index;
91 eventTypeValue = anEventTypeValue;
92 }
93
94 /**
95 * Gets the integer value of this state as specified in the J2EE Management specification
96 */
97 public int toInt() {
98 return index;
99 }
100
101 /**
102 * Gets the event type that should be send after changeing to this state.
103 *
104 * @return the event type that should be sent after a transistion to this state
105 */
106 public String getEventTypeValue() {
107 return eventTypeValue;
108 }
109
110 public String getName() {
111 return name;
112 }
113
114 public boolean isRunning() {
115 return this == State.RUNNING;
116 }
117
118 public boolean isStopped() {
119 return this == State.STOPPED;
120 }
121
122 public boolean isFailed() {
123 return this == State.FAILED;
124 }
125
126 public String toString() {
127 return name;
128 }
129
130 private Object readResolve() {
131 return fromInt[index];
132 }
133 }