Java Reference

Applications

Introduction

This article gives a high-level view of the KOS application class, which is the only required class in a KOS application.

Overview

The only required class in your KOS application is a class that extends one of the two abstract application classes:

  • SystemApplication

  • Application

Every application must have one and only one "system" app, which means all of our examples extend SystemApplication. The SystemApplication extends Application and simply adds a handful of convenience methods.

The KOS system makes callbacks to methods in your system application class:

  • load() : called when the application is loading; used for initialization

  • start() : called when the app is starting

  • started() : called after start() returns; indicates all components are ready

  • stop() : called when the application is stopping (the system app is never stopped)

  • unload() : called when the app is unloading from memory

The two methods of most interest are load() and start().

Tip

This page gives an overview of the KOS application class you must create. There are references to many other objects, like handles, config objects, bean context, etc. Details about those items are covered in their own articles, so just focus on the general design at this time.

API Components

This section gives the summary of the Application, SystemApplication, and BaseAppConfig classes.

Application

This is the definition of the abstract Application class.

API: Application class
public abstract class Application<T extends BaseAppConfig>
    implements ConfigAware<T>, ContextHandleAware, JsonDescriptorAware {

    //--- App ID and descriptor ---

    // Returns the ID of this application, which comes from the "kos.app.appId" field of the "descriptor.json" file:
    public String getAppId();

    // Returns the descriptor from this application's KAB file (descriptor.json):
    public JsonDescriptor getDescriptor();

    //--- Handle ---

    // Returns the application's handle:
    public Handle getHandle();

    // Returns the application's handle prefix:
    public String getHandlePrefix();

    //--- Config ---

    // Returns the application's config bean:
    public T getConfig();

    // Sets the application's config bean:
    public void setConfig(T config);

    //--- App directory and files ---

    // Returns the base directory of the application:
    public File getAppDir();

    // Adds the specified path to the base application directory and returns it as a File:
    public File getAppFile(String path);

    //--- Data directory and files ---

    // Returns the base directory of the application's local storage area:
    public File getDataDir();

    // Adds the specified path to the base application data directory and returns it as a File:
    public File getDataFile(String path);

    //--- Context and VFS ---

    // Returns this applicaton's bean context:
    public BeanContext getCtx();

    // Returns the root VFS (virtual file system) for this application:
    public VFS getVfs();

    //--- KAB file and related ---

    // Returns the application KAB file, allowing for direct access to it contents:
    public KabFile getKab();

    // Returns the resolved manifest section associated with the application:
    public ResolvedManifestSection getSection();

    // Returns the storage domain associated with the application:
    public StorageDomain getStorage();

    //--- Databases ---

    // Opens or creates a database with the specified name in the local storage area of the application:
    public Jdbi openDatabase(String name, DatabaseMigrator migrator);

    //--- Profiles ---

    // Returns true if the given profile is in effect:
    public boolean hasProfile(String profile);

    //--- Methods called by KOS during the application's lifecycle ---

    // Called when the application is first loaded; used for application initialization:
    public void load() throws Exception;

    // Called when the application is started. Once this returns, the application is considered to be running:
    public abstract void start() throws Exception;

    // Called after start() returns. When this is called, all application VFS and endpoints are available:
    public void started() throws Exception;

    // Called to stop the application:
    public abstract void stop() throws Exception;

    // Called when the application is unloaded from memory:
    public void unload() throws Exception;
}

SystemApplication

This is the definition of the abstract SystemApplication class.

API: SystemApplication class
public abstract class SystemApplication<T extends BaseAppConfig> extends Application<T> {

    // Installs an assembly; simply calls AssemblyService.install():
    public void installAssembly(Assembly assembly);

    // Installs regions for the application:
    public void installRegions(Collection<BaseRegion> regions);

    // Install regions for the application with a custom settings bean:
    public void installRegions(Collection<BaseRegion> regions, RegionSettings settings);

    // System applications are never stopped:
    public void stop() throws Exception;
}
Note

Every KOS application must have one and only one class derived from SystemApplication.

BaseAppConfig

This is the definition of the abstract BaseAppConfig class.

API: BaseAppConfig class
public class BaseAppConfig extends ConfigBean {

    //--- These methods come from the ConfigBean class ---

    // Returns true once the bean is configured:
    public boolean isConfigured();

    // Returns the associated ready indicator:
    public ReadyIndicator getReady();

    // Adds a listener to the list:
    public void addListener(ConfigBeanListener listener);

    // Removes a listener from the list:
    public void removeListener(ConfigBeanListener listener);
}

Example Code

This section provides example code to demonstrate some of the API components.

Source code for this and most other Java reference pages is available on GitHub.

Log basic data

Running the following test code:

@Slf4j
public class ApplicationModule implements Module {

    private MyKosApp myKosApp;

    @Override
    public void init(BeanContext ctx) {
        log.info("> ApplicationModule.init()");
        myKosApp = ctx.getBean(MyKosApp.class);
    }

    @Override
    public void run() {
        log.info("> ApplicationModule.run()");

        log.info("> appId           : {}", myKosApp.getAppId());
        log.info("> descriptor.json : {}", myKosApp.getDescriptor().getRoot().toPrettyString());

        log.info("> appDir          : {}", myKosApp.getAppDir());
        log.info("> dataDir         : {}", myKosApp.getDataDir());

        log.info("> appFile         : {}", myKosApp.getAppFile("bogus1.txt"));
        log.info("> dataFile        : {}", myKosApp.getDataFile("bogus2.txt"));

        log.info("> handle          : {}", myKosApp.getHandle());
        log.info("> handlePrefix    : {}", myKosApp.getHandlePrefix());
        log.info("> config          : {}", myKosApp.getConfig());
    }
}

Gives this log output:

> ====== BEGIN =========================================
> MyKosApp.load()
> ApplicationModule.init()
> MyKosApp.start()
> ApplicationModule.run()
> appId           : system      (1)
> descriptor.json : {           (2)
  "kos" : {
    "app" : {
      "appClass" : "com.example.MyKosApp",
      "appId" : "system"
    }
  }
}
> appDir          : /mnt/fuse/app_system                            (3)
> dataDir         : /mnt/datafs/myNodeType/apps/system              (4)
> appFile         : /mnt/fuse/app_system/bogus1.txt                 (3)
> dataFile        : /mnt/datafs/myNodeType/apps/system/bogus2.txt   (4)
> handle          : system.app                                      (5)
> handlePrefix    : null
> config          : com.tccc.kos.core.service.app.BaseAppConfig@1a0e2b15
> ====== END ===========================================
1 The application’s ID, which must be "system" since it’s the system app
2 The contents of the app’s descriptor.json file
3 The location of the app directory
4 The location of the data directory
5 The handle (path) of this system app

Summary

Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.