1
2
3
4
5
6 package gov.nist.secauto.metaschema.cli.processor;
7
8 import org.apache.logging.log4j.LogBuilder;
9 import org.apache.logging.log4j.LogManager;
10 import org.apache.logging.log4j.Logger;
11
12 import edu.umd.cs.findbugs.annotations.NonNull;
13 import edu.umd.cs.findbugs.annotations.Nullable;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15
16
17
18
19
20
21
22
23
24 public abstract class AbstractExitStatus implements ExitStatus {
25 private static final Logger LOGGER = LogManager.getLogger(AbstractExitStatus.class);
26
27 @NonNull
28 private final ExitCode exitCode;
29
30 private Throwable throwable;
31
32
33
34
35
36
37
38 public AbstractExitStatus(@NonNull ExitCode exitCode) {
39 this.exitCode = exitCode;
40 }
41
42 @Override
43 public ExitCode getExitCode() {
44 return exitCode;
45 }
46
47
48
49
50
51
52 @Override
53 public Throwable getThrowable() {
54 return throwable;
55 }
56
57 @Override
58 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "intended as a exposed property")
59 public ExitStatus withThrowable(@NonNull Throwable throwable) {
60 this.throwable = throwable;
61 return this;
62 }
63
64
65
66
67
68
69 @Nullable
70 protected abstract String getMessage();
71
72
73
74
75
76
77
78
79
80 @Nullable
81 private LogBuilder getLogBuilder() {
82 LogBuilder logBuilder = null;
83 if (getExitCode().getStatusCode() <= 0) {
84 if (LOGGER.isInfoEnabled()) {
85 logBuilder = LOGGER.atInfo();
86 }
87 } else if (LOGGER.isErrorEnabled()) {
88 logBuilder = LOGGER.atError();
89 }
90 return logBuilder;
91 }
92
93
94
95
96
97
98
99
100
101
102 @Override
103 public void generateMessage(boolean showStackTrace) {
104 LogBuilder logBuilder = getLogBuilder();
105 if (logBuilder == null) {
106 return;
107 }
108
109 boolean useStackTrace = showStackTrace && throwable != null;
110 if (useStackTrace) {
111 logBuilder.withThrowable(throwable);
112 }
113
114 String message = getMessage();
115 if (throwable != null && message == null) {
116 message = throwable.getLocalizedMessage();
117 }
118
119 if (message != null && !message.isEmpty()) {
120 logBuilder.log(message);
121 } else if (useStackTrace) {
122
123 logBuilder.log();
124 }
125 }
126
127 }