001/*
002 * Copyright 2012-2015 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;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.EnumSet;
022import java.util.Set;
023
024/**
025 * Runtime exception wrapper that defines additional {@link Option}s that are understood
026 * by the {@link CommandRunner}.
027 *
028 * @author Phillip Webb
029 */
030public class CommandException extends RuntimeException {
031
032        private static final long serialVersionUID = 0L;
033
034        private final EnumSet<Option> options;
035
036        /**
037         * Create a new {@link CommandException} with the specified options.
038         * @param options the exception options
039         */
040        public CommandException(Option... options) {
041                this.options = asEnumSet(options);
042        }
043
044        /**
045         * Create a new {@link CommandException} with the specified options.
046         * @param message the exception message to display to the user
047         * @param options the exception options
048         */
049        public CommandException(String message, Option... options) {
050                super(message);
051                this.options = asEnumSet(options);
052        }
053
054        /**
055         * Create a new {@link CommandException} with the specified options.
056         * @param message the exception message to display to the user
057         * @param cause the underlying cause
058         * @param options the exception options
059         */
060        public CommandException(String message, Throwable cause, Option... options) {
061                super(message, cause);
062                this.options = asEnumSet(options);
063        }
064
065        /**
066         * Create a new {@link CommandException} with the specified options.
067         * @param cause the underlying cause
068         * @param options the exception options
069         */
070        public CommandException(Throwable cause, Option... options) {
071                super(cause);
072                this.options = asEnumSet(options);
073        }
074
075        private EnumSet<Option> asEnumSet(Option[] options) {
076                if (options == null || options.length == 0) {
077                        return EnumSet.noneOf(Option.class);
078                }
079                return EnumSet.copyOf(Arrays.asList(options));
080        }
081
082        /**
083         * Returns a set of options that are understood by the {@link CommandRunner}.
084         * @return the options understood by the runner
085         */
086        public Set<Option> getOptions() {
087                return Collections.unmodifiableSet(this.options);
088        }
089
090        /**
091         * Specific options understood by the {@link CommandRunner}.
092         */
093        public enum Option {
094
095                /**
096                 * Hide the exception message.
097                 */
098                HIDE_MESSAGE,
099
100                /**
101                 * Print basic CLI usage information.
102                 */
103                SHOW_USAGE,
104
105                /**
106                 * Print the stack-trace of the exception.
107                 */
108                STACK_TRACE,
109
110                /**
111                 * Re-throw the exception rather than dealing with it.
112                 */
113                RETHROW
114
115        }
116
117}