001/* 002 * Copyright 2012-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.command.status; 018 019/** 020 * Encapsulation of the outcome of a command. 021 * 022 * @author Dave Syer 023 * @see ExitStatus#OK 024 * @see ExitStatus#ERROR 025 * 026 */ 027public final class ExitStatus { 028 029 /** 030 * Generic "OK" exit status with zero exit code and {@literal hangup=false}. 031 */ 032 public static ExitStatus OK = new ExitStatus(0, "OK"); 033 034 /** 035 * Generic "not OK" exit status with non-zero exit code and {@literal hangup=true}. 036 */ 037 public static ExitStatus ERROR = new ExitStatus(-1, "ERROR", true); 038 039 private final int code; 040 041 private final String name; 042 043 private final boolean hangup; 044 045 /** 046 * Create a new {@link ExitStatus} instance. 047 * @param code the exit code 048 * @param name the name 049 */ 050 public ExitStatus(int code, String name) { 051 this(code, name, false); 052 } 053 054 /** 055 * Create a new {@link ExitStatus} instance. 056 * @param code the exit code 057 * @param name the name 058 * @param hangup true if it is OK for the caller to hangup 059 */ 060 public ExitStatus(int code, String name, boolean hangup) { 061 this.code = code; 062 this.name = name; 063 this.hangup = hangup; 064 } 065 066 /** 067 * An exit code appropriate for use in {@code System.exit()}. 068 * @return an exit code 069 */ 070 public int getCode() { 071 return this.code; 072 } 073 074 /** 075 * A name describing the outcome. 076 * @return a name 077 */ 078 public String getName() { 079 return this.name; 080 } 081 082 /** 083 * Flag to signal that the caller can (or should) hangup. A server process with 084 * non-daemon threads should set this to false. 085 * @return the flag 086 */ 087 public boolean isHangup() { 088 return this.hangup; 089 } 090 091 /** 092 * Convert the existing code to a hangup. 093 * @return a new ExitStatus with hangup=true 094 */ 095 public ExitStatus hangup() { 096 return new ExitStatus(this.code, this.name, true); 097 } 098 099 @Override 100 public String toString() { 101 return getName() + ":" + getCode(); 102 } 103 104}