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

org.embulk.cli.Main Maven / Gradle / Ivy

package org.embulk.cli;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.jar.Manifest;
import org.embulk.EmbulkSystemProperties;
import org.embulk.deps.EmbulkDependencyClassLoaders;
import org.embulk.deps.EmbulkSelfContainedJarFiles;
import org.embulk.deps.cli.CommandLineParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.SubstituteLoggingEvent;
import org.slf4j.helpers.SubstituteLogger;

public class Main {
    public static void main(final String[] args) {
        final Manifest manifest = CliManifest.getManifest();

        // They are loaded before SLF4J is initialized along with Logback. They don't use SLF4J for error logging.
        EmbulkSelfContainedJarFiles.staticInitializer().addFromManifest(manifest).initialize();
        EmbulkDependencyClassLoaders.staticInitializer().useSelfContainedJarFiles().initialize();

        // Using SubstituteLogger here because SLF4J and Logback are initialized later (CliLogbackConfigurator.configure).
        final LinkedBlockingQueue loggingEventQueue = new LinkedBlockingQueue();
        final SubstituteLogger substituteLogger = new SubstituteLogger(Main.class.getName(), loggingEventQueue, false);

        final CommandLineParser commandLineParser = CommandLineParser.create();
        final CommandLine commandLine = commandLineParser.parse(Arrays.asList(args), substituteLogger);
        System.err.print(commandLine.getStdErr());
        System.out.print(commandLine.getStdOut());

        final Properties commandLineProperties = commandLine.getCommandLineProperties();
        final EmbulkSystemPropertiesBuilder embulkSystemPropertiesBuilder = EmbulkSystemPropertiesBuilder.from(
                System.getProperties(), commandLineProperties, System.getenv(), manifest, substituteLogger);
        final EmbulkSystemProperties embulkSystemProperties = embulkSystemPropertiesBuilder.buildProperties();

        CliLogbackConfigurator.configure(embulkSystemProperties);

        // Creating a real logger after SLF4J is initialized along with Logback. Flushing recorded logs by SubstituteLogger, then.
        final Logger mainLogger = LoggerFactory.getLogger(Main.class);
        flushLogs(substituteLogger, loggingEventQueue, mainLogger);

        if (!commandLine.isValid()) {
            System.exit(-1);
            return;
        }

        if (mainLogger.isDebugEnabled()) {
            mainLogger.debug("Command-line arguments: {}", commandLine.getArguments());
            mainLogger.debug("Embulk system properties: {}", embulkSystemProperties);
        }

        System.exit(EmbulkRun.run(commandLine, embulkSystemProperties));
    }

    private static void flushLogs(
            final SubstituteLogger substituteLogger,
            final LinkedBlockingQueue loggingEventQueue,
            final Logger mainLogger) {
        substituteLogger.setDelegate(mainLogger);
        final ArrayList events = new ArrayList<>();
        loggingEventQueue.drainTo(events);
        for (final SubstituteLoggingEvent event : events) {
            substituteLogger.log(event);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy