logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwom...@apache.org
Subject cvs commit: jakarta-log4j/src/java/org/apache/log4j/net SocketNodeEventListener.java SocketNode.java
Date Sat, 08 Feb 2003 07:16:28 GMT
mwomack     2003/02/07 23:16:27

  Modified:    src/java/org/apache/log4j/net SocketNode.java
  Added:       src/java/org/apache/log4j/net SocketNodeEventListener.java
  Log:
  Added SocketNodeEventListener.  This listener can be used by the SocketNode creator to learn
when the node closes the associated socket.
  Factored some of the code in SocketNode.
  
  Revision  Changes    Path
  1.24      +65 -50    jakarta-log4j/src/java/org/apache/log4j/net/SocketNode.java
  
  Index: SocketNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/net/SocketNode.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- SocketNode.java	11 Dec 2002 07:30:31 -0000	1.23
  +++ SocketNode.java	8 Feb 2003 07:16:27 -0000	1.24
  @@ -36,81 +36,96 @@
     Socket socket;
     LoggerRepository hierarchy;
     Receiver receiver;
  -  ObjectInputStream ois;
  +  SocketNodeEventListener listener;
   
     static Logger logger = Logger.getLogger(SocketNode.class);
   
  +  /**
  +    Constructor for socket and logger repository. */
     public SocketNode(Socket socket, LoggerRepository hierarchy) {
       this.socket = socket;
       this.hierarchy = hierarchy;
  -    try {
  -      ois = new ObjectInputStream(
  -                         new BufferedInputStream(socket.getInputStream()));
  -    }
  -    catch(Exception e) {
  -      logger.error("Could not open ObjectInputStream to "+socket, e);
  -    }
     }
     
  +  /**
  +    Constructor for socket and reciever. */
     public SocketNode(Socket socket, Receiver receiver) {
       this.socket = socket;
       this.receiver = receiver;
  -    try {
  -      ois = new ObjectInputStream(
  -                         new BufferedInputStream(socket.getInputStream()));
  -    }
  -    catch(Exception e) {
  -      logger.error("Could not open ObjectInputStream to "+socket, e);
  -    }
     }
   
  -
  -  //public
  -  //void finalize() {
  -  //System.err.println("-------------------------Finalize called");
  -  // System.err.flush();
  -  //}
  +  /**
  +    Set the event listener on this node. */
  +  public void setListener(SocketNodeEventListener _listener) {
  +    listener = _listener;
  +  }
  +  
   
     public void run() {
       LoggingEvent event;
       Logger remoteLogger;
  -
  +    Exception listenerException = null;
  +    ObjectInputStream ois = null;
  +    
       try {
  -      while(true) {
  -	// read an event from the wire
  -      	event = (LoggingEvent) ois.readObject();
  -      	
  -      	// if configured with a receiver, tell it to post the event
  -        if (receiver != null) {
  -          receiver.doPost(event);
  -        // else post it via the hierarchy
  -        } else {
  -	  // get a logger from the hierarchy. The name of the logger
  -	  // is taken to be the name contained in the event.
  -          remoteLogger = hierarchy.getLogger(event.categoryName);
  -          //event.logger = remoteLogger;
  -	  // apply the logger-level filter
  -          if(event.level.isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
  -	    // finally log the event as if was generated locally
  -            remoteLogger.callAppenders(event);
  +      ois = new ObjectInputStream(
  +        new BufferedInputStream(socket.getInputStream()));
  +    } catch (Exception e) {
  +      ois = null;
  +      listenerException = e;
  +      logger.error("Exception opening ObjectInputStream to " + socket, e);
  +    }
  +
  +    if (ois != null) {
  +      try {
  +        while(true) {
  +          // read an event from the wire
  +        	event = (LoggingEvent) ois.readObject();
  +        	
  +        	// if configured with a receiver, tell it to post the event
  +          if (receiver != null) {
  +            receiver.doPost(event);
  +          // else post it via the hierarchy
  +          } else {
  +            // get a logger from the hierarchy. The name of the logger
  +            // is taken to be the name contained in the event.
  +            remoteLogger = hierarchy.getLogger(event.categoryName);
  +            //event.logger = remoteLogger;
  +            // apply the logger-level filter
  +            if(event.level.isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
  +              // finally log the event as if was generated locally
  +              remoteLogger.callAppenders(event);
  +            }
             }
           }
  +      } catch(java.io.EOFException e) {
  +        logger.info("Caught java.io.EOFException closing conneciton.");
  +        listenerException = e;
  +      } catch(java.net.SocketException e) {
  +        logger.info("Caught java.net.SocketException closing conneciton.");
  +        listenerException = e;
  +      } catch(IOException e) {
  +        logger.info("Caught java.io.IOException: "+e);
  +        logger.info("Closing connection.");
  +        listenerException = e;
  +      } catch(Exception e) {
  +        logger.error("Unexpected exception. Closing connecition.", e);
  +        listenerException = e;
         }
  -    } catch(java.io.EOFException e) {
  -      logger.info("Caught java.io.EOFException closing conneciton.");
  -    } catch(java.net.SocketException e) {
  -      logger.info("Caught java.net.SocketException closing conneciton.");
  -    } catch(IOException e) {
  -      logger.info("Caught java.io.IOException: "+e);
  -      logger.info("Closing connection.");
  -    } catch(Exception e) {
  -      logger.error("Unexpected exception. Closing conneciton.", e);
       }
   
  +    // close the socket
       try {
  -      ois.close();
  +      if (ois != null) {
  +        ois.close();
  +      }
       } catch(Exception e) {
  -      logger.info("Could not close connection.", e);
  +      //logger.info("Could not close connection.", e);
  +    }
  +    
  +    // send event to listener, if configured
  +    if (listener != null) {
  +      listener.socketClosedEvent(listenerException);
       }
     }
   }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/net/SocketNodeEventListener.java
  
  Index: SocketNodeEventListener.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software
   * License version 1.1, a copy of which has been included with this
   * distribution in the LICENSE.txt file.  */
  
  package org.apache.log4j.net;
  
  /**
    Interface used to listen for {@link SocketNode} related
    events. Clients register an instance of the interface and the
    instance is called back when the various events occur.
    
    @author Mark Womack
    @since 1.3
  */
  public interface SocketNodeEventListener {
    
    /**
      Called when the socket the node was given has been closed. */
    public void socketClosedEvent(Exception e);
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message