Skip to content

Commit a5fc87c

Browse files
author
David Waltermire
committed
Replaced Paths.get() with Paths.get(System.getProperty(user.dir)) to be more explict. Improved value creation methods in date and date/time item classes. Added many Javadocs.
1 parent cd5c5a3 commit a5fc87c

29 files changed

+358
-87
lines changed

cli-processor/src/main/java-templates/gov/nist/secauto/metaschema/cli/processor/ProcessorVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class ProcessorVersion implements IVersionInfo {
1414

15-
private static final String NAME = "metaschema-java";
15+
private static final String NAME = "${project.name}";
1616
private static final String VERSION = "${project.version}";
1717
private static final String BUILD_TIMESTAMP = "${timestamp}";
1818
private static final String COMMIT = "@git.commit.id.abbrev@";

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/AbstractExitStatus.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
/**
1717
* Records information about the exit status of a CLI command.
18+
* <p>
19+
* This abstract class provides base functionality for handling CLI command exit
20+
* statuses, including error logging and throwable management. Implementing
21+
* classes must provide the {@link #getMessage()} implementation to define the
22+
* status message content.
1823
*/
1924
public abstract class AbstractExitStatus implements ExitStatus {
2025
private static final Logger LOGGER = LogManager.getLogger(AbstractExitStatus.class);
@@ -64,6 +69,14 @@ public ExitStatus withThrowable(@NonNull Throwable throwable) {
6469
@Nullable
6570
protected abstract String getMessage();
6671

72+
/**
73+
* Determines the appropriate LogBuilder based on the exit code status. For
74+
* non-positive exit codes (success/info), returns an INFO level builder. For
75+
* positive exit codes (errors), returns an ERROR level builder.
76+
*
77+
* @return the appropriate LogBuilder based on exit status, or {@code null} if
78+
* logging is disabled at the determined level
79+
*/
6780
@Nullable
6881
private LogBuilder getLogBuilder() {
6982
LogBuilder logBuilder = null;
@@ -77,6 +90,15 @@ private LogBuilder getLogBuilder() {
7790
return logBuilder;
7891
}
7992

93+
/**
94+
* Generates and logs a message based on the current exit status. The message is
95+
* logged at either INFO level (for success/info status) or ERROR level (for
96+
* error status).
97+
*
98+
* @param showStackTrace
99+
* if {@code true} and a throwable is present, includes the stack trace
100+
* in the log
101+
*/
80102
@Override
81103
public void generateMessage(boolean showStackTrace) {
82104
LogBuilder logBuilder = getLogBuilder();
@@ -99,7 +121,7 @@ public void generateMessage(boolean showStackTrace) {
99121
} else if (useStackTrace) {
100122
// log the throwable
101123
logBuilder.log();
102-
}
124+
} // else avoid an empty log line
103125
}
104126

105127
}

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151

5252
/**
5353
* Processes command line arguments and dispatches called commands.
54+
* <p>
55+
* This implementation make significant use of the command pattern to support a
56+
* delegation chain of commands based on implementations of {@link ICommand}.
5457
*/
5558
@SuppressWarnings("PMD.CouplingBetweenObjects")
5659
public class CLIProcessor {
@@ -114,7 +117,7 @@ public class CLIProcessor {
114117
@NonNull
115118
private final List<ICommand> commands = new LinkedList<>();
116119
@NonNull
117-
private final String args;
120+
private final String exec;
118121
@NonNull
119122
private final Map<String, IVersionInfo> versionInfos;
120123

@@ -152,13 +155,13 @@ public CLIProcessor(@NonNull String args) {
152155
* <p>
153156
* This uses the provided version information.
154157
*
155-
* @param args
156-
* the command line arguments
158+
* @param exec
159+
* the command name
157160
* @param versionInfos
158161
* the version info to display when the version option is provided
159162
*/
160-
public CLIProcessor(@NonNull String args, @NonNull Map<String, IVersionInfo> versionInfos) {
161-
this.args = args;
163+
public CLIProcessor(@NonNull String exec, @NonNull Map<String, IVersionInfo> versionInfos) {
164+
this.exec = exec;
162165
this.versionInfos = versionInfos;
163166
AnsiConsole.systemInstall();
164167
}
@@ -169,8 +172,8 @@ public CLIProcessor(@NonNull String args, @NonNull Map<String, IVersionInfo> ver
169172
* @return the command name
170173
*/
171174
@NonNull
172-
public String getArguments() {
173-
return args;
175+
public String getExec() {
176+
return exec;
174177
}
175178

176179
/**
@@ -631,7 +634,7 @@ private String buildHelpFooter() {
631634
builder
632635
.append(System.lineSeparator())
633636
.append('\'')
634-
.append(getArguments())
637+
.append(getExec())
635638
.append(" <command> --help' will show help on that specific command.")
636639
.append(System.lineSeparator());
637640
retval = builder.toString();
@@ -648,7 +651,7 @@ private String buildHelpFooter() {
648651
private String buildHelpCliSyntax() {
649652

650653
StringBuilder builder = new StringBuilder(64);
651-
builder.append(getArguments());
654+
builder.append(getExec());
652655

653656
List<ICommand> calledCommands = getCalledCommands();
654657
if (!calledCommands.isEmpty()) {

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/MessageExitStatus.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
/**
1717
* An {@link ExitStatus} implementation with an associated message.
18+
* <p>
19+
* The message arguments are stored in an unmodifiable list to ensure
20+
* thread-safety and immutability.
1821
*/
1922
public class MessageExitStatus
2023
extends AbstractExitStatus {

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/NonMessageExitStatus.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@
88
import edu.umd.cs.findbugs.annotations.NonNull;
99

1010
/**
11-
* An {@link ExitStatus} implementation without an associated message.
11+
* An {@link ExitStatus} implementation that represents a status without an
12+
* associated message.
13+
* <p>
14+
* This implementation is useful when only the exit code needs to be
15+
* communicated, without additional context or explanation.
1216
*/
1317
public class NonMessageExitStatus
1418
extends AbstractExitStatus {
1519

1620
/**
17-
* Construct a new message status.
21+
* Construct a new exit status without an associated message.
22+
*
23+
* @param code
24+
* the non-null exit code representing the status
1825
*/
1926
NonMessageExitStatus(@NonNull ExitCode code) {
2027
super(code);
2128
}
2229

30+
/**
31+
* {@inheritDoc}
32+
*
33+
* @return {@code null} as this implementation does not support messages
34+
*/
2335
@Override
2436
protected String getMessage() {
25-
// always null
2637
return null;
2738
}
2839
}

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractCommandExecutor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected AbstractCommandExecutor(
3838
}
3939

4040
/**
41-
* Get the context of the command execution.
41+
* Get the context of the command execution, which provides access to the
42+
* execution environment needed for command processing.
4243
*
4344
* @return the context
4445
*/
@@ -48,7 +49,8 @@ protected CallingContext getCallingContext() {
4849
}
4950

5051
/**
51-
* Get the parsed command line details.
52+
* Get the parsed command line details containing the command options and
53+
* arguments provided by the user during execution.
5254
*
5355
* @return the cli details
5456
*/

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractParentCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import edu.umd.cs.findbugs.annotations.NonNull;
2222

2323
/**
24-
* A base class for a command that has child commands.
24+
* A base class for a command that supports hierarchical command structure with
25+
* child commands. This class provides the foundation for implementing complex
26+
* CLI commands that can have multiple levels of sub-commands.
27+
* <p>
28+
* This class is thread-safe and supports concurrent access to command handlers.
2529
*/
2630
public abstract class AbstractParentCommand implements ICommand {
2731
@NonNull

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractTerminalCommand.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
import nl.talsmasoftware.lazy4j.Lazy;
1818

1919
/**
20-
* A base class for commands that have no children.
20+
* A base class for terminal commands in the command processing hierarchy.
21+
* Terminal commands represent leaf nodes that perform actual operations and
22+
* cannot have child commands.
2123
*/
2224
public abstract class AbstractTerminalCommand implements ICommand {
23-
private static Lazy<Path> currentWorkingDirectory = Lazy.lazy(() -> Paths.get("").toAbsolutePath());
25+
private static Lazy<Path> currentWorkingDirectory = Lazy.lazy(() -> Paths.get(System.getProperty("user.dir")));
2426

2527
/**
2628
* A utility method that can be used to get the current working directory.
29+
* <p>
30+
* This method is thread-safe due to lazy initialization.
2731
*
2832
* @return the current working directory
2933
*/
@@ -35,6 +39,8 @@ protected static Path getCurrentWorkingDirectory() {
3539
/**
3640
* A utility method that can be used to resolve a path against the current
3741
* working directory.
42+
* <p>
43+
* If the path is already absolute, then the provided path is returned.
3844
*
3945
* @param path
4046
* the path to resolve
@@ -43,12 +49,21 @@ protected static Path getCurrentWorkingDirectory() {
4349
*/
4450
@NonNull
4551
protected static Path resolveAgainstCWD(@NonNull Path path) {
46-
return ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
52+
53+
return path.isAbsolute()
54+
? path
55+
: ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
4756
}
4857

4958
/**
5059
* A utility method that can be used to resolve a URI against the URI for the
5160
* current working directory.
61+
* <p>
62+
* If the URI is already absolute, then the provided URI is returned.
63+
* <p>
64+
* The path is normalized after resolution to remove any redundant name elements
65+
* (like "." or "..").
66+
*
5267
*
5368
* @param uri
5469
* the uri to resolve
@@ -57,7 +72,9 @@ protected static Path resolveAgainstCWD(@NonNull Path path) {
5772
*/
5873
@NonNull
5974
protected static URI resolveAgainstCWD(@NonNull URI uri) {
60-
return ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
75+
return uri.isAbsolute()
76+
? uri
77+
: ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
6178
}
6279

6380
/**

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/CommandService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
/**
1919
* A service that loads commands using SPI.
20+
* <p>
21+
* This class implements the singleton pattern to ensure a single instance of
22+
* the command service is used throughout the application.
2023
*
2124
* @see ServiceLoader for more information
2225
*/
@@ -36,8 +39,12 @@ public static CommandService getInstance() {
3639

3740
/**
3841
* Construct a new service.
42+
* <p>
43+
* Initializes the ServiceLoader for ICommand implementations.
44+
* <p>
45+
* This constructor is private to enforce the singleton pattern.
3946
*/
40-
public CommandService() {
47+
private CommandService() {
4148
ServiceLoader<ICommand> loader = ServiceLoader.load(ICommand.class);
4249
assert loader != null;
4350
this.loader = loader;

cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/ExtraArgument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public interface ExtraArgument {
2424
*/
2525
@NonNull
2626
static ExtraArgument newInstance(@NonNull String name, boolean required) {
27+
if (name.isBlank()) {
28+
throw new IllegalArgumentException("name cannot be empty or blank");
29+
}
2730
return new DefaultExtraArgument(name, required);
2831
}
2932

0 commit comments

Comments
 (0)