001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *       http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing, software
013 *  distributed under the License is distributed on an "AS IS" BASIS,
014 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 *  See the License for the specific language governing permissions and
016 *  limitations under the License.
017 */
018
019package org.apache.hadoop.service;
020
021import org.apache.hadoop.classification.InterfaceAudience.Public;
022import org.apache.hadoop.classification.InterfaceStability.Evolving;
023
024/**
025 * Exception that is raised on state change operations.
026 */
027@Public
028@Evolving
029public class ServiceStateException extends RuntimeException {
030
031  private static final long serialVersionUID = 1110000352259232646L;
032
033  public ServiceStateException(String message) {
034    super(message);
035  }
036
037  public ServiceStateException(String message, Throwable cause) {
038    super(message, cause);
039  }
040
041  public ServiceStateException(Throwable cause) {
042    super(cause);
043  }
044
045  /**
046   * Convert any exception into a {@link RuntimeException}.
047   * If the caught exception is already of that type, it is typecast to a
048   * {@link RuntimeException} and returned.
049   *
050   * All other exception types are wrapped in a new instance of
051   * ServiceStateException
052   * @param fault exception or throwable
053   * @return a ServiceStateException to rethrow
054   */
055  public static RuntimeException convert(Throwable fault) {
056    if (fault instanceof RuntimeException) {
057      return (RuntimeException) fault;
058    } else {
059      return new ServiceStateException(fault);
060    }
061  }
062
063  /**
064   * Convert any exception into a {@link RuntimeException}.
065   * If the caught exception is already of that type, it is typecast to a
066   * {@link RuntimeException} and returned.
067   *
068   * All other exception types are wrapped in a new instance of
069   * ServiceStateException
070   * @param text text to use if a new exception is created
071   * @param fault exception or throwable
072   * @return a ServiceStateException to rethrow
073   */
074  public static RuntimeException convert(String text, Throwable fault) {
075    if (fault instanceof RuntimeException) {
076      return (RuntimeException) fault;
077    } else {
078      return new ServiceStateException(text, fault);
079    }
080  }
081}