KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > LoggingReceiver


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.chainsaw;
17
18 import java.io.EOFException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import java.net.Socket JavaDoc;
23 import java.net.SocketException JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.spi.LoggingEvent;
26
27 /**
28  * A daemon thread the processes connections from a
29  * <code>org.apache.log4j.net.SocketAppender.html</code>.
30  *
31  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
32  */

33 class LoggingReceiver extends Thread JavaDoc {
34     /** used to log messages **/
35     private static final Logger LOG = Logger.getLogger(LoggingReceiver.class);
36
37     /**
38      * Helper that actually processes a client connection. It receives events
39      * and adds them to the supplied model.
40      *
41      * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
42      */

43     private class Slurper implements Runnable JavaDoc {
44         /** socket connection to read events from **/
45         private final Socket JavaDoc mClient;
46
47         /**
48          * Creates a new <code>Slurper</code> instance.
49          *
50          * @param aClient socket to receive events from
51          */

52         Slurper(Socket JavaDoc aClient) {
53             mClient = aClient;
54         }
55
56         /** loops getting the events **/
57         public void run() {
58             LOG.debug("Starting to get data");
59             try {
60                 final ObjectInputStream JavaDoc ois =
61                     new ObjectInputStream JavaDoc(mClient.getInputStream());
62                 while (true) {
63                     final LoggingEvent event = (LoggingEvent) ois.readObject();
64                     mModel.addEvent(new EventDetails(event));
65                 }
66             } catch (EOFException JavaDoc e) {
67                 LOG.info("Reached EOF, closing connection");
68             } catch (SocketException JavaDoc e) {
69                 LOG.info("Caught SocketException, closing connection");
70             } catch (IOException JavaDoc e) {
71                 LOG.warn("Got IOException, closing connection", e);
72             } catch (ClassNotFoundException JavaDoc e) {
73                 LOG.warn("Got ClassNotFoundException, closing connection", e);
74             }
75
76             try {
77                 mClient.close();
78             } catch (IOException JavaDoc e) {
79                 LOG.warn("Error closing connection", e);
80             }
81         }
82     }
83
84     /** where to put the events **/
85     private final MyTableModel mModel;
86
87     /** server for listening for connections **/
88     private final ServerSocket JavaDoc mSvrSock;
89
90     /**
91      * Creates a new <code>LoggingReceiver</code> instance.
92      *
93      * @param aModel model to place put received into
94      * @param aPort port to listen on
95      * @throws IOException if an error occurs
96      */

97     LoggingReceiver(MyTableModel aModel, int aPort) throws IOException JavaDoc {
98         setDaemon(true);
99         mModel = aModel;
100         mSvrSock = new ServerSocket JavaDoc(aPort);
101     }
102
103     /** Listens for client connections **/
104     public void run() {
105         LOG.info("Thread started");
106         try {
107             while (true) {
108                 LOG.debug("Waiting for a connection");
109                 final Socket JavaDoc client = mSvrSock.accept();
110                 LOG.debug("Got a connection from " +
111                           client.getInetAddress().getHostName());
112                 final Thread JavaDoc t = new Thread JavaDoc(new Slurper(client));
113                 t.setDaemon(true);
114                 t.start();
115             }
116         } catch (IOException JavaDoc e) {
117             LOG.error("Error in accepting connections, stopping.", e);
118         }
119     }
120 }
121
Popular Tags