View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j.chainsaw;
    18
    19  import java.io.EOFException;
    20  import java.io.IOException;
    21  import java.io.ObjectInputStream;
    22  import java.net.ServerSocket;
    23  import java.net.Socket;
    24  import java.net.SocketException;
    25  import org.apache.log4j.Logger;
    26  import org.apache.log4j.spi.LoggingEvent;
    27
    28  /***
    29   * A daemon thread the processes connections from a
    30   * <code>org.apache.log4j.net.SocketAppender.html</code>.
    31   *
    32   * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
    33   */
    34  class LoggingReceiver extends Thread {
    35      /*** used to log messages **/
    36      private static final Logger LOG = Logger.getLogger(LoggingReceiver.class);
    37
    38      /***
    39       * Helper that actually processes a client connection. It receives events
    40       * and adds them to the supplied model.
    41       *
    42       * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
    43       */
    44      private class Slurper implements Runnable {
    45          /*** socket connection to read events from **/
    46          private final Socket mClient;
    47
    48          /***
    49           * Creates a new <code>Slurper</code> instance.
    50           *
    51           * @param aClient socket to receive events from
    52           */
    53          Slurper(Socket aClient) {
    54              mClient = aClient;
    55          }
    56
    57          /*** loops getting the events **/
    58          public void run() {
    59              LOG.debug("Starting to get data");
    60              try {
    61                  final ObjectInputStream ois =
    62                      new ObjectInputStream(mClient.getInputStream());
    63                  while (true) {
    64                      final LoggingEvent event = (LoggingEvent) ois.readObject();
    65                      mModel.addEvent(new EventDetails(event));
    66                  }
    67              } catch (EOFException e) {
    68                  LOG.info("Reached EOF, closing connection");
    69              } catch (SocketException e) {
    70                  LOG.info("Caught SocketException, closing connection");
    71              } catch (IOException e) {
    72                  LOG.warn("Got IOException, closing connection", e);
    73              } catch (ClassNotFoundException e) {
    74                  LOG.warn("Got ClassNotFoundException, closing connection", e);
    75              }
    76
    77              try {
    78                  mClient.close();
    79              } catch (IOException e) {
    80                  LOG.warn("Error closing connection", e);
    81              }
    82          }
    83      }
    84
    85      /*** where to put the events **/
    86      private MyTableModel mModel;
    87
    88      /*** server for listening for connections **/
    89      private ServerSocket mSvrSock;
    90
    91      /***
    92       * Creates a new <code>LoggingReceiver</code> instance.
    93       *
    94       * @param aModel model to place put received into
    95       * @param aPort port to listen on
    96       * @throws IOException if an error occurs
    97       */
    98      LoggingReceiver(MyTableModel aModel, int aPort) throws IOException {
    99          setDaemon(true);
    100         mModel = aModel;
    101         mSvrSock = new ServerSocket(aPort);
    102     }
    103
    104     /*** Listens for client connections **/
    105     public void run() {
    106         LOG.info("Thread started");
    107         try {
    108             while (true) {
    109                 LOG.debug("Waiting for a connection");
    110                 final Socket client = mSvrSock.accept();
    111                 LOG.debug("Got a connection from " +
    112                           client.getInetAddress().getHostName());
    113                 final Thread t = new Thread(new Slurper(client));
    114                 t.setDaemon(true);
    115                 t.start();
    116             }
    117         } catch (IOException e) {
    118             LOG.error("Error in accepting connections, stopping.", e);
    119         }
    120     }
    121 }