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

org.graylog2.bindings.MessageOutputBindings Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * 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
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.bindings;

import com.google.common.base.Strings;
import com.google.inject.Key;
import com.google.inject.Scopes;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.OptionalBinder;
import org.graylog2.Configuration;
import org.graylog2.outputs.BlockingBatchedESOutput;
import org.graylog2.outputs.DefaultMessageOutput;
import org.graylog2.outputs.GelfOutput;
import org.graylog2.outputs.LoggingOutput;
import org.graylog2.plugin.inject.Graylog2Module;
import org.graylog2.plugin.outputs.MessageOutput;
import org.graylog2.shared.plugins.ChainingClassLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Dennis Oelkers 
 */
public class MessageOutputBindings extends Graylog2Module {
    private static final Logger LOG = LoggerFactory.getLogger(MessageOutputBindings.class);

    private final Configuration configuration;
    private final ChainingClassLoader chainingClassLoader;

    public MessageOutputBindings(final Configuration configuration, ChainingClassLoader chainingClassLoader) {
        this.configuration = configuration;
        this.chainingClassLoader = chainingClassLoader;
    }

    @Override
    protected void configure() {
        final Class defaultMessageOutputClass = getDefaultMessageOutputClass(BlockingBatchedESOutput.class);
        LOG.debug("Using default message output class: {}", defaultMessageOutputClass.getCanonicalName());
        OptionalBinder.newOptionalBinder(binder(), Key.get(MessageOutput.class, DefaultMessageOutput.class))
                .setDefault().to(defaultMessageOutputClass).in(Scopes.SINGLETON);

        final MapBinder> outputMapBinder = outputsMapBinder();
        installOutput(outputMapBinder, GelfOutput.class, GelfOutput.Factory.class);
        installOutput(outputMapBinder, LoggingOutput.class, LoggingOutput.Factory.class);
    }

    private Class getDefaultMessageOutputClass(Class fallbackClass) {
        // Just use the default fallback if nothing is configured. This is the default case.
        if (Strings.isNullOrEmpty(configuration.getDefaultMessageOutputClass())) {
            return fallbackClass;
        }

        try {
            @SuppressWarnings("unchecked")
            final Class defaultMessageOutputClass = (Class) chainingClassLoader.loadClass(configuration.getDefaultMessageOutputClass());

            if (MessageOutput.class.isAssignableFrom(defaultMessageOutputClass)) {
                LOG.info("Using {} as default message output", defaultMessageOutputClass.getCanonicalName());
                return defaultMessageOutputClass;
            } else {
                LOG.warn("Class \"{}\" is not a subclass of \"{}\". Using \"{}\" as default message output",
                        configuration.getDefaultMessageOutputClass(),
                        MessageOutput.class.getCanonicalName(),
                        fallbackClass.getCanonicalName());
                return fallbackClass;
            }
        } catch (ClassNotFoundException e) {
            LOG.warn("Unable to find default message output class \"{}\", using \"{}\"",
                    configuration.getDefaultMessageOutputClass(),
                    fallbackClass.getCanonicalName());
            return fallbackClass;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy