KivaKit Applications – Java Code Geeks


The application-kivakit module contains building blocks for building applications and servers. In the diagram below, we can see that the Application the class expands Basic component. Server, in turn, extends Application. Basic component inherited Repeater functionality of BaseRepeater, and practical default methods of Making up interface. ComponentMixin (shown in the following diagram) also inherits these methods from Making up.

Application provides command line analysis (see KivaKit Command Line Analysis) and application lifecycle methods. In addition, it inherits the functionality of Basic component to save and locate objects and parameters (see Kivakit components and parameters):

Example

In the following example, we’ll create an application that counts the lines of its file argument. Without arguments, the application will provide detailed help. With the argument -show-file-size = true, it will display the file size in bytes.

Application initialization

To start our running application, we provide code similar to this:

public class ApplicationExample extends Application
{
    public static void main(String[] arguments)
    {
        new ApplicationExample().run(arguments);
    }
    
    private ApplicationExample()
    {
        super(ApplicationExampleProject());
    }        
    
    [...]
    
    @Override
    protected void onRun()
    {
        [...]
    }        
}

The main () The method creates an instance of the application and the application constructor passes an instance of Project to the superclass. The application then calls Course(), which initializes the project and the application. When the application is fully initialized and ready to operate, the onRun () method is called.

Project initialization

To initialize the project of our application, we create a subclass of Project, which provides any required initialization logic, as well as a set of dependent projects retrieved through the Dependencies() method. When the application runs, its project and all subprojects in its project dependency tree will be initialized before onRun () is called:

public class ApplicationExampleProject extends Project
{
    private static Lazy<ApplicationExampleProject> project = 
        Lazy.of(ApplicationExampleProject::new);

    public static ApplicationExampleProject get()
    {
        return project.get();
    }

    protected ApplicationExampleProject()
    {
    }

    @Override
    public Set<Project> dependencies()
    {
        return Set.of(ResourceProject.get());
    }
}

Command line analysis and application logic

Once our sample application is initialized, onRun () provides the logic of the application:

private ArgumentParser<File> INPUT =
        fileArgumentParser("Input text file")
                .required()
                .build();

private SwitchParser<Boolean> SHOW_FILE_SIZE =
        booleanSwitchParser("show-file-size", "Show the file size in bytes")
                .optional()
                .defaultValue(false)
                .build();
                
@Override
public String description()
{
    return "Example application that counts the number of lines" +
           " in the file argument passed to the command line";
}

@Override
protected void onRun()
{
    var input = argument(INPUT);

    if (input.exists())
    {
        showFile(input);
    }
    else
    {
        problem("File does not exist: $", input.path());
    }
}

@Override
protected List<ArgumentParser<?>> argumentParsers()
{
    return List.of(INPUT);
}

@Override
protected Set<SwitchParser<?>> switchParsers()
{
    return Set.of(SHOW_FILE_SIZE);
}

private void showFile(File input)
{
    if (get(SHOW_FILE_SIZE))
    {
        information("File size = $", input.sizeInBytes());
    }

    information("Lines = $", Count.count(input.reader().lines()));
}

When the Application.run () method is called, the switch and argument parsers for the application are retrieved from switchParsors () and argument analyzers (), respectively. Logic in Application.run () then uses these analyzers to analyze the String of characters[] argument that was passed to main () in Command line object.

The onRun () method in our sample application calls argument() with the GRAB file argument parser to get input To file object of the Command line:

var input = argument(INPUT);

Then, if the file exists, our sample app calls showFile () to display the number of lines in the file. In this same method, if the boolean switch SHOW_FILE_SIZE is true, it also displays the file size in bytes:

if (get(SHOW_FILE_SIZE))
{
    information("File size = $", input.sizeInBytes());
}

information("Lines = $", Count.count(input.reader().lines()));

To help

Finally, if something is wrong with interpreting our application’s command line arguments, KivaKit will capture all broadcast messages, as well as information from argument analyzers and switches. It will then use this information to provide detailed help on what went wrong and how to properly use the app:

┏━━━━━━━━━━━━━━━━━┫ COMMAND LINE ERROR(S) ┣━━━━━━━━━━━━━━━━━┓
┋     ○ Required File argument "Input text file" is missing ┋
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 

KivaKit 0.9.9-SNAPSHOT (puffy telephone)

Usage: ApplicationExample 0.9.9-SNAPSHOT <switches> <arguments>

Example application that counts the number of lines in the file argument passed to the command line

Arguments:

  1. File (required) - Input text file

Switches:

    Optional:

  -show-file-size=Boolean (optional, default: false) : Show the file size in bytes

Coded

The complete code for the example shown here is available in the kivakit-examples deposit. The Maven dependency for application-kivakit is:

<dependency>
    <groupId>com.telenav.kivakit</groupId>
    <artifactId>kivakit-application</artifactId>
    <version>${kivakit.version}</version>
</dependency>
Posted on Java Code Geeks with the permission of Jonathan Locke, partner of our JCG program. See the original article here: KivaKit apps

The opinions expressed by contributors to Java Code Geeks are their own.



Source link