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.
appenders=console, stderrappender.console.type=Consoleappender.console.name=STDOUTappender.console.target=SYSTEM_OUTappender.console.layout.type=PatternLayoutappender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%nappender.console.filter.threshold.type=ThresholdFilterappender.console.filter.threshold.level=WARNappender.console.filter.threshold.onMismatch=acceptappender.console.filter.threshold.onMatch=denyappender.stderr.type=Consoleappender.stderr.target=SYSTEM_ERRappender.stderr.name=STDERRappender.stderr.layout.type=PatternLayoutappender.stderr.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%nappender.stderr.filter.threshold.type=ThresholdFilterappender.stderr.filter.threshold.level=WARNappender.stderr.filter.threshold.onMatch=acceptrootLogger.level=INFOrootLogger.appenderRefs=stdout, stderrrootLogger.appenderRef.stdout.ref=STDOUTrootLogger.appenderRef.stderr.ref=STDERRloggers=ftpServiceClientlogger.ftpServiceClient.name=io.datajek.ftpservice.cliclient.SampleCLIClientlogger.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";}
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 thegetLogger
method of theLogManager
class. It takes the class nameSampleCLIClient.class
as a parameter, which helps identify the source of log messages.String ADD = "add"
is a constantString
representing the command-line option for adding a file in our FTP service.String OUT = "out"
is a constantString
representing the command-line option for specifying the destination filename when adding a file.String DEL = "del"
is a constantString
representing the command-line option for deleting a file....