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

com.usoog.logback.irc.DynamicPircBotXProxy Maven / Gradle / Ivy

Go to download

Logback IRC appender is a very simple appender for the logback project that can log messages to an IRC channel.

The newest version!
/*
 * Copyright (C) 2013  Jimmy Axenhus
 *
 * 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 The performance penalty of the reflection is insignificant as most methods
 * are only called a few times during init of the logging backend. For the
 * performance critical cases (the actual logging itself) the heavy work is done
 * by StaticPircBotXProxy.processLoggingEvents(), which avoids reflection.
 *
 * @author Jimmy Axenhus
 */
public class DynamicPircBotXProxy implements PircBotXProxy {

    /**
     * The Class of the object to delegate to.
     */
    private Class klass;
    /**
     * The object to delegate all the method calls to.
     */
    private PircBotXProxy pircbotXProxy;

    /**
     * Constructor for a new proxy.
     *
     * @param appender The appender to use.
     */
    // For Class cast
    @SuppressWarnings("unchecked")
    public DynamicPircBotXProxy(IrcAppender appender) {
        try {
            klass = (Class) Class.forName("com.usoog.logback.irc.StaticPircBotXProxy");
            pircbotXProxy = klass.getConstructor(IrcAppender.class).newInstance(appender);
        } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) {
            throw new RuntimeException("Fatal error", ex);
        }
    }

    @Override
    public void start() {
        invokePircBotXProxy("start");
    }

    @Override
    public boolean isConnected() {
        return (Boolean) invokePircBotXProxy("isConnected");
    }

    @Override
    public void quit() {
        invokePircBotXProxy("quit");
    }

    @Override
    public void processLoggingEvents(BlockingQueue loggingEvents) {
        try {
            // Special care for this as we got a class, but we want the interface.
            klass.getMethod("processLoggingEvents", BlockingQueue.class).invoke(pircbotXProxy, loggingEvents);
        } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            throw new RuntimeException("Fatal error", ex);
        }
    }

    /**
     * Invokes the specified method on the object to delegate to.
     *
     * @param method The method to invoke.
     * @return The reutrn value from the method being invoked.
     */
    private Object invokePircBotXProxy(String method) {
        try {
            return klass.getMethod(method).invoke(pircbotXProxy);
        } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) {
            throw new RuntimeException("Fatal error", ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy