logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sde...@apache.org
Subject cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/net XMLSocketNode.java MulticastReceiver.java UDPReceiver.java
Date Mon, 05 May 2003 15:15:59 GMT
sdeboy      2003/05/05 08:15:59

  Modified:    src/java/org/apache/log4j/net XMLSocketNode.java
                        MulticastReceiver.java UDPReceiver.java
  Log:
  Corrected missing decoder implementation code in a receiver.
  
  Revision  Changes    Path
  1.2       +159 -145  jakarta-log4j-sandbox/src/java/org/apache/log4j/net/XMLSocketNode.java
  
  Index: XMLSocketNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/net/XMLSocketNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLSocketNode.java	5 May 2003 00:41:17 -0000	1.1
  +++ XMLSocketNode.java	5 May 2003 15:15:59 -0000	1.2
  @@ -49,21 +49,24 @@
   
   package org.apache.log4j.net;
   
  +import org.apache.log4j.*;
  +import org.apache.log4j.plugins.Receiver;
  +import org.apache.log4j.spi.*;
  +
  +import java.io.BufferedInputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.ObjectInputStream;
  +
   import java.net.InetAddress;
   import java.net.Socket;
  +
   import java.util.Iterator;
   import java.util.Vector;
  -import java.io.IOException;
  -import java.io.InputStream;
  -import java.io.ObjectInputStream;
  -import java.io.BufferedInputStream;
   
  -import org.apache.log4j.*;
  -import org.apache.log4j.spi.*;
  -import org.apache.log4j.plugins.Receiver;
   
   /**
  -   Read {@link LoggingEvent} objects sent from a remote client using XML over 
  +   Read {@link LoggingEvent} objects sent from a remote client using XML over
      Sockets (TCP). These logging events are logged according to local
      policy, as if they were generated locally.
   
  @@ -75,149 +78,160 @@
       @since 0.8.4
   */
   public class XMLSocketNode implements Runnable {
  -
  -	Socket socket;
  -	LoggerRepository hierarchy;
  -	Receiver receiver;
  -	Decoder decoder;
  -	SocketNodeEventListener listener;
  -
  -	static Logger logger = Logger.getLogger(SocketNode.class);
  +  static Logger logger = Logger.getLogger(SocketNode.class);
  +  Socket socket;
  +  LoggerRepository hierarchy;
  +  Receiver receiver;
  +  Decoder decoder;
  +  SocketNodeEventListener listener;
   
     /**
       Constructor for socket and logger repository. */
  -  public XMLSocketNode(String decoder, Socket socket, LoggerRepository hierarchy) {
  -		try {
  -			Class c = Class.forName(decoder);
  -			Object o = c.newInstance();
  -			if (o instanceof Decoder) {
  -				this.decoder = (Decoder) o;
  -			}
  -		} catch (ClassNotFoundException cnfe) {
  -		} catch (IllegalAccessException iae) {
  -		} catch (InstantiationException ie) {
  -		}
  +  public XMLSocketNode(
  +    String decoder, Socket socket, LoggerRepository hierarchy) {
  +    try {
  +      Class c = Class.forName(decoder);
  +      Object o = c.newInstance();
  +
  +      if (o instanceof Decoder) {
  +        this.decoder = (Decoder) o;
  +      }
  +    } catch (ClassNotFoundException cnfe) {
  +    } catch (IllegalAccessException iae) {
  +    } catch (InstantiationException ie) {
  +    }
   
       this.socket = socket;
       this.hierarchy = hierarchy;
     }
  -  
   
  -	/**
  -	  Constructor for socket and reciever. */
  -	public XMLSocketNode(String decoder, Socket socket, Receiver receiver) {
  -		try {
  -			Class c = Class.forName(decoder);
  -			Object o = c.newInstance();
  -			if (o instanceof Decoder) {
  -				this.decoder = (Decoder) o;
  -			}
  -		} catch (ClassNotFoundException cnfe) {
  -		} catch (IllegalAccessException iae) {
  -		} catch (InstantiationException ie) {
  -		}
  -
  -		this.socket = socket;
  -		this.receiver = receiver;
  -	}
  -
  -	/**
  -	  Set the event listener on this node. */
  -	public void setListener(SocketNodeEventListener _listener) {
  -		listener = _listener;
  -	}
  -
  -	public void run() {
  -		LoggingEvent event;
  -		Logger remoteLogger;
  -		Exception listenerException = null;
  -		InputStream bis = null;
  -
  -		if (this.receiver == null || this.decoder == null) {
  -			bis = null;
  -			listenerException =
  -				new Exception("No receiver or decoder provided.  Cannot process xml socket events");
  -			logger.error(
  -				"Exception constructing XML Socket Receiver",
  -				listenerException);
  -		}
  -
  -		try {
  -			bis = new BufferedInputStream(socket.getInputStream());
  -		} catch (Exception e) {
  -			bis = null;
  -			listenerException = e;
  -			logger.error("Exception opening ObjectInputStream to " + socket, e);
  -		}
  -
  -		if (bis != null) {
  -			String remoteInfo =
  -				socket.getInetAddress().getHostName() + ":" + socket.getPort();
  -			try {
  -				StringBuffer buf = new StringBuffer();
  -				while (true) {
  -					while (bis.available()>0) {
  -						byte[] b = new byte[1024];
  -						int readCount = bis.read(b);
  -						buf.append(new String(b));
  -					}
  -					if (buf.length()>0) {
  -					Vector v = decoder.decodeEvents(buf.toString());
  -					if (v != null) {
  -						Iterator iter = v.iterator();
  -						while (iter.hasNext()) {
  -							LoggingEvent e = (LoggingEvent) iter.next();
  -							// store the known remote info in an event property
  -							e.setProperty("log4j.remoteSourceInfo", remoteInfo);
  -							// if configured with a receiver, tell it to post the event
  -				          if (receiver != null) {
  -				            receiver.doPost(e);
  -				          // 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(e.getLoggerName());
  -				            //event.logger = remoteLogger;
  -				            // apply the logger-level filter
  -				            if(e.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
  -				              // finally log the event as if was generated locally
  -				              remoteLogger.callAppenders(e);
  -				            }
  -				          }
  -						}
  -					}
  -
  -					buf.setLength(0);
  -					}
  -				}
  -		} 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;
  -}
  -}
  -
  -// close the socket
  -try {
  -	if (bis != null) {
  -		bis.close();
  -	}
  -} catch (Exception e) {
  -	//logger.info("Could not close connection.", e);
  -}
  -
  -// send event to listener, if configured
  -if (listener != null) {
  -	listener.socketClosedEvent(listenerException);
  -}
  -}
  +  /**
  +    Constructor for socket and reciever. */
  +  public XMLSocketNode(String decoder, Socket socket, Receiver receiver) {
  +    try {
  +      Class c = Class.forName(decoder);
  +      Object o = c.newInstance();
  +
  +      if (o instanceof Decoder) {
  +        this.decoder = (Decoder) o;
  +      }
  +    } catch (ClassNotFoundException cnfe) {
  +    } catch (IllegalAccessException iae) {
  +    } catch (InstantiationException ie) {
  +    }
  +
  +    this.socket = socket;
  +    this.receiver = receiver;
  +  }
  +
  +  /**
  +    Set the event listener on this node. */
  +  public void setListener(SocketNodeEventListener _listener) {
  +    listener = _listener;
  +  }
  +
  +  public void run() {
  +    LoggingEvent event;
  +    Logger remoteLogger;
  +    Exception listenerException = null;
  +    InputStream bis = null;
  +
  +    if ((this.receiver == null) || (this.decoder == null)) {
  +      bis = null;
  +      listenerException =
  +        new Exception(
  +          "No receiver or decoder provided.  Cannot process xml socket events");
  +      logger.error(
  +        "Exception constructing XML Socket Receiver", listenerException);
  +    }
  +
  +    try {
  +      bis = new BufferedInputStream(socket.getInputStream());
  +    } catch (Exception e) {
  +      bis = null;
  +      listenerException = e;
  +      logger.error("Exception opening ObjectInputStream to " + socket, e);
  +    }
  +
  +    if (bis != null) {
  +      String remoteInfo =
  +        socket.getInetAddress().getHostName() + ":" + socket.getPort();
  +
  +      try {
  +        StringBuffer buf = new StringBuffer();
  +
  +        while (true) {
  +          while (bis.available() > 0) {
  +            byte[] b = new byte[1024];
  +            int readCount = bis.read(b);
  +            buf.append(new String(b));
  +          }
  +
  +          if (buf.length() > 0) {
  +            Vector v = decoder.decodeEvents(buf.toString());
  +
  +            if (v != null) {
  +              Iterator iter = v.iterator();
  +
  +              while (iter.hasNext()) {
  +                LoggingEvent e = (LoggingEvent) iter.next();
  +
  +                // store the known remote info in an event property
  +                e.setProperty("log4j.remoteSourceInfo", remoteInfo);
  +
  +                // if configured with a receiver, tell it to post the event
  +                if (receiver != null) {
  +                  receiver.doPost(e);
  +
  +                  // 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(e.getLoggerName());
  +
  +                  //event.logger = remoteLogger;
  +                  // apply the logger-level filter
  +                  if (
  +                    e.getLevel().isGreaterOrEqual(
  +                        remoteLogger.getEffectiveLevel())) {
  +                    // finally log the event as if was generated locally
  +                    remoteLogger.callAppenders(e);
  +                  }
  +                }
  +              }
  +            }
  +
  +            buf.setLength(0);
  +          }
  +        }
  +      } 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;
  +      }
  +    }
  +
  +    // close the socket
  +    try {
  +      if (bis != null) {
  +        bis.close();
  +      }
  +    } catch (Exception e) {
  +      //logger.info("Could not close connection.", e);
  +    }
  +
  +    // send event to listener, if configured
  +    if (listener != null) {
  +      listener.socketClosedEvent(listenerException);
  +    }
  +  }
   }
  
  
  
  1.3       +16 -3     jakarta-log4j-sandbox/src/java/org/apache/log4j/net/MulticastReceiver.java
  
  Index: MulticastReceiver.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/net/MulticastReceiver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MulticastReceiver.java	5 May 2003 00:41:17 -0000	1.2
  +++ MulticastReceiver.java	5 May 2003 15:15:59 -0000	1.3
  @@ -67,8 +67,8 @@
   
   
   /**
  - *  Multicast-based receiver.  Accepts LoggingEvents encoded using 
  - *  MulticastAppender and XMLLayout. The the XML data is converted  
  + *  Multicast-based receiver.  Accepts LoggingEvents encoded using
  + *  MulticastAppender and XMLLayout. The the XML data is converted
    *  back to a LoggingEvent and is posted.
    *
    *  @author Scott Deboy <sdeboy@apache.org>
  @@ -80,8 +80,9 @@
     private int port;
     private String address;
     private MulticastSocket socket = null;
  +
     //default to log4j xml decoder
  -  private String decoder="org.apache.log4j.xml.XMLDecoder";
  +  private String decoder = "org.apache.log4j.xml.XMLDecoder";
     private Decoder decoderImpl;
     private final UDPThread receiverThread = new UDPThread();
     private final HandlerThread handlerThread = new HandlerThread();
  @@ -128,6 +129,18 @@
   
     public void activateOptions() {
       InetAddress addr = null;
  +
  +    try {
  +      Class c = Class.forName(decoder);
  +      Object o = c.newInstance();
  +
  +      if (o instanceof Decoder) {
  +        this.decoderImpl = (Decoder) o;
  +      }
  +    } catch (ClassNotFoundException cnfe) {
  +    } catch (IllegalAccessException iae) {
  +    } catch (InstantiationException ie) {
  +    }
   
       try {
         addr = InetAddress.getByName(address);
  
  
  
  1.4       +13 -11    jakarta-log4j-sandbox/src/java/org/apache/log4j/net/UDPReceiver.java
  
  Index: UDPReceiver.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/net/UDPReceiver.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- UDPReceiver.java	5 May 2003 00:41:17 -0000	1.3
  +++ UDPReceiver.java	5 May 2003 15:15:59 -0000	1.4
  @@ -73,8 +73,9 @@
    */
   public class UDPReceiver extends Receiver {
     private static final int PACKET_LENGTH = 8192;
  +
     //default to log4j xml decoder
  -  private String decoder="org.apache.log4j.xml.XMLDecoder";
  +  private String decoder = "org.apache.log4j.xml.XMLDecoder";
     private Decoder decoderImpl;
     private boolean isActive = false;
     private int port;
  @@ -116,17 +117,18 @@
   
     public void activateOptions() {
       try {
  -		try {
  -			Class c = Class.forName(decoder);
  -			Object o = c.newInstance();
  -			if (o instanceof Decoder) {
  -				this.decoderImpl = (Decoder) o;
  -			}
  -		} catch (ClassNotFoundException cnfe) {
  -		} catch (IllegalAccessException iae) {
  -		} catch (InstantiationException ie) {
  -		}
  +      Class c = Class.forName(decoder);
  +      Object o = c.newInstance();
  +
  +      if (o instanceof Decoder) {
  +        this.decoderImpl = (Decoder) o;
  +      }
  +    } catch (ClassNotFoundException cnfe) {
  +    } catch (IllegalAccessException iae) {
  +    } catch (InstantiationException ie) {
  +    }
   
  +    try {
         socket = new DatagramSocket(port);
         receiverThread.start();
         handlerThread.start();
  
  
  

---------------------------------------------------------------------
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