...

/

Sample CLI Client

Sample CLI Client

Create a simple CLI client to quickly test end-to-end functionality.

Before we define the class containing the CLI client implementation, we will define the configuration properties for controlling the behavior of the log4j logging framework in our application. We will  create a file log4j2.properties in the src/main/resources package. You can find the a brief explanation of the file in the Configuring the Logger lesson.

Press + to interact
appenders=console, stderr
appender.console.type=Console
appender.console.name=STDOUT
appender.console.target=SYSTEM_OUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.console.filter.threshold.type=ThresholdFilter
appender.console.filter.threshold.level=WARN
appender.console.filter.threshold.onMismatch=accept
appender.console.filter.threshold.onMatch=deny
appender.stderr.type=Console
appender.stderr.target=SYSTEM_ERR
appender.stderr.name=STDERR
appender.stderr.layout.type=PatternLayout
appender.stderr.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.stderr.filter.threshold.type=ThresholdFilter
appender.stderr.filter.threshold.level=WARN
appender.stderr.filter.threshold.onMatch=accept
rootLogger.level=INFO
rootLogger.appenderRefs=stdout, stderr
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.stderr.ref=STDERR
loggers=ftpServiceClient
logger.ftpServiceClient.name=io.datajek.ftpservice.cliclient.SampleCLIClient
logger.ftpServiceClient.level=DEBUG

SampleCLIClient class

Now, we will define the class which serves as the main entry point for the command-line client. Modify the package structure to be src/main/java/io/datajek/ftpservice/cliclient.

Create a class named SampleCLIClient in the cliclient package. This class contains methods to parse command-line arguments, add and delete files from the server, as well as a utility method for printing usage instructions. In the SampleCLIClient class, we will define a logger and the following string constants for command line arguments:

public class SampleCLIClient {
private static final Logger logger = LogManager.getLogger(SampleCLIClient.class);
private final static String ADD = "add";
private final static String OUT = "out";
private final static String DEL = "del";
private final static String SERVER = "server";
}
SampleCLIClient class members

Let’s explain the fields:

  • logger is a logger instance from the Log4j framework. It is used for logging messages, errors, and information during the execution of the client. The logger is initialized using the getLogger method of the LogManager class. It takes the class name SampleCLIClient.class as a parameter, which helps identify the source of log messages.

  • String ADD = "add" is a constant String representing the command-line option for adding a file in our FTP service.

  • String OUT = "out" is a constant String representing the command-line option for specifying the destination filename when adding a file.

  • String DEL = "del" is a constant String representing the command-line option for deleting a file.

  • ...