1 7 package org.jahia.sqlprofiler.gui; 8 9 import java.io.EOFException ; 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.net.ServerSocket ; 13 import java.net.Socket ; 14 import java.net.SocketException ; 15 import org.apache.log4j.Logger; 16 import org.apache.log4j.spi.LoggingEvent; 17 import javax.swing.event.EventListenerList ; 18 19 25 class LoggingReceiver extends Thread { 26 27 private static final Logger LOG = Logger.getLogger(LoggingReceiver.class); 28 29 35 private class Slurper 36 implements Runnable { 37 38 private final Socket mClient; 39 40 45 Slurper(Socket aClient) { 46 mClient = aClient; 47 } 48 49 50 public void run() { 51 LOG.debug("Starting to get data"); 52 fireLogReceptionConnected(); 53 try { 54 final ObjectInputStream ois = 55 new ObjectInputStream (mClient.getInputStream()); 56 while (true) { 57 final LoggingEvent event = (LoggingEvent) ois.readObject(); 58 fireLogReceptionDataReceived(new EventDetails(event)); 59 } 60 } catch (EOFException e) { 61 LOG.info("Reached EOF, closing connection"); 62 } catch (SocketException e) { 63 LOG.info("Caught SocketException, closing connection"); 64 } catch (IOException e) { 65 LOG.warn("Got IOException, closing connection", e); 66 } catch (ClassNotFoundException e) { 67 LOG.warn("Got ClassNotFoundException, closing connection", e); 68 } 69 70 try { 71 mClient.close(); 72 } catch (IOException e) { 73 LOG.warn("Error closing connection", e); 74 } 75 fireLogReceptionDisconnected(); 76 } 77 } 78 79 80 private final ServerSocket mSvrSock; 81 82 EventListenerList listenerList = new EventListenerList (); 83 LogReceptionEvent defaultLogReceptionEvent = null; 84 85 public void addLogReceptionListener(LogReceptionListener l) { 86 listenerList.add(LogReceptionListener.class, l); 87 } 88 89 public void removeLogReceptionListener(LogReceptionListener l) { 90 listenerList.remove(LogReceptionListener.class, l); 91 } 92 93 protected void fireLogReceptionConnected() { 94 Object [] listeners = listenerList.getListenerList(); 96 for (int i = listeners.length - 2; i >= 0; i -= 2) { 99 if (listeners[i] == LogReceptionListener.class) { 100 if (defaultLogReceptionEvent == null) { 101 defaultLogReceptionEvent = new LogReceptionEvent(this); 102 } 103 ( (LogReceptionListener) listeners[i + 1]). 104 logReceptionConnected(defaultLogReceptionEvent); 105 } 106 } 107 } 108 109 protected void fireLogReceptionDisconnected() { 110 Object [] listeners = listenerList.getListenerList(); 112 for (int i = listeners.length - 2; i >= 0; i -= 2) { 115 if (listeners[i] == LogReceptionListener.class) { 116 if (defaultLogReceptionEvent == null) { 117 defaultLogReceptionEvent = new LogReceptionEvent(this); 118 } 119 ( (LogReceptionListener) listeners[i + 1]). 120 logReceptionDisconnected(defaultLogReceptionEvent); 121 } 122 } 123 } 124 125 protected void fireLogReceptionDataReceived(EventDetails eventDetails) { 126 Object [] listeners = listenerList.getListenerList(); 128 for (int i = listeners.length - 2; i >= 0; i -= 2) { 131 if (listeners[i] == LogReceptionListener.class) { 132 ( (LogReceptionListener) listeners[i + 1]). 133 logReceptionDataReceived(new LogReceptionEvent(this, 134 eventDetails)); 135 } 136 } 137 } 138 139 146 LoggingReceiver(LoggerTableModel aModel, int aPort) throws IOException { 147 setDaemon(true); 148 mSvrSock = new ServerSocket (aPort); 149 } 150 151 152 public void run() { 153 LOG.info("Thread started"); 154 try { 155 while (true) { 156 LOG.debug("Waiting for a connection"); 157 final Socket client = mSvrSock.accept(); 158 LOG.debug("Got a connection from " + 159 client.getInetAddress().getHostName()); 160 final Thread t = new Thread (new Slurper(client)); 161 t.setDaemon(true); 162 t.start(); 163 } 164 } catch (IOException e) { 165 LOG.error("Error in accepting connections, stopping.", e); 166 } 167 } 168 } | Popular Tags |