1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.processor.command;
7   
8   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import org.apache.commons.cli.CommandLine;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  
15  /**
16   * A base class for implementation that perform the operation supported by a
17   * command.
18   */
19  public abstract class AbstractCommandExecutor implements ICommandExecutor {
20    @NonNull
21    private final CallingContext callingContext;
22    @NonNull
23    private final CommandLine commandLine;
24  
25    /**
26     * Construct a new command executor.
27     *
28     * @param callingContext
29     *          the context of the command execution
30     * @param commandLine
31     *          the parsed command line details
32     */
33    protected AbstractCommandExecutor(
34        @NonNull CallingContext callingContext,
35        @NonNull CommandLine commandLine) {
36      this.callingContext = callingContext;
37      this.commandLine = commandLine;
38    }
39  
40    /**
41     * Get the context of the command execution, which provides access to the
42     * execution environment needed for command processing.
43     *
44     * @return the context
45     */
46    @NonNull
47    protected CallingContext getCallingContext() {
48      return callingContext;
49    }
50  
51    /**
52     * Get the parsed command line details containing the command options and
53     * arguments provided by the user during execution.
54     *
55     * @return the cli details
56     */
57    @NonNull
58    protected CommandLine getCommandLine() {
59      return commandLine;
60    }
61  
62    @Override
63    public abstract void execute() throws CommandExecutionException;
64  
65    /**
66     * Get the command associated with this execution.
67     *
68     * @return the command
69     */
70    @NonNull
71    protected ICommand getCommand() {
72      return ObjectUtils.requireNonNull(getCallingContext().getTargetCommand());
73    }
74  }