View Javadoc
1 package net.sf.panoptes.module.log4j; 2 3 import java.io.IOException; 4 import java.io.ObjectInputStream; 5 import java.net.InetAddress; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.apache.log4j.spi.LoggingEvent; 12 13 /*** 14 * SocketAppenderListener.java 15 * 16 * @author Dag Liodden 17 * @version 0.1 18 */ 19 public class SocketAppenderListener implements Runnable { 20 21 private int port; 22 private volatile boolean running = false; 23 private LoggerRepository repository; 24 private Log log = LogFactory.getLog(getClass()); 25 26 /*** 27 * Constructor for SocketAppenderListener. 28 */ 29 public SocketAppenderListener(int port, LoggerRepository repository) { 30 this.port = port; 31 this.repository = repository; 32 } 33 34 public void startListening() { 35 running = true; 36 (new Thread(this)).start(); 37 } 38 39 /*** 40 * @see java.lang.Runnable#run() 41 */ 42 public void run() { 43 ServerSocket serverSocket; 44 try { 45 serverSocket = new ServerSocket(port); 46 while (true) { 47 Socket sock = serverSocket.accept(); 48 log.info("New log source at " + sock.getInetAddress().toString()); 49 ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); 50 51 Thread t = new Worker(in, sock.getInetAddress()); 52 t.start(); 53 } 54 55 } catch (IOException e) { 56 log.error("Terminated", e); 57 } 58 59 } 60 61 class Worker extends Thread { 62 private volatile boolean running = true; 63 private ObjectInputStream in; 64 private InetAddress host; 65 66 Worker(ObjectInputStream in, InetAddress host) { 67 this.in = in; 68 this.host = host; 69 } 70 71 public void run() { 72 while (running) { 73 try { 74 Object o = in.readObject(); 75 log.debug("Added new log event from " + host); 76 repository.addEvent((LoggingEvent) o, host); 77 } catch (IOException e) { 78 // TODO Auto-generated catch block 79 break; 80 } catch (ClassNotFoundException e) { 81 break; 82 } 83 84 } 85 } 86 } 87 }

This page was automatically generated by Maven