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

io.bootique.command.ExecutionPlanBuilder 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 io.bootique.cli.CliFactory;
import io.bootique.log.BootLogger;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import javax.inject.Provider;

/**
 * Optionally decorates commands with the code to execute additional auxiliary commands if those are configured for
 * a given type of command.
 *
 * @since 0.25
 */
public class ExecutionPlanBuilder {

    private BootLogger logger;
    private Provider cliFactoryProvider;
    private Provider commandManagerProvider;
    private Provider executorProvider;
    private Map, CommandDecorator> decorators;

    public ExecutionPlanBuilder(
            Provider cliFactoryProvider,
            Provider commandManagerProvider,
            Provider executorProvider,
            Map, CommandDecorator> decorators,
            BootLogger logger) {

        this.logger = logger;
        this.decorators = decorators;
        this.cliFactoryProvider = cliFactoryProvider;
        this.commandManagerProvider = commandManagerProvider;
        this.executorProvider = executorProvider;
    }

    public static Map, CommandDecorator> mergeDecorators(
            Set decoratorSet) {


        if (decoratorSet.isEmpty()) {
            return Collections.emptyMap();
        }

        Map, CommandDecorator.Builder> mergedMutable
                = new HashMap<>((int) (decoratorSet.size() / 0.75));

        decoratorSet.forEach(ref -> {
            mergedMutable.computeIfAbsent(ref.getCommandType(), c -> CommandDecorator.builder())
                    .copyFrom(ref.getDecorator());
        });

        Map, CommandDecorator> merged = new HashMap<>();
        mergedMutable.forEach((k, v) -> merged.put(k, v.build()));
        return merged;
    }

    /**
     * Optionally decorates provided command to execute additional auxiliary commands if those are configured for
     * this type of command.
     *
     * @param mainCommand the primary command whose execution plan was requested.
     * @return either main command or a decorated main command.
     */
    public Command prepareForExecution(Command mainCommand) {

        if (decorators.isEmpty()) {
            return mainCommand;
        }

        CommandDecorator decorator = decorators.get(mainCommand.getClass());
        if (decorator == null) {
            return mainCommand;
        }

        return new MultiCommand(
                mainCommand,
                decorator,
                cliFactoryProvider,
                commandManagerProvider,
                executorProvider,
                logger);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy