KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > LoggingReceiver


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the APACHE.txt file. */

7 package org.jahia.sqlprofiler.gui;
8
9 import java.io.EOFException JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.net.ServerSocket JavaDoc;
13 import java.net.Socket JavaDoc;
14 import java.net.SocketException JavaDoc;
15 import org.apache.log4j.Logger;
16 import org.apache.log4j.spi.LoggingEvent;
17 import javax.swing.event.EventListenerList JavaDoc;
18
19 /**
20  * A daemon thread the processes connections from a
21  * <code>org.apache.log4j.net.SocketAppender.html</code>.
22  *
23  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
24  */

25 class LoggingReceiver extends Thread JavaDoc {
26     /** used to log messages **/
27     private static final Logger LOG = Logger.getLogger(LoggingReceiver.class);
28
29     /**
30      * Helper that actually processes a client connection. It receives events
31      * and adds them to the supplied model.
32      *
33      * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
34      */

35     private class Slurper
36         implements Runnable JavaDoc {
37         /** socket connection to read events from **/
38         private final Socket JavaDoc mClient;
39
40         /**
41          * Creates a new <code>Slurper</code> instance.
42          *
43          * @param aClient socket to receive events from
44          */

45         Slurper(Socket JavaDoc aClient) {
46             mClient = aClient;
47         }
48
49         /** loops getting the events **/
50         public void run() {
51             LOG.debug("Starting to get data");
52             fireLogReceptionConnected();
53             try {
54                 final ObjectInputStream JavaDoc ois =
55                     new ObjectInputStream JavaDoc(mClient.getInputStream());
56                 while (true) {
57                     final LoggingEvent event = (LoggingEvent) ois.readObject();
58                     fireLogReceptionDataReceived(new EventDetails(event));
59                 }
60             } catch (EOFException JavaDoc e) {
61                 LOG.info("Reached EOF, closing connection");
62             } catch (SocketException JavaDoc e) {
63                 LOG.info("Caught SocketException, closing connection");
64             } catch (IOException JavaDoc e) {
65                 LOG.warn("Got IOException, closing connection", e);
66             } catch (ClassNotFoundException JavaDoc e) {
67                 LOG.warn("Got ClassNotFoundException, closing connection", e);
68             }
69
70             try {
71                 mClient.close();
72             } catch (IOException JavaDoc e) {
73                 LOG.warn("Error closing connection", e);
74             }
75             fireLogReceptionDisconnected();
76         }
77     }
78
79     /** server for listening for connections **/
80     private final ServerSocket JavaDoc mSvrSock;
81
82     EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
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         // Guaranteed to return a non-null array
95
Object JavaDoc[] listeners = listenerList.getListenerList();
96         // Process the listeners last to first, notifying
97
// those that are interested in this event
98
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         // Guaranteed to return a non-null array
111
Object JavaDoc[] listeners = listenerList.getListenerList();
112         // Process the listeners last to first, notifying
113
// those that are interested in this event
114
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         // Guaranteed to return a non-null array
127
Object JavaDoc[] listeners = listenerList.getListenerList();
128         // Process the listeners last to first, notifying
129
// those that are interested in this event
130
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     /**
140      * Creates a new <code>LoggingReceiver</code> instance.
141      *
142      * @param aModel model to place put received into
143      * @param aPort port to listen on
144      * @throws IOException if an error occurs
145      */

146     LoggingReceiver(LoggerTableModel aModel, int aPort) throws IOException JavaDoc {
147         setDaemon(true);
148         mSvrSock = new ServerSocket JavaDoc(aPort);
149     }
150
151     /** Listens for client connections **/
152     public void run() {
153         LOG.info("Thread started");
154         try {
155             while (true) {
156                 LOG.debug("Waiting for a connection");
157                 final Socket JavaDoc client = mSvrSock.accept();
158                 LOG.debug("Got a connection from " +
159                           client.getInetAddress().getHostName());
160                 final Thread JavaDoc t = new Thread JavaDoc(new Slurper(client));
161                 t.setDaemon(true);
162                 t.start();
163             }
164         } catch (IOException JavaDoc e) {
165             LOG.error("Error in accepting connections, stopping.", e);
166         }
167     }
168 }
Popular Tags