001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.net;
015
016import java.io.BufferedInputStream;
017import java.io.IOException;
018import java.net.Socket;
019import java.net.SocketAddress;
020
021import ch.qos.logback.classic.Logger;
022import ch.qos.logback.classic.LoggerContext;
023import ch.qos.logback.classic.net.server.HardenedLoggingEventInputStream;
024import ch.qos.logback.classic.spi.ILoggingEvent;
025
026// Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
027
028/**
029 * Read {@link ILoggingEvent} objects sent from a remote client using Sockets
030 * (TCP). These logging events are logged according to local policy, as if they
031 * were generated locally.
032 * 
033 * <p>
034 * For example, the socket node might decide to log events to a local file and
035 * also resent them to a second socket node.
036 * 
037 * @author Ceki G&uuml;lc&uuml;
038 * @author S&eacute;bastien Pennec
039 * 
040 * @since 0.8.4
041 */
042public class SocketNode implements Runnable {
043
044    Socket socket;
045    LoggerContext context;
046    HardenedLoggingEventInputStream hardenedLoggingEventInputStream;
047    SocketAddress remoteSocketAddress;
048
049    Logger logger;
050    boolean closed = false;
051    SimpleSocketServer socketServer;
052
053    public SocketNode(SimpleSocketServer socketServer, Socket socket, LoggerContext context) {
054        this.socketServer = socketServer;
055        this.socket = socket;
056        remoteSocketAddress = socket.getRemoteSocketAddress();
057        this.context = context;
058        logger = context.getLogger(SocketNode.class);
059    }
060
061    // public
062    // void finalize() {
063    // System.err.println("-------------------------Finalize called");
064    // System.err.flush();
065    // }
066
067    public void run() {
068
069        try {
070            hardenedLoggingEventInputStream = new HardenedLoggingEventInputStream(new BufferedInputStream(socket.getInputStream()));
071        } catch (Exception e) {
072            logger.error("Could not open ObjectInputStream to " + socket, e);
073            closed = true;
074        }
075
076        ILoggingEvent event;
077        Logger remoteLogger;
078
079        try {
080            while (!closed) {
081                // read an event from the wire
082                event = (ILoggingEvent) hardenedLoggingEventInputStream.readObject();
083                // get a logger from the hierarchy. The name of the logger is taken to
084                // be the name contained in the event.
085                remoteLogger = context.getLogger(event.getLoggerName());
086                // apply the logger-level filter
087                if (remoteLogger.isEnabledFor(event.getLevel())) {
088                    // finally log the event as if was generated locally
089                    remoteLogger.callAppenders(event);
090                }
091            }
092        } catch (java.io.EOFException e) {
093            logger.info("Caught java.io.EOFException closing connection.");
094        } catch (java.net.SocketException e) {
095            logger.info("Caught java.net.SocketException closing connection.");
096        } catch (IOException e) {
097            logger.info("Caught java.io.IOException: " + e);
098            logger.info("Closing connection.");
099        } catch (Exception e) {
100            logger.error("Unexpected exception. Closing connection.", e);
101        }
102
103        socketServer.socketNodeClosing(this);
104        close();
105    }
106
107    void close() {
108        if (closed) {
109            return;
110        }
111        closed = true;
112        if (hardenedLoggingEventInputStream != null) {
113            try {
114                hardenedLoggingEventInputStream.close();
115            } catch (IOException e) {
116                logger.warn("Could not close connection.", e);
117            } finally {
118                hardenedLoggingEventInputStream = null;
119            }
120        }
121    }
122
123    @Override
124    public String toString() {
125        return this.getClass().getName() + remoteSocketAddress.toString();
126    }
127}