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

io.bootique.command.CommandRefWithArgs Maven / Gradle / Ivy

Go to download

Bootique is a simple DI-based framework for launching command-line Java applications of any kind. Be it webservices, webapps, jobs, etc.

There is a newer version: 3.0.M2
Show newest version
/*
 * Licensed to ObjectStyle LLC under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ObjectStyle LLC licenses
 * this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.bootique.command;

import java.util.Objects;

/**
 * Stores a reference to a command and a set of invocation arguments. Used to capture auxiliary commands to be run with
 * the main command.
 *
 * @since 0.25
 */
public abstract class CommandRefWithArgs {

    private final String[] args;
    private final boolean terminateOnErrors;

    protected CommandRefWithArgs(String[] args, boolean terminateOnErrors) {
        this.args = args;
        this.terminateOnErrors = terminateOnErrors;
    }

    /**
     * Starts building an invocation with a named command.
     *
     * @param fullCommandName full name of the command.
     */
    public static ArgsBuilder nameRef(String fullCommandName) {
        return new ArgsBuilder(fullCommandName);
    }

    /**
     * Starts building an invocation with a command of a known type.
     */
    public static ArgsBuilder typeRef(Class commandType) {
        return new ArgsBuilder(commandType);
    }

    /**
     * Starts building an invocation with a command of a known type.
     */
    public static NoArgsBuilder commandRef(Command command) {
        return new NoArgsBuilder(command);
    }

    public abstract Command resolve(CommandManager manager);

    public String[] getArgs() {
        return args;
    }

    /**
     * @return true, if Bootique program should terminate, when this invocation fails
     */
    public boolean shouldTerminateOnErrors() {
        return terminateOnErrors;
    }

    static class RefByNameWithArgs extends CommandRefWithArgs {
        private String commandName;

        RefByNameWithArgs(String commandName, String[] args, boolean terminateOnErrors) {
            super(args, terminateOnErrors);
            this.commandName = commandName;
        }

        @Override
        public Command resolve(CommandManager manager) {
            return manager.lookupByName(commandName).getCommand();
        }
    }

    static class RefByTypeWithArgs extends CommandRefWithArgs {
        private Class commandType;

        RefByTypeWithArgs(Class commandType, String[] args, boolean terminateOnErrors) {
            super(args, terminateOnErrors);
            this.commandType = commandType;
        }

        @Override
        public Command resolve(CommandManager manager) {
            return manager.lookupByType(commandType).getCommand();
        }
    }

    static class RefByInstanceWithArgs extends CommandRefWithArgs {
        private Command command;

        RefByInstanceWithArgs(Command command, boolean terminateOnErrors) {
            super(ArgsBuilder.NO_ARGS, terminateOnErrors);
            this.command = command;
        }

        @Override
        public Command resolve(CommandManager manager) {
            return command;
        }
    }

    /**
     * A builder of {@link CommandRefWithArgs} bound to a specific command. In this case the command doesn't have to be
     * registered in Bootique, and hence we can't parse the arguments. So any parameters need to be captured within the
     * command itself.
     *
     * @since 0.25
     */
    public static class NoArgsBuilder> {

        private Command command;
        private boolean terminateOnErrors;

        protected NoArgsBuilder(Command command) {
            this.command = Objects.requireNonNull(command);
        }

        /**
         * Indicate, that Bootique program should terminate, when this invocation fails.
         */
        public NoArgsBuilder terminateOnErrors() {
            this.terminateOnErrors = true;
            return this;
        }

        public CommandRefWithArgs build() {
            return new RefByInstanceWithArgs(command, terminateOnErrors);
        }
    }

    /**
     * @since 0.25
     */
    public static class ArgsBuilder {

        private static final String[] NO_ARGS = new String[0];

        private String commandName;
        private Class commandType;

        private String[] args = NO_ARGS;
        private boolean terminateOnErrors;

        protected ArgsBuilder(String commandName) {
            this.commandName = Objects.requireNonNull(commandName);
        }

        protected ArgsBuilder(Class commandType) {
            this.commandType = Objects.requireNonNull(commandType);
        }

        /**
         * Set command line arguments for this invocation
         */
        public ArgsBuilder arguments(String[] args) {
            this.args = args != null ? args : NO_ARGS;
            return this;
        }

        /**
         * Indicate, that Bootique program should terminate, when this invocation fails.
         */
        public ArgsBuilder terminateOnErrors() {
            this.terminateOnErrors = true;
            return this;
        }

        public CommandRefWithArgs build() {
            return (commandType != null) ?
                    new RefByTypeWithArgs(commandType, args, terminateOnErrors)
                    : new RefByNameWithArgs(commandName, args, terminateOnErrors);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy