com.braffdev.server.core.config.ConfigLogger.java Source code

Java tutorial

Introduction

Here is the source code for com.braffdev.server.core.config.ConfigLogger.java

Source

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *  Copyright (C) 2015, Markus Staudt <info@braffdev.com>                  *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package com.braffdev.server.core.config;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.braffdev.server.core.config.handler.ClassConfig;
import com.braffdev.server.core.config.types.server.AcceptorConfig;
import com.braffdev.server.core.config.types.server.SSLConfig;
import com.braffdev.server.core.config.types.server.ServerConfig;
import com.braffdev.server.core.io.logger.Logger;

/**
 *
 */
public class ConfigLogger {

    private static final Logger LOGGER = Logger.getLogger(ConfigLogger.class);
    private static final int LENGTH = 40;

    /**
     * Logs the given config with a pretty message.
     *
     * @param config the config to log.
     */
    public void log(ServerConfig config) {
        // directories
        this.log();
        this.logSection("Directories");

        this.log("Working directory", config.getWorkingDirectory());
        this.log("Working directory: config", config.getWorkingDirectoryConfig());
        this.log("Working directory: containers", config.getWorkingDirectoryContainers());
        this.log("Working directory: logs", config.getWorkingDirectoryLogs());

        // general
        this.log();
        this.logSection("General");

        this.log("Log: debug", config.getLog().isDebug());
        this.log();

        this.log("Session: id cookie name", config.getSession().getSessionIdCookieName());
        this.log("Session: timeout", config.getSession().getTimeoutInMillis() + " ms");
        this.log("Session: time between checks", config.getSession().getTimeBetweenTimeoutChecksInMillis() + " ms");

        // http
        this.log();
        this.logSection("HTTP");
        this.log("TRACE enabled", config.getHttp().isTraceEnabled());
        this.log("OPTIONS enabled", config.getHttp().isOptionsEnabled());
        this.log("Socket timeout", config.getHttp().getSocketTimeoutInMillis() + " ms");
        this.log();

        this.logSsl(config.getHttp().getSsl());

        // acceptors
        this.log();
        this.logSection("Acceptors");
        this.logAcceptors(config.getAcceptors());

        // file handlers
        this.log();
        this.logSection("File handlers");
        this.logHandlers(config.getFileHandlers());

        // controller result handlers
        this.log();
        this.logSection("Controller result handlers");
        this.logHandlers(config.getControllerResultHandlers());
        this.log();
    }

    /**
     * Logs the file handler config.
     *
     * @param fileHandlers the list of file handlers to log.
     */
    private void logHandlers(List<ClassConfig> fileHandlers) {
        if (CollectionUtils.isNotEmpty(fileHandlers)) {

            for (ClassConfig fileHandler : fileHandlers) {
                this.log("Class", fileHandler.getClazz());
            }

        } else {
            this.log("No file handlers configured");
        }
    }

    /**
     * Logs the acceptor config.
     *
     * @param acceptors the list of acceptors to log.
     */
    private void logAcceptors(List<AcceptorConfig> acceptors) {
        if (CollectionUtils.isNotEmpty(acceptors)) {

            this.logAcceptor(acceptors.get(0));

            for (int i = 1; i < acceptors.size(); i++) {
                this.logAcceptor(acceptors.get(i));
                this.log();
            }

        } else {
            this.log("No acceptors configured");
            this.log();
        }
    }

    /**
     * Logs the ssl config.
     *
     * @param ssl the ssl config log.
     */
    private void logSsl(SSLConfig ssl) {
        if (ssl != null) {

            this.log("SSL: keystore", ssl.getKeystore());
            this.log("SSL: password", ssl.getPassword());
            this.log("SSL: supported protocols", ssl.getSupportedProtocols());

        } else {
            this.log("SSL", "disabled");
        }
    }

    /**
     * Logs the acceptor config.
     *
     * @param config the acceptor config log.
     */
    private void logAcceptor(AcceptorConfig config) {
        this.log("Port", config.getPort());
        this.log("SSL", config.isUseSsl());
    }

    /**
     * Logs the beginning of a new section.
     *
     * @param name the name of the section.
     */
    private void logSection(String name) {
        StringBuilder builder = new StringBuilder();
        builder.append(" ___");
        builder.append(name);

        while (builder.length() < LENGTH) {
            builder.append("_");
        }

        LOGGER.info(builder.toString());
        this.log();
    }

    /**
     * Logs the given text.
     *
     * @param text the text to log.
     */
    private void log(String text) {
        this.log(text, null);
    }

    /**
     * Logs the given values.
     *
     * @param key the key to log.
     * @param value the value to log.
     */
    private void log(String key, Object value) {
        StringBuilder builder = new StringBuilder();
        builder.append("    ");
        builder.append(key);

        while (builder.length() < LENGTH) {
            builder.append(" ");
        }

        if (value != null) {
            builder.append(": ");
            builder.append(value);
        }

        LOGGER.info(builder.toString());
    }

    /**
     * Logs an empty line.
     */
    private void log() {
        LOGGER.info("");
    }

}