ru.xxlabaza.popa.Main.java Source code

Java tutorial

Introduction

Here is the source code for ru.xxlabaza.popa.Main.java

Source

/*
 * Copyright 2016 Artem Labazin <xxlabaza@gmail.com>.
 *
 * Licensed 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 ru.xxlabaza.popa;

import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import ru.xxlabaza.popa.command.Command;
import ru.xxlabaza.popa.command.CommandPrintHelp;

/**
 * @author Artem Labazin <xxlabaza@gmail.com>
 * @since 25.02.2016
 */
@Slf4j
@Configuration
@Import({ GroovyTemplateAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
        AppConfiguration.class })
public class Main implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Main.class).logStartupInfo(false).run(args);
    }

    @Autowired
    private CommandPrintHelp commandHelp;

    @Resource
    private Map<Option, Command> commandsMap;

    @Autowired
    private ConfigurableApplicationContext context;

    @Autowired
    private Options options;

    @Override
    public void run(String... args) throws Exception {
        val commandLine = parseArguments(args);
        for (Option option : commandLine.getOptions()) {
            commandsMap.get(option).execute(option.getValues());
        }
        //        exit(0);
    }

    private void exit(int code) {
        context.close();
        System.exit(code);
    }

    private CommandLine parseArguments(String[] args) {
        val parser = new DefaultParser();
        try {
            return parser.parse(options, args);
        } catch (ParseException ex) {
            log.error("Parsing arguments failed");
            log.error("Reason: {}", ex.getMessage());
            commandHelp.execute(null);
            exit(1);
        }
        return null;
    }
}