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

com.redislabs.picocliredis.Main Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.redislabs.picocliredis;

import java.security.Security;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import org.slf4j.LoggerFactory;

import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.JdkLoggerFactory;
import lombok.Getter;
import lombok.Setter;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.PicocliException;
import picocli.CommandLine.Spec;

@Command(abbreviateSynopsis = true, mixinStandardHelpOptions = true, versionProvider = ManifestVersionProvider.class, usageHelpAutoWidth = true, subcommands = HiddenGenerateCompletion.class, sortOptions = false)
public class Main implements Runnable {

	@Spec
	private CommandSpec spec;

	private final static String DNS_CACHE_TTL = "networkaddress.cache.ttl";
	private final static String DNS_CACHE_NEGATIVE_TTL = "networkaddress.cache.negative.ttl";
//	private final static String SSL_PREFIX = "javax.net.ssl.";
//	private final static String SSL_TRUST_STORE = SSL_PREFIX + "trustStore";
//	private final static String SSL_TRUST_STORE_TYPE = SSL_PREFIX + "trustStoreType";
//	private final static String SSL_TRUST_STORE_PASSWORD = SSL_PREFIX + "trustStorePassword";
//	private final static String SSL_KEY_STORE = SSL_PREFIX + "keyStore";
//	private final static String SSL_KEY_STORE_TYPE = SSL_PREFIX + "keyStoreType";
//	private final static String SSL_KEY_STORE_PASSWORD = SSL_PREFIX + "keyStorePassword";

	private static final String ROOT_LOGGER = "";

	@Mixin
	private @Getter @Setter BaseOptions options = new BaseOptions();

	public int execute(String[] args) {
		CommandLine commandLine = new CommandLine(this);
		registerConverters(commandLine);
		commandLine.setCaseInsensitiveEnumValuesAllowed(true);
		try {
			ParseResult parseResult = commandLine.parseArgs(args);
			configureLogging();
			configureDns();
			return commandLine.getExecutionStrategy().execute(parseResult);
		} catch (PicocliException e) {
			System.err.println(e.getMessage());
			return 1;
		}
	}

	protected void registerConverters(CommandLine commandLine) {
		commandLine.registerConverter(Server.class, s -> new Server(s));
	}

	private void configureDns() {
		org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
		log.debug("Setting {}={}", DNS_CACHE_TTL, options.dnsTtl());
		Security.setProperty(DNS_CACHE_TTL, String.valueOf(options.dnsTtl()));
		log.debug("Setting {}={}", DNS_CACHE_NEGATIVE_TTL, options.dnsNegativeTtl());
		Security.setProperty(DNS_CACHE_NEGATIVE_TTL, String.valueOf(options.dnsNegativeTtl()));
	}

	private void configureLogging() {
		InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
		LogManager.getLogManager().reset();
		Logger activeLogger = Logger.getLogger(ROOT_LOGGER);
		ConsoleHandler handler = new ConsoleHandler();
		handler.setLevel(Level.ALL);
		handler.setFormatter(new OneLineLogFormat(options.debug()));
		activeLogger.addHandler(handler);
		Logger.getLogger(ROOT_LOGGER).setLevel(rootLoggingLevel());
		Logger.getLogger(getClass().getPackage().getName()).setLevel(packageLoggingLevel());
	}

	private Level packageLoggingLevel() {
		if (options.debug()) {
			return Level.FINEST;
		}
		if (options.verbose()) {
			return Level.FINE;
		}
		if (options.quiet()) {
			return Level.OFF;
		}
		return Level.INFO;
	}

	private Level rootLoggingLevel() {
		if (options.debug()) {
			return Level.FINE;
		}
		if (options.verbose()) {
			return Level.INFO;
		}
		if (options.quiet()) {
			return Level.OFF;
		}
		return Level.SEVERE;
	}

	@Override
	public void run() {
		CommandLine.usage(this, System.out);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy