/fj-tool-util

Runtime tool utilities

Primary LanguageJavaApache License 2.0Apache-2.0

fj-tool-util

Runtime tool utilities

Keep a Changelog v1.1.0 badge Maven Central license code of conduct Quality Gate Status Coverage

Java runtime version Java build version Apache Maven Fugerit Github Project Conventions

Sample helpers class to build java standalone tools

1. ArgHelper

Check if all the parameters needed are set. If not a ConfigRuntimeException with code

try {
    ParamHolder holder = ParamHolder.newAndHolder(
            ParamHolder.newHolder("arg1"),
            ParamHolder.newOrHolder("arg2", "arg3"));
    if ( ArgHelper.checkAllRequiredThrowRuntimeEx(params, holder) ) {
        // code
    }
} catch (ConfigRuntimeException e) {
    if ( e.getCode() == MainHelper.FAIL_MISSING_REQUIRED_PARAM ) {
        log.info( "Missing parameters : {}", e.getMessage() );    
    } else {
        // code
    }
}

2. MainHelper

Handle main method

public static void main( String[] args ) {
    MainHelper.handleMain( () -> {
        // code    
    } ); 
}

By default any exception will be evaluated as follow in an exit code

public static int evaluateExitCode(Throwable ex ) {
    if ( ex instanceof CodeEx) {
        CodeEx codeEx = (CodeEx) ex;
        return codeEx.getCode();
    } else if ( ex != null ) {
        return FAIL_DEFAULT;
    } else {
        return OK_DEFAULT;
    }
}

Behaviours may be customized, for instance this way exit code will be printed with no System.exit() call :

MainHelper.setDefaultExitAction( ec -> log.warn( "Exit : {}", ec ) );