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.access.common.net;
15  
16  import java.io.BufferedInputStream;
17  import java.io.IOException;
18  import java.net.Socket;
19  
20  import ch.qos.logback.access.common.spi.AccessContext;
21  import ch.qos.logback.access.common.spi.IAccessEvent;
22  import ch.qos.logback.core.spi.FilterReply;
23  
24  // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
25  
26  /**
27   * Read {@link IAccessEvent} objects sent from a remote client using Sockets
28   * (TCP). These logging events are logged according to local policy, as if they
29   * were generated locally.
30   * 
31   * <p>
32   * For example, the socket node might decide to log events to a local file and
33   * also resent them to a second socket node.
34   * 
35   * @author Ceki G&uuml;lc&uuml;
36   * @author S&eacute;bastien Pennec
37   * 
38   * @since 0.8.4
39   */
40  public class SocketNode implements Runnable {
41  
42      Socket socket;
43      AccessContext context;
44      HardenedAccessEventInputStream hardenedOIS;
45  
46      public SocketNode(Socket socket, AccessContext context) {
47          this.socket = socket;
48          this.context = context;
49          try {
50              hardenedOIS = new HardenedAccessEventInputStream(new BufferedInputStream(socket.getInputStream()));
51          } catch (Exception e) {
52              System.out.println("Could not open HardenedObjectInputStream to " + socket + e);
53          }
54      }
55  
56      @Override
57      public void run() {
58          IAccessEvent event;
59  
60          try {
61              while (true) {
62                  // read an event from the wire
63                  event = (IAccessEvent) hardenedOIS.readObject();
64                  // check that the event should be logged
65                  if (context.getFilterChainDecision(event) == FilterReply.DENY) {
66                      break;
67                  }
68                  // send it to the appenders
69                  context.callAppenders(event);
70              }
71          } catch (java.io.EOFException e) {
72              System.out.println("Caught java.io.EOFException closing connection.");
73          } catch (java.net.SocketException e) {
74              System.out.println("Caught java.net.SocketException closing connection.");
75          } catch (IOException e) {
76              System.out.println("Caught java.io.IOException: " + e);
77              System.out.println("Closing connection.");
78          } catch (Exception e) {
79              System.out.println("Unexpected exception. Closing connection." + e);
80          }
81  
82          try {
83              hardenedOIS.close();
84          } catch (Exception e) {
85              System.out.println("Could not close connection." + e);
86          }
87      }
88  }