All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dbstarll.utils.lang.launcher.AbstractLauncher Maven / Gradle / Ivy

The newest version!
package io.github.dbstarll.utils.lang.launcher;

import org.apache.commons.lang3.ArrayUtils;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractLauncher implements Task {
    private final Map> tasks = new HashMap>();

    protected final void addTask(String token, Class taskClass) {
        tasks.put(token, taskClass);
    }

    @Override
    public final int run(String... args) throws Throwable {
        if (ArrayUtils.isEmpty(args)) {
            throw new IllegalArgumentException("Need task token.");
        }

        String token = args[0];
        Class taskClass = tasks.get(token);
        if (taskClass == null) {
            throw new IllegalArgumentException("Unknown task token: " + token);
        }

        try {
            return run(taskClass, ArrayUtils.subarray(args, 1, args.length));
        } catch (LaunchException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    protected abstract int run(Class taskClass, String... args) throws LaunchException, Throwable;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy