001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the APACHE.txt file. */
007: package org.jahia.sqlprofiler.gui;
008:
009: import java.io.EOFException;
010: import java.io.IOException;
011: import java.io.ObjectInputStream;
012: import java.net.ServerSocket;
013: import java.net.Socket;
014: import java.net.SocketException;
015: import org.apache.log4j.Logger;
016: import org.apache.log4j.spi.LoggingEvent;
017: import javax.swing.event.EventListenerList;
018:
019: /**
020: * A daemon thread the processes connections from a
021: * <code>org.apache.log4j.net.SocketAppender.html</code>.
022: *
023: * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
024: */
025: class LoggingReceiver extends Thread {
026: /** used to log messages **/
027: private static final Logger LOG = Logger
028: .getLogger(LoggingReceiver.class);
029:
030: /**
031: * Helper that actually processes a client connection. It receives events
032: * and adds them to the supplied model.
033: *
034: * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
035: */
036: private class Slurper implements Runnable {
037: /** socket connection to read events from **/
038: private final Socket mClient;
039:
040: /**
041: * Creates a new <code>Slurper</code> instance.
042: *
043: * @param aClient socket to receive events from
044: */
045: Slurper(Socket aClient) {
046: mClient = aClient;
047: }
048:
049: /** loops getting the events **/
050: public void run() {
051: LOG.debug("Starting to get data");
052: fireLogReceptionConnected();
053: try {
054: final ObjectInputStream ois = new ObjectInputStream(
055: mClient.getInputStream());
056: while (true) {
057: final LoggingEvent event = (LoggingEvent) ois
058: .readObject();
059: fireLogReceptionDataReceived(new EventDetails(event));
060: }
061: } catch (EOFException e) {
062: LOG.info("Reached EOF, closing connection");
063: } catch (SocketException e) {
064: LOG.info("Caught SocketException, closing connection");
065: } catch (IOException e) {
066: LOG.warn("Got IOException, closing connection", e);
067: } catch (ClassNotFoundException e) {
068: LOG
069: .warn(
070: "Got ClassNotFoundException, closing connection",
071: e);
072: }
073:
074: try {
075: mClient.close();
076: } catch (IOException e) {
077: LOG.warn("Error closing connection", e);
078: }
079: fireLogReceptionDisconnected();
080: }
081: }
082:
083: /** server for listening for connections **/
084: private final ServerSocket mSvrSock;
085:
086: EventListenerList listenerList = new EventListenerList();
087: LogReceptionEvent defaultLogReceptionEvent = null;
088:
089: public void addLogReceptionListener(LogReceptionListener l) {
090: listenerList.add(LogReceptionListener.class, l);
091: }
092:
093: public void removeLogReceptionListener(LogReceptionListener l) {
094: listenerList.remove(LogReceptionListener.class, l);
095: }
096:
097: protected void fireLogReceptionConnected() {
098: // Guaranteed to return a non-null array
099: Object[] listeners = listenerList.getListenerList();
100: // Process the listeners last to first, notifying
101: // those that are interested in this event
102: for (int i = listeners.length - 2; i >= 0; i -= 2) {
103: if (listeners[i] == LogReceptionListener.class) {
104: if (defaultLogReceptionEvent == null) {
105: defaultLogReceptionEvent = new LogReceptionEvent(
106: this );
107: }
108: ((LogReceptionListener) listeners[i + 1])
109: .logReceptionConnected(defaultLogReceptionEvent);
110: }
111: }
112: }
113:
114: protected void fireLogReceptionDisconnected() {
115: // Guaranteed to return a non-null array
116: Object[] listeners = listenerList.getListenerList();
117: // Process the listeners last to first, notifying
118: // those that are interested in this event
119: for (int i = listeners.length - 2; i >= 0; i -= 2) {
120: if (listeners[i] == LogReceptionListener.class) {
121: if (defaultLogReceptionEvent == null) {
122: defaultLogReceptionEvent = new LogReceptionEvent(
123: this );
124: }
125: ((LogReceptionListener) listeners[i + 1])
126: .logReceptionDisconnected(defaultLogReceptionEvent);
127: }
128: }
129: }
130:
131: protected void fireLogReceptionDataReceived(
132: EventDetails eventDetails) {
133: // Guaranteed to return a non-null array
134: Object[] listeners = listenerList.getListenerList();
135: // Process the listeners last to first, notifying
136: // those that are interested in this event
137: for (int i = listeners.length - 2; i >= 0; i -= 2) {
138: if (listeners[i] == LogReceptionListener.class) {
139: ((LogReceptionListener) listeners[i + 1])
140: .logReceptionDataReceived(new LogReceptionEvent(
141: this , eventDetails));
142: }
143: }
144: }
145:
146: /**
147: * Creates a new <code>LoggingReceiver</code> instance.
148: *
149: * @param aModel model to place put received into
150: * @param aPort port to listen on
151: * @throws IOException if an error occurs
152: */
153: LoggingReceiver(LoggerTableModel aModel, int aPort)
154: throws IOException {
155: setDaemon(true);
156: mSvrSock = new ServerSocket(aPort);
157: }
158:
159: /** Listens for client connections **/
160: public void run() {
161: LOG.info("Thread started");
162: try {
163: while (true) {
164: LOG.debug("Waiting for a connection");
165: final Socket client = mSvrSock.accept();
166: LOG.debug("Got a connection from "
167: + client.getInetAddress().getHostName());
168: final Thread t = new Thread(new Slurper(client));
169: t.setDaemon(true);
170: t.start();
171: }
172: } catch (IOException e) {
173: LOG.error("Error in accepting connections, stopping.", e);
174: }
175: }
176: }
|