001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.kernel.management;
019
020 import java.io.Serializable;
021
022
023 /**
024 * This class contains a type safe enumeration of the states from the J2EE Management specification.
025 *
026 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
027 */
028 public final class State implements Serializable {
029 public static final int STARTING_INDEX = 0;
030 public static final int RUNNING_INDEX = 1;
031 public static final int STOPPING_INDEX = 2;
032 public static final int STOPPED_INDEX = 3;
033 public static final int FAILED_INDEX = 4;
034
035 public static final State STARTING = new State("starting", STARTING_INDEX, NotificationType.STATE_STARTING);
036 public static final State RUNNING = new State("running", RUNNING_INDEX, NotificationType.STATE_RUNNING);
037 public static final State STOPPING = new State("stopping", STOPPING_INDEX, NotificationType.STATE_STOPPING);
038 public static final State STOPPED = new State("stopped", STOPPED_INDEX, NotificationType.STATE_STOPPED);
039 public static final State FAILED = new State("failed", FAILED_INDEX, NotificationType.STATE_FAILED);
040
041 private static final State[] fromInt = {STARTING, RUNNING, STOPPING, STOPPED, FAILED};
042
043 /**
044 * Get a State from an int index
045 *
046 * @param index int index of the state
047 * @return The State instance or null if no such State.
048 */
049 public static State fromInt(int index) {
050 if (index < 0 || index >= fromInt.length) {
051 return null;
052 }
053 return fromInt[index];
054 }
055
056 /**
057 * Get a State from an Integer index
058 *
059 * @param index Integer index of the state
060 * @return The State instance or null if no such State.
061 */
062 public static State fromInteger(Integer index) {
063 return fromInt(index.intValue());
064 }
065
066 public static String toString(int state) {
067 if (state < 0 || state >= fromInt.length) {
068 throw new IllegalArgumentException("State must be between 0 and " + fromInt.length);
069 }
070 return fromInt[state].name;
071 }
072
073 /**
074 * The user readable name of this state from the J2EE Management specification
075 */
076 private final String name;
077
078 /**
079 * The state index from the J2EE Management specification
080 */
081 private final int index;
082
083 /**
084 * Type value to be broadcasted on entering this state.
085 */
086 private final String eventTypeValue;
087
088 private State(String name, int index, String anEventTypeValue) {
089 this.name = name;
090 this.index = index;
091 eventTypeValue = anEventTypeValue;
092 }
093
094 /**
095 * Gets the integer value of this state as specified in the J2EE Management specification
096 */
097 public int toInt() {
098 return index;
099 }
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 }