001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.cli.processor;
028
029import org.apache.logging.log4j.LogBuilder;
030import org.apache.logging.log4j.LogManager;
031import org.apache.logging.log4j.Logger;
032
033import edu.umd.cs.findbugs.annotations.NonNull;
034import edu.umd.cs.findbugs.annotations.Nullable;
035import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
036
037public abstract class AbstractExitStatus implements ExitStatus {
038  private static final Logger LOGGER = LogManager.getLogger(AbstractExitStatus.class);
039
040  @NonNull
041  private final ExitCode exitCode;
042
043  private Throwable throwable;
044
045  /**
046   * Construct a new exit status based on the provided {@code exitCode}.
047   *
048   * @param exitCode
049   *          the exit code
050   */
051  public AbstractExitStatus(@NonNull ExitCode exitCode) {
052    this.exitCode = exitCode;
053  }
054
055  @Override
056  public ExitCode getExitCode() {
057    return exitCode;
058  }
059
060  /**
061   * Get the associated throwable.
062   *
063   * @return the throwable or {@code null}
064   */
065  @Override
066  public Throwable getThrowable() {
067    return throwable;
068  }
069
070  @Override
071  @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "intended as a exposed property")
072  public ExitStatus withThrowable(@NonNull Throwable throwable) {
073    this.throwable = throwable;
074    return this;
075  }
076
077  /**
078   * Get the associated message.
079   *
080   * @return the message or {@code null}
081   */
082  @Nullable
083  protected abstract String getMessage();
084
085  @Override
086  public void generateMessage(boolean showStackTrace) {
087    LogBuilder logBuilder = null;
088    if (getExitCode().getStatusCode() <= 0) {
089      if (LOGGER.isInfoEnabled()) {
090        logBuilder = LOGGER.atInfo();
091      }
092    } else if (LOGGER.isErrorEnabled()) {
093      logBuilder = LOGGER.atError();
094    }
095
096    if (logBuilder != null) {
097      Throwable throwable = getThrowable();
098      if (showStackTrace && throwable != null) {
099        logBuilder.withThrowable(throwable);
100      }
101
102      String message = getMessage();
103      if (message == null && throwable != null) {
104        message = throwable.getLocalizedMessage();
105      }
106
107      if (message != null && !message.isEmpty()) {
108        logBuilder.log(message);
109      } else if (throwable != null && showStackTrace) {
110        // log the throwable
111        logBuilder.log();
112      }
113    }
114  }
115
116}