View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net.server;
15  
16  import java.io.EOFException;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.net.Socket;
20  
21  import ch.qos.logback.classic.Logger;
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.spi.ILoggingEvent;
24  import ch.qos.logback.core.net.HardenedObjectInputStream;
25  import ch.qos.logback.core.util.CloseUtil;
26  
27  /**
28   * A {@link RemoteAppenderClient} that reads serialized {@link ILoggingEvent} 
29   * objects from an {@link InputStream}.
30   *
31   * @author Carl Harris
32   */
33  class RemoteAppenderStreamClient implements RemoteAppenderClient {
34  
35      private final String id;
36      private final Socket socket;
37      private final InputStream inputStream;
38  
39      private LoggerContext lc;
40      private Logger logger;
41  
42      /**
43       * Constructs a new client.  
44       * @param id a display name for the client
45       * @param inputStream input stream from which events will be read
46       */
47      public RemoteAppenderStreamClient(String id, Socket socket) {
48          this.id = id;
49          this.socket = socket;
50          this.inputStream = null;
51      }
52  
53      /**
54       * Constructs a new client.
55       * <p>
56       * This constructor is provided primarily to support unit tests for which
57       * it is inconvenient to create a socket.
58       *  
59       * @param id a display name for the client
60       * @param inputStream input stream from which events will be read
61       */
62      public RemoteAppenderStreamClient(String id, InputStream inputStream) {
63          this.id = id;
64          this.socket = null;
65          this.inputStream = inputStream;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public void setLoggerContext(LoggerContext lc) {
72          this.lc = lc;
73          this.logger = lc.getLogger(getClass().getPackage().getName());
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public void close() {
80          if (socket == null)
81              return;
82          CloseUtil.closeQuietly(socket);
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public void run() {
89          logger.info(this + ": connected");
90          HardenedObjectInputStream ois = null;
91          try {
92              ois = createObjectInputStream();
93              while (true) {
94                  // read an event from the wire
95                  ILoggingEvent event = (ILoggingEvent) ois.readObject();
96                  // get a logger from the hierarchy. The name of the logger is taken to
97                  // be the name contained in the event.
98                  Logger remoteLogger = lc.getLogger(event.getLoggerName());
99                  // apply the logger-level filter
100                 if (remoteLogger.isEnabledFor(event.getLevel())) {
101                     // finally log the event as if was generated locally
102                     remoteLogger.callAppenders(event);
103                 }
104             }
105         } catch (EOFException ex) {
106             // this is normal and expected
107             assert true;
108         } catch (IOException ex) {
109             logger.info(this + ": " + ex);
110         } catch (ClassNotFoundException ex) {
111             logger.error(this + ": unknown event class");
112         } catch (RuntimeException ex) {
113             logger.error(this + ": " + ex);
114         } finally {
115             if (ois != null) {
116                 CloseUtil.closeQuietly(ois);
117             }
118             close();
119             logger.info(this + ": connection closed");
120         }
121     }
122 
123     private HardenedObjectInputStream createObjectInputStream() throws IOException {
124         if (inputStream != null) {
125             return new HardenedLoggingEventInputStream(inputStream);
126         }
127         return new HardenedLoggingEventInputStream(socket.getInputStream());
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public String toString() {
135         return "client " + id;
136     }
137 
138 }