1 16 package org.apache.log4j.chainsaw; 17 18 import java.io.EOFException ; 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.net.ServerSocket ; 22 import java.net.Socket ; 23 import java.net.SocketException ; 24 import org.apache.log4j.Logger; 25 import org.apache.log4j.spi.LoggingEvent; 26 27 33 class LoggingReceiver extends Thread { 34 35 private static final Logger LOG = Logger.getLogger(LoggingReceiver.class); 36 37 43 private class Slurper implements Runnable { 44 45 private final Socket mClient; 46 47 52 Slurper(Socket aClient) { 53 mClient = aClient; 54 } 55 56 57 public void run() { 58 LOG.debug("Starting to get data"); 59 try { 60 final ObjectInputStream ois = 61 new ObjectInputStream (mClient.getInputStream()); 62 while (true) { 63 final LoggingEvent event = (LoggingEvent) ois.readObject(); 64 mModel.addEvent(new EventDetails(event)); 65 } 66 } catch (EOFException e) { 67 LOG.info("Reached EOF, closing connection"); 68 } catch (SocketException e) { 69 LOG.info("Caught SocketException, closing connection"); 70 } catch (IOException e) { 71 LOG.warn("Got IOException, closing connection", e); 72 } catch (ClassNotFoundException e) { 73 LOG.warn("Got ClassNotFoundException, closing connection", e); 74 } 75 76 try { 77 mClient.close(); 78 } catch (IOException e) { 79 LOG.warn("Error closing connection", e); 80 } 81 } 82 } 83 84 85 private final MyTableModel mModel; 86 87 88 private final ServerSocket mSvrSock; 89 90 97 LoggingReceiver(MyTableModel aModel, int aPort) throws IOException { 98 setDaemon(true); 99 mModel = aModel; 100 mSvrSock = new ServerSocket (aPort); 101 } 102 103 104 public void run() { 105 LOG.info("Thread started"); 106 try { 107 while (true) { 108 LOG.debug("Waiting for a connection"); 109 final Socket client = mSvrSock.accept(); 110 LOG.debug("Got a connection from " + 111 client.getInetAddress().getHostName()); 112 final Thread t = new Thread (new Slurper(client)); 113 t.setDaemon(true); 114 t.start(); 115 } 116 } catch (IOException e) { 117 LOG.error("Error in accepting connections, stopping.", e); 118 } 119 } 120 } 121 | Popular Tags |