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 package org.apache.xbean.kernel;
018
019 import java.io.Serializable;
020
021 /**
022 * The state of services within the Kernel. The state model is directly adapted from the J2EE Management Specification
023 * (JSR 77) with the removal of the FAILED state.
024 *
025 * @author Dain Sundstrom
026 * @version $Id$
027 * @since 2.0
028 */
029 public final class ServiceState implements Serializable {
030 private static final long serialVersionUID = -2629672602273580572L;
031
032 /**
033 * This state indicates that the service is attempting to start but has not fully started yet. Normally, a service
034 * in this state is waiting for a required service to enter the RUNNING state.
035 */
036 public static final ServiceState STARTING = new ServiceState((byte) 0, "STARTING");
037
038 /**
039 * This state indicates that the service is in the normal operational state.
040 */
041 public static final ServiceState RUNNING = new ServiceState((byte) 1, "RUNNING");
042
043 /**
044 * This state indicates that the service is attempting to stop but has not fully stopped yet. Normally, a service
045 * in this state because another service is still usind this service.
046 */
047 public static final ServiceState STOPPING = new ServiceState((byte) 2, "STOPPING");
048
049 /**
050 * This state indicates that the service is stopped and not operational.
051 */
052 public static final ServiceState STOPPED = new ServiceState((byte) 3, "STOPPED");
053
054 /**
055 * A quick index for looking up service states.
056 */
057 private static final ServiceState[] serviceStateIndex = new ServiceState[]{STARTING, RUNNING, STOPPING, STOPPED};
058
059 static {
060 for (int i = 0; i < serviceStateIndex.length; i++) {
061 ServiceState serviceState = serviceStateIndex[i];
062 if (serviceState.getIndex() != i) {
063 throw new AssertionError(serviceState + " state index is " + serviceState.getIndex() +
064 ", but is located at index " + i + " in the serviceStateIndex");
065 }
066 }
067 }
068
069 /**
070 * Converts the state index into corresponding state name.
071 *
072 * @param state the state index
073 * @return the name of the state
074 * @throws IllegalArgumentException if the state index is not 0, 1, 2 or 3
075 */
076 public static ServiceState getServiceState(int state) throws IllegalArgumentException {
077 if (state < 0 || state >= serviceStateIndex.length) {
078 throw new IllegalArgumentException("Unknown state " + state);
079 }
080 return serviceStateIndex[state];
081 }
082
083 /**
084 * Converts the state name in the corresponding state index. This method performs a case insensitive comparison.
085 *
086 * @param state the state name
087 * @return the state index
088 * @throws IllegalArgumentException if the state index is not STARTING, RUNNING, STOPPING or FAILED
089 */
090 public static ServiceState parseServiceState(String state) {
091 if (state == null) throw new NullPointerException("state is null");
092 if (STARTING.toString().equalsIgnoreCase(state)) {
093 return STARTING;
094 } else if (RUNNING.toString().equalsIgnoreCase(state)) {
095 return RUNNING;
096 } else if (STOPPING.toString().equalsIgnoreCase(state)) {
097 return STOPPING;
098 } else if (STOPPED.toString().equalsIgnoreCase(state)) {
099 return STOPPED;
100 } else {
101 throw new IllegalArgumentException("Unknown state " + state);
102 }
103 }
104
105 private final byte index;
106 private final transient String name;
107
108 private ServiceState(byte index, String name) {
109 this.index = index;
110 this.name = name;
111 }
112
113 /**
114 * Gets the unique index of this state. This index can be be used to retrieve this state instance using the
115 * getServiceState(int) method.
116 *
117 * @return the unique index of this state
118 */
119 public int getIndex() {
120 return index;
121 }
122
123 /**
124 * The unique name of this state. This uppercase name can be used to retrieve this state instance using the
125 * parseServiceState(String).
126 *
127 * @return the unique name of this state
128 */
129 public String getName() {
130 return name;
131 }
132
133 /**
134 * Returns the name of this state.
135 *
136 * @return the unique name of this state
137 */
138 public String toString() {
139 return name;
140 }
141
142 private Object readResolve() {
143 return serviceStateIndex[index];
144 }
145 }