View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.access.net;
12  
13  import java.io.BufferedInputStream;
14  import java.io.IOException;
15  import java.io.ObjectInputStream;
16  import java.net.Socket;
17  
18  import ch.qos.logback.access.spi.AccessContext;
19  import ch.qos.logback.access.spi.AccessEvent;
20  import ch.qos.logback.core.spi.FilterReply;
21  
22  // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
23  
24  /**
25   * Read {@link AccessEvent} objects sent from a remote client using Sockets
26   * (TCP). These logging events are logged according to local policy, as if they
27   * were generated locally.
28   * 
29   * <p>
30   * For example, the socket node might decide to log events to a local file and
31   * also resent them to a second socket node.
32   * 
33   * @author Ceki G&uuml;lc&uuml;
34   * @author S&eacute;bastien Pennec
35   * 
36   * @since 0.8.4
37   */
38  public class SocketNode implements Runnable {
39  
40    Socket socket;
41    AccessContext context;
42    ObjectInputStream ois;
43  
44    public SocketNode(Socket socket, AccessContext context) {
45      this.socket = socket;
46      this.context = context;
47      try {
48        ois = new ObjectInputStream(new BufferedInputStream(socket
49            .getInputStream()));
50      } catch (Exception e) {
51        System.out.println("Could not open ObjectInputStream to " + socket + e);
52      }
53    }
54  
55    public void run() {
56      AccessEvent event;
57  
58      try {
59        while (true) {
60          // read an event from the wire
61          event = (AccessEvent) ois.readObject();
62          //check that the event should be logged
63          if (context.getFilterChainDecision(event) == FilterReply.DENY) {
64            break;
65          }
66          //send it to the appenders
67          context.callAppenders(event); 
68        }
69      } catch (java.io.EOFException e) {
70        System.out.println("Caught java.io.EOFException closing connection.");
71      } catch (java.net.SocketException e) {
72        System.out.println("Caught java.net.SocketException closing connection.");
73      } catch (IOException e) {
74        System.out.println("Caught java.io.IOException: " + e);
75        System.out.println("Closing connection.");
76      } catch (Exception e) {
77        System.out.println("Unexpected exception. Closing connection." + e);
78      }
79  
80      try {
81        ois.close();
82      } catch (Exception e) {
83        System.out.println("Could not close connection." + e);
84      }
85    }
86  }