logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sde...@apache.org
Subject svn commit: r924176 - in /logging/log4j: companions/receivers/trunk/ companions/receivers/trunk/src/main/java/org/apache/log4j/net/ companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ companions/zeroconf/trunk/src/test/java/org/apache/log4j/n...
Date Wed, 17 Mar 2010 07:16:07 GMT
Author: sdeboy
Date: Wed Mar 17 07:16:06 2010
New Revision: 924176

URL: http://svn.apache.org/viewvc?rev=924176&view=rev
Log:
Added ZeroConf support to most of the network-based appenders and receivers.

To enable ZeroConf advertising of an appender or receiver, a user can now:
 - add jmdns jar to their classpath 
 - set the 'advertiseViaMulticastDNS' param to 'true'

Also added support for discovery of appenders in Chainsaw (Chainsaw will automatically configure
a matching receiver when connected).
Supported appenders:
    SocketAppender
    SocketHubAppender (ZeroConfSocketHubAppender is still functional)
    UDPAppender
    MulticastAppender
Supported receivers:
    SocketReceiver
    SocketHubReceiver (also receives events from ZeroConfSocketHubAppender)
    XMLSocketReceiver (can receive events sent over TCP by other logging frameworks)
    UDPReceiver
    MulticastReceiver

Implementation details:
 - removed Zeroconf4log4j class, jmdns access is now through the ZeroConfSupport class (class
provides support of JmDNS and ServiceInfo creation via reflection, and supports both jmdns
1.0 and jmdns 3.1 apis)
 - ZeroConfSupport class is now used by ZeroConfSocketHubAppender, Chainsaw and all appenders
& receivers that can advertise their configuration via ZeroConf
 - added new 'advertiseViaMulticastDNS' param to the appenders and receivers that support
ZeroConf
 - updated the ZeroConf site documentation
 - updated release notes
 - updated the ZeroConfPlugin html file
 - tested appenders with 1.0 and 3.1 jmdns jars
 - updated log4j references in poms to log4j 1.2.16-snapshot where necessary 

For those wanting to add ZeroConf support to third-party appenders and have Chainsaw discover
the appenders, the service info naming convention is described here: https://issues.apache.org/bugzilla/show_bug.cgi?id=48907

Added:
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/ZeroConfSupport.java
Removed:
    logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/Zeroconf4log4j.java
Modified:
    logging/log4j/companions/receivers/trunk/pom.xml
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastAppender.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiver.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiverBeanInfo.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketHubReceiver.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketReceiver.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPAppender.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPReceiver.java
    logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/XMLSocketReceiver.java
    logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ZeroConfSocketHubAppender.java
    logging/log4j/companions/zeroconf/trunk/src/test/java/org/apache/log4j/net/ZeroConfSocketHubAppenderTest.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketAppender.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketHubAppender.java

Modified: logging/log4j/companions/receivers/trunk/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/pom.xml?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/pom.xml (original)
+++ logging/log4j/companions/receivers/trunk/pom.xml Wed Mar 17 07:16:06 2010
@@ -217,7 +217,7 @@
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
-      <version>1.2.15</version>
+      <version>1.2.16-SNAPSHOT</version>
       <exclusions>
         <exclusion>
           <groupId>javax.jms</groupId>

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastAppender.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastAppender.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastAppender.java
Wed Mar 17 07:16:06 2010
@@ -22,11 +22,15 @@ import java.net.DatagramPacket;
 import java.net.InetAddress;
 import java.net.MulticastSocket;
 import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
 
 import org.apache.log4j.AppenderSkeleton;
 import org.apache.log4j.helpers.Constants;
 import org.apache.log4j.spi.LoggingEvent;
 import org.apache.log4j.helpers.LogLog;
+import org.apache.log4j.xml.XMLLayout;
 
 
 /**
@@ -54,7 +58,13 @@ public class MulticastAppender extends A
      The default port number for the multicast packets. (9991).
   */
   static final int DEFAULT_PORT = 9991;
-  
+
+  /**
+   * The MulticastDNS zone advertised by a MulticastAppender
+   * the MulticastAppender also adds a 'multicastAddress' property with the multicast address
value as a string
+   */
+  public static final String ZONE = "_log4j_xml_mcast_appender.local.";
+
   /**
      We remember host name as String in addition to the resolved
      InetAddress so that it can be returned via getOption().
@@ -69,7 +79,9 @@ public class MulticastAppender extends A
   private String encoding;
 
   private boolean locationInfo = false;
-  
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
   public MulticastAppender() {
      super(false);
   }
@@ -100,11 +112,21 @@ public class MulticastAppender extends A
     if(remoteHost != null) {
       address = getAddressByName(remoteHost);
     } else {
-      String err = "The RemoteHost property is required for SocketAppender named "+ name;
+      String err = "The RemoteHost property is required for MulticastAppender named "+ name;
       LogLog.error(err);
       throw new IllegalStateException(err);
     }
-    
+
+    if (layout == null) {
+        layout = new XMLLayout();
+    }
+      
+    if (advertiseViaMulticastDNS) {
+        Map properties = new HashMap();
+        properties.put("multicastAddress", remoteHost);
+        zeroConf = new ZeroConfSupport(ZONE, port, getName(), properties);
+        zeroConf.advertise();
+    }
     connect();
     super.activateOptions();
   }
@@ -120,6 +142,9 @@ public class MulticastAppender extends A
     }
 
     this.closed = true;
+    if (advertiseViaMulticastDNS) {
+        zeroConf.unadvertise();
+    }
     cleanUp();
   }
 
@@ -310,4 +335,11 @@ public class MulticastAppender extends A
       return true;
   }
 
+  public boolean isAdvertiseViaMulticastDNS() {
+      return advertiseViaMulticastDNS;
+  }
+
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+      this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
 }

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiver.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiver.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiver.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiver.java
Wed Mar 17 07:16:06 2010
@@ -58,6 +58,13 @@ public class MulticastReceiver extends R
   private MulticastHandlerThread handlerThread;
   private MulticastReceiverThread receiverThread;
   private boolean paused;
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
+  /**
+   * The MulticastDNS zone advertised by a MulticastReceiver
+   */
+  public static final String ZONE = "_log4j_xml_mcast_receiver.local.";
 
   public String getDecoder() {
     return decoder;
@@ -96,6 +103,9 @@ public class MulticastReceiver extends R
 
   public synchronized void shutdown() {
     isActive = false;
+    if (advertiseViaMulticastDNS) {
+        zeroConf.unadvertise();
+    }
     handlerThread.interrupt();
     receiverThread.interrupt();
     socket.close();
@@ -113,12 +123,6 @@ public class MulticastReceiver extends R
     paused = b;
   }
 
-  /**
-    Returns true if this receiver is active. */
-  public synchronized boolean isActive() {
-    return isActive;
-  }
-
   public void activateOptions() {
     InetAddress addr = null;
 
@@ -151,12 +155,25 @@ public class MulticastReceiver extends R
       receiverThread.start();
       handlerThread = new MulticastHandlerThread();
       handlerThread.start();
+      if (advertiseViaMulticastDNS) {
+        zeroConf = new ZeroConfSupport(ZONE, port, getName());
+        zeroConf.advertise();
+      }
+
     } catch (IOException ioe) {
       ioe.printStackTrace();
     }
   }
 
-  class MulticastHandlerThread extends Thread {
+    public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+        this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+    }
+
+    public boolean isAdvertiseViaMulticastDNS() {
+        return advertiseViaMulticastDNS;
+    }
+
+    class MulticastHandlerThread extends Thread {
     private List list = new ArrayList();
 
     public MulticastHandlerThread() {

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiverBeanInfo.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiverBeanInfo.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiverBeanInfo.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/MulticastReceiverBeanInfo.java
Wed Mar 17 07:16:06 2010
@@ -41,6 +41,7 @@ public class MulticastReceiverBeanInfo e
                 new PropertyDescriptor("port", MulticastReceiver.class),
                 new PropertyDescriptor("threshold", MulticastReceiver.class),
                 new PropertyDescriptor("decoder", MulticastReceiver.class),
+                new PropertyDescriptor("advertiseViaMulticastDNS", MulticastReceiver.class),
             };
         } catch (Exception e) {
         }

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketHubReceiver.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketHubReceiver.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketHubReceiver.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketHubReceiver.java
Wed Mar 17 07:16:06 2010
@@ -63,6 +63,11 @@ extends Receiver implements SocketNodeEv
      */
   protected int reconnectionDelay = DEFAULT_RECONNECTION_DELAY;
 
+  /**
+   * The MulticastDNS zone advertised by a SocketHubReceiver
+   */
+  public static final String ZONE = "_log4j_obj_tcpconnect_receiver.local.";
+
     /**
      * Active.
      */
@@ -83,6 +88,9 @@ extends Receiver implements SocketNodeEv
      */
   private List listenerList = Collections.synchronizedList(new ArrayList());
 
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
     /**
      * Create new instance.
      */
@@ -223,14 +231,6 @@ extends Receiver implements SocketNodeEv
   }
 
   /**
-    Returns true if this receiver is active.
-   @return true if receiver is active
-   */
-  public synchronized boolean isActive() {
-    return active;
-  }
-
-  /**
     Sets the flag to indicate if receiver is active or not.
    @param b new value
    */
@@ -243,6 +243,11 @@ extends Receiver implements SocketNodeEv
   public void activateOptions() {
     if (!isActive()) {
       setActive(true);
+      if (advertiseViaMulticastDNS) {
+        zeroConf = new ZeroConfSupport(ZONE, port, getName());
+        zeroConf.advertise();
+      }
+
       fireConnector(false);
     }
   }
@@ -269,6 +274,9 @@ extends Receiver implements SocketNodeEv
       connector.interrupted = true;
       connector = null;  // allow gc
     }
+    if (advertiseViaMulticastDNS) {
+        zeroConf.unadvertise();
+    }
   }
 
   /**
@@ -320,6 +328,14 @@ extends Receiver implements SocketNodeEv
     new Thread(socketNode).start();
   }
 
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+      this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+      return advertiseViaMulticastDNS;
+  }
+
   /**
    The Connector will reconnect when the server becomes available
    again.  It does this by attempting to open a new connection every

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketReceiver.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketReceiver.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketReceiver.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/SocketReceiver.java
Wed Mar 17 07:16:06 2010
@@ -75,6 +75,12 @@ public class SocketReceiver extends Rece
      * Socket list.
      */
   private Vector socketList = new Vector();
+
+  /**
+   * The MulticastDNS zone advertised by a SocketReceiver
+   */
+  public static final String ZONE = "_log4j_obj_tcpaccept_receiver.local.";
+
     /**
      * Listener.
      */
@@ -83,6 +89,8 @@ public class SocketReceiver extends Rece
      * Listeners.
      */
   private List listenerList = Collections.synchronizedList(new ArrayList());
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
 
     /**
      * Create new instance.
@@ -148,6 +156,11 @@ public class SocketReceiver extends Rece
       rThread = new Thread(this);
       rThread.setDaemon(true);
       rThread.start();
+      if (advertiseViaMulticastDNS) {
+        zeroConf = new ZeroConfSupport(ZONE, port, getName());
+        zeroConf.advertise();
+      }
+
       active = true;
     }
   }
@@ -166,6 +179,9 @@ public class SocketReceiver extends Rece
       rThread.interrupt();
       rThread = null;
     }
+    if (advertiseViaMulticastDNS) {
+        zeroConf.unadvertise();
+    }
 
     doShutdown();
   }
@@ -221,6 +237,22 @@ public class SocketReceiver extends Rece
   }
 
   /**
+    Sets the flag to indicate if receiver is active or not.
+   @param b new value
+   */
+  protected synchronized void setActive(final boolean b) {
+    active = b;
+  }
+
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+      this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+      return advertiseViaMulticastDNS;
+  }
+
+  /**
     Loop, accepting new socket connections. */
   public void run() {
     /**

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPAppender.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPAppender.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPAppender.java
Wed Mar 17 07:16:06 2010
@@ -20,8 +20,8 @@ package org.apache.log4j.net;
 import org.apache.log4j.AppenderSkeleton;
 import org.apache.log4j.helpers.Constants;
 import org.apache.log4j.spi.LoggingEvent;
-import org.apache.log4j.xml.XMLLayout;
 import org.apache.log4j.helpers.LogLog;
+import org.apache.log4j.xml.XMLLayout;
 
 import java.io.IOException;
 import java.net.DatagramPacket;
@@ -68,11 +68,18 @@ public class UDPAppender extends Appende
   int port = DEFAULT_PORT;
   DatagramSocket outSocket;
 
+  /**
+   * The MulticastDNS zone advertised by a UDPAppender
+   */
+  public static final String ZONE = "_log4j_xml_udp_appender.local.";
+
   // if there is something irrecoverably wrong with the settings, there is no
   // point in sending out packeets.
   boolean inError = false;
-  
-  public UDPAppender() {
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
+    public UDPAppender() {
       super(false);
   }
 
@@ -129,6 +136,16 @@ public class UDPAppender extends Appende
       LogLog.error(err);
       throw new IllegalStateException(err);
     }
+
+    if (layout == null) {
+        layout = new XMLLayout();
+    }
+
+    if (advertiseViaMulticastDNS) {
+      zeroConf = new ZeroConfSupport(ZONE, port, getName());
+      zeroConf.advertise();
+    }
+
     super.activateOptions();
   }
 
@@ -142,6 +159,10 @@ public class UDPAppender extends Appende
       return;
     }
 
+    if (advertiseViaMulticastDNS) {
+      zeroConf.unadvertise();
+    }
+      
     this.closed = true;
     cleanUp();
   }
@@ -199,7 +220,6 @@ public class UDPAppender extends Appende
       }
 
       try {
-        // TODO UDPAppender throws NullPointerException if the layout is not set
         StringBuffer buf = new StringBuffer(layout.format(event));
 
         byte[] payload;
@@ -300,4 +320,11 @@ public class UDPAppender extends Appende
     return port;
   }
 
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+    this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+    return advertiseViaMulticastDNS;
+  }
 }

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPReceiver.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPReceiver.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPReceiver.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/UDPReceiver.java
Wed Mar 17 07:16:06 2010
@@ -51,8 +51,16 @@ public class UDPReceiver extends Receive
   private int port;
   private DatagramSocket socket;
   UDPHandlerThread handlerThread;
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
 
-  public int getPort() {
+  /**
+   * The MulticastDNS zone advertised by a UDPReceiver
+   */
+  public static final String ZONE = "_log4j_xml_udp_receiver.local.";
+
+
+    public int getPort() {
     return port;
   }
 
@@ -91,6 +99,14 @@ public class UDPReceiver extends Receive
     paused = b;
   }
 
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+    this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+    return advertiseViaMulticastDNS;
+  }
+
   public synchronized void shutdown() {
     if(closed == true) {
       return;
@@ -102,6 +118,10 @@ public class UDPReceiver extends Receive
     	socket.close();
     }
 
+    if (advertiseViaMulticastDNS) {
+      zeroConf.unadvertise();
+    }
+      
     try {
       if(handlerThread != null) {
       	handlerThread.close();
@@ -142,6 +162,11 @@ public class UDPReceiver extends Receive
       receiverThread.start();
       handlerThread = new UDPHandlerThread();
       handlerThread.start();
+      if (advertiseViaMulticastDNS) {
+        zeroConf = new ZeroConfSupport(ZONE, port, getName());
+        zeroConf.advertise();
+      }
+
     } catch (IOException ioe) {
       ioe.printStackTrace();
     }
@@ -160,7 +185,7 @@ public class UDPReceiver extends Receive
         list.notify();
       }
     }
-  
+
     /**
      * Allow the UDPHandlerThread to wakeup and exit gracefully.
      */
@@ -177,7 +202,7 @@ public class UDPReceiver extends Receive
         synchronized (list) {
           try {
             while (!UDPReceiver.this.closed && list.size() == 0) {
-              list.wait();
+              list.wait(300);
             }
 
             if (list.size() > 0) {

Modified: logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/XMLSocketReceiver.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/XMLSocketReceiver.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/XMLSocketReceiver.java
(original)
+++ logging/log4j/companions/receivers/trunk/src/main/java/org/apache/log4j/net/XMLSocketReceiver.java
Wed Mar 17 07:16:06 2010
@@ -60,6 +60,18 @@ public class XMLSocketReceiver extends R
   private Thread rThread;
   public static final int DEFAULT_PORT = 4448;
   protected int port = DEFAULT_PORT;
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
+  /**
+   * The MulticastDNS zone advertised by an XMLSocketReceiver
+   */
+  public static final String ZONE = "_log4j_xml_tcpaccept_receiver.local.";
+
+  /*
+   * Log4j doesn't provide an XMLSocketAppender, but the MulticastDNS zone that should be
advertised by one is:
+   * _log4j_xml_tcpconnect_appender.local.
+   */
 
   public XMLSocketReceiver() {
   }
@@ -129,11 +141,13 @@ public class XMLSocketReceiver extends R
   	result = result * 37 + port;
   	return (result * 37 + (getName() != null? getName().hashCode():0));
   }
-  	
+
   /**
-    Returns true if this receiver is active. */
-  public synchronized boolean isActive() {
-    return active;
+    Sets the flag to indicate if receiver is active or not.
+   @param b new value
+   */
+  protected synchronized void setActive(final boolean b) {
+    active = b;
   }
 
   /**
@@ -143,10 +157,24 @@ public class XMLSocketReceiver extends R
       rThread = new Thread(this);
       rThread.setDaemon(true);
       rThread.start();
+
+      if (advertiseViaMulticastDNS) {
+        zeroConf = new ZeroConfSupport(ZONE, port, getName());
+        zeroConf.advertise();
+      }
+
       active = true;
     }
   }
 
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+    this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+    return advertiseViaMulticastDNS;
+  }
+
   /**
     Called when the receiver should be stopped. Closes the
     server socket and all of the open sockets. */
@@ -175,6 +203,10 @@ public class XMLSocketReceiver extends R
 
       // close all of the accepted sockets
       closeAllAcceptedSockets();
+
+      if (advertiseViaMulticastDNS) {
+          zeroConf.unadvertise();
+      }
     }
 
     /**

Modified: logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ZeroConfSocketHubAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ZeroConfSocketHubAppender.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ZeroConfSocketHubAppender.java
(original)
+++ logging/log4j/companions/zeroconf/trunk/src/main/java/org/apache/log4j/net/ZeroConfSocketHubAppender.java
Wed Mar 17 07:16:06 2010
@@ -42,63 +42,18 @@ public class ZeroConfSocketHubAppender e
     public static final String DEFAULT_ZEROCONF_ZONE = "_log4j._tcp.local.";
     private String zeroConfZone = DEFAULT_ZEROCONF_ZONE;
 
-    private Object logger;
-    private Method logInfoMethod;
-    private Method logErrorMethod;
-
     private int actualPortUsed;
     private InetAddress actualAddressUsed;
+    private ZeroConfSupport zeroConfSupport;
 
     public ZeroConfSocketHubAppender() {
         setName("SocketHubAppender");
-        try {
-            Method getLoggerMethod = this.getClass().getMethod("getLogger",
-                    new Class[0]);
-            logger = getLoggerMethod.invoke(this, new Object[0]);
-            logInfoMethod = logger.getClass().getMethod("info",
-                    new Class[] { Object.class });
-            logErrorMethod = logger.getClass().getMethod("error",
-                    new Class[] { Object.class });
-        } catch (Exception e) {
-            // we're not in log4j1.3 land
-        }
     }
 
     public void activateOptions() {
         super.activateOptions();
-        try {
-            JmDNS jmDNS = Zeroconf4log4j.getInstance();
-            ServiceInfo info = buildServiceInfo();
-            logWithlog4j12Compatibility(Level.INFO,
-                    "Registering this SocketHubAppender as :" + info);
-            jmDNS.registerService(info);
-        } catch (IOException e) {
-            logWithlog4j12Compatibility(
-                    Level.ERROR,
-                    "Failed to instantiate JmDNS to broadcast via ZeroConf, will now operate
in simple SocketHubAppender mode");
-        }
-    }
-
-    private ServiceInfo buildServiceInfo() {
-        return new ServiceInfo(zeroConfZone, getName(), actualPortUsed,
-                "SocketHubAppender on port " + this.actualPortUsed);
-    }
-
-    private void logWithlog4j12Compatibility(Level level, String message) {
-        if (logger != null && logInfoMethod != null & logErrorMethod != null)
{
-            try {
-                switch (level.toInt()) {
-                case Level.INFO_INT:
-                    logInfoMethod.invoke(logger, new Object[] { message });
-                    break;
-                case Level.ERROR_INT:
-                    logInfoMethod.invoke(logger, new Object[] { message });
-                    break;
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
+        zeroConfSupport = new ZeroConfSupport(zeroConfZone, actualPortUsed, getName());
+        zeroConfSupport.advertise();
     }
 
     /**
@@ -127,16 +82,9 @@ public class ZeroConfSocketHubAppender e
 
     public synchronized void close() {
         super.close();
-        try {
-            JmDNS jmDNS = Zeroconf4log4j.getInstance();
-            ServiceInfo info = buildServiceInfo();
-            logWithlog4j12Compatibility(Level.INFO,
-                    "Deregistering this SocketHubAppender (" + info + ")");
-            jmDNS.unregisterService(info);
-        } catch (Exception e) {
-            logWithlog4j12Compatibility(
-                    Level.ERROR,
-                    "Failed to instantiate JmDNS to broadcast via ZeroConf, will now operate
in simple SocketHubAppender mode");
+        if (zeroConfSupport != null)
+        {
+            zeroConfSupport.unadvertise();
         }
     }
 

Modified: logging/log4j/companions/zeroconf/trunk/src/test/java/org/apache/log4j/net/ZeroConfSocketHubAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/zeroconf/trunk/src/test/java/org/apache/log4j/net/ZeroConfSocketHubAppenderTest.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/companions/zeroconf/trunk/src/test/java/org/apache/log4j/net/ZeroConfSocketHubAppenderTest.java
(original)
+++ logging/log4j/companions/zeroconf/trunk/src/test/java/org/apache/log4j/net/ZeroConfSocketHubAppenderTest.java
Wed Mar 17 07:16:06 2010
@@ -41,13 +41,12 @@ public class ZeroConfSocketHubAppenderTe
 
     protected void setUp() throws Exception {
         super.setUp();
-        jmdns = Zeroconf4log4j.getInstance();
+        jmdns = (JmDNS) ZeroConfSupport.getJMDNSInstance();
     }
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        Zeroconf4log4j.shutdown();
-
+        jmdns.close();
     }
 
     /**

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketAppender.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketAppender.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketAppender.java Wed Mar 17
07:16:06 2010
@@ -118,6 +118,11 @@ public class SocketAppender extends Appe
   */
   String remoteHost;
 
+  /**
+   * The MulticastDNS zone advertised by a SocketAppender
+   */
+  public static final String ZONE = "_log4j_obj_tcpconnect_appender.local.";
+
   InetAddress address;
   int port = DEFAULT_PORT;
   ObjectOutputStream oos;
@@ -132,6 +137,8 @@ public class SocketAppender extends Appe
   // reset the ObjectOutputStream every 70 calls
   //private static final int RESET_FREQUENCY = 70;
   private static final int RESET_FREQUENCY = 1;
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
 
   public SocketAppender() {
   }
@@ -160,6 +167,10 @@ public class SocketAppender extends Appe
      Connect to the specified <b>RemoteHost</b> and <b>Port</b>.
   */
   public void activateOptions() {
+    if (advertiseViaMulticastDNS) {
+      zeroConf = new ZeroConfSupport(ZONE, port, getName());
+      zeroConf.advertise();
+    }
     connect(address, port);
   }
 
@@ -174,6 +185,10 @@ public class SocketAppender extends Appe
       return;
 
     this.closed = true;
+    if (advertiseViaMulticastDNS) {
+      zeroConf.unadvertise();
+    }
+
     cleanUp();
   }
 
@@ -276,6 +291,14 @@ public class SocketAppender extends Appe
     }
   }
 
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+    this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+    return advertiseViaMulticastDNS;
+  }
+
   void fireConnector() {
     if(connector == null) {
       LogLog.debug("Starting a new connector thread.");

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketHubAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketHubAppender.java?rev=924176&r1=924175&r2=924176&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketHubAppender.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/SocketHubAppender.java Wed Mar
17 07:16:06 2010
@@ -117,7 +117,15 @@ public class SocketHubAppender extends A
   private boolean locationInfo = false;
   private CyclicBuffer buffer = null;
   private String application;
-  
+  private boolean advertiseViaMulticastDNS;
+  private ZeroConfSupport zeroConf;
+
+  /**
+   * The MulticastDNS zone advertised by a SocketHubAppender
+   */
+  public static final String ZONE = "_log4j_obj_tcpaccept_appender.local.";
+
+
   public SocketHubAppender() { }
 
   /**
@@ -132,6 +140,10 @@ public class SocketHubAppender extends A
      Set up the socket server on the specified port.  */
   public
   void activateOptions() {
+    if (advertiseViaMulticastDNS) {
+      zeroConf = new ZeroConfSupport(ZONE, port, getName());
+      zeroConf.advertise();
+    }
     startServer();
   }
 
@@ -147,7 +159,11 @@ public class SocketHubAppender extends A
 
 	LogLog.debug("closing SocketHubAppender " + getName());
     this.closed = true;
+    if (advertiseViaMulticastDNS) {
+      zeroConf.unadvertise();
+    }
     cleanUp();
+
 	LogLog.debug("SocketHubAppender " + getName() + " closed");
   }
 
@@ -323,7 +339,15 @@ public class SocketHubAppender extends A
   boolean getLocationInfo() {
     return locationInfo;
   }
-  
+
+  public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
+    this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
+  }
+
+  public boolean isAdvertiseViaMulticastDNS() {
+    return advertiseViaMulticastDNS;
+  }
+
   /**
     Start the ServerMonitor thread. */
   private

Added: logging/log4j/trunk/src/main/java/org/apache/log4j/net/ZeroConfSupport.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/ZeroConfSupport.java?rev=924176&view=auto
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/ZeroConfSupport.java (added)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/ZeroConfSupport.java Wed Mar 17
07:16:06 2010
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j.net;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.log4j.helpers.LogLog;
+
+public class ZeroConfSupport {
+    private static Object jmDNS = initializeJMDNS();
+
+    Object serviceInfo;
+    private static Class jmDNSClass;
+    private static Class serviceInfoClass;
+
+    public ZeroConfSupport(String zone, int port, String name, Map properties) {
+        //if version 3 is available, use it to constuct a serviceInfo instance, otherwise
support the version1 API
+        boolean isVersion3 = false;
+        try {
+            //create method is in version 3, not version 1
+            jmDNSClass.getMethod("create", null);
+            isVersion3 = true;
+        } catch (NoSuchMethodException e) {
+            //no-op
+        }
+
+        if (isVersion3) {
+            LogLog.debug("using JmDNS version 3 to construct serviceInfo instance");
+            serviceInfo = buildServiceInfoVersion3(zone, port, name, properties);
+        } else {
+            LogLog.debug("using JmDNS version 1.0 to construct serviceInfo instance");
+            serviceInfo = buildServiceInfoVersion1(zone, port, name, properties);
+        }
+    }
+
+    public ZeroConfSupport(String zone, int port, String name) {
+        this(zone, port, name, new HashMap());
+    }
+
+    private static Object createJmDNSVersion1()
+    {
+        try {
+            return jmDNSClass.newInstance();
+        } catch (InstantiationException e) {
+            LogLog.warn("Unable to instantiate JMDNS", e);
+        } catch (IllegalAccessException e) {
+            LogLog.warn("Unable to instantiate JMDNS", e);
+        }
+        return null;
+    }
+
+    private static Object createJmDNSVersion3()
+    {
+        try {
+            Method jmDNSCreateMethod = jmDNSClass.getMethod("create", null);
+            return jmDNSCreateMethod.invoke(null, null);
+        } catch (IllegalAccessException e) {
+            LogLog.warn("Unable to instantiate jmdns class", e);
+        } catch (NoSuchMethodException e) {
+            LogLog.warn("Unable to access constructor", e);
+        } catch (InvocationTargetException e) {
+                LogLog.warn("Unable to call constructor", e);
+        }
+        return null;
+    }
+
+    private Object buildServiceInfoVersion1(String zone, int port, String name, Map properties)
{
+        //version 1 uses a hashtable
+        Hashtable hashtableProperties = new Hashtable(properties);
+        try {
+            Class[] args = new Class[6];
+            args[0] = String.class;
+            args[1] = String.class;
+            args[2] = int.class;
+            args[3] = int.class; //weight (0)
+            args[4] = int.class; //priority (0)
+            args[5] = Hashtable.class;
+            Constructor constructor  = serviceInfoClass.getConstructor(args);
+            Object[] values = new Object[6];
+            values[0] = zone;
+            values[1] = name;
+            values[2] = new Integer(port);
+            values[3] = new Integer(0);
+            values[4] = new Integer(0);
+            values[5] = hashtableProperties;
+            Object result = constructor.newInstance(values);
+            LogLog.debug("created serviceinfo: " + result);
+            return result;
+        } catch (IllegalAccessException e) {
+            LogLog.warn("Unable to construct ServiceInfo instance", e);
+        } catch (NoSuchMethodException e) {
+            LogLog.warn("Unable to get ServiceInfo constructor", e);
+        } catch (InstantiationException e) {
+            LogLog.warn("Unable to construct ServiceInfo instance", e);
+        } catch (InvocationTargetException e) {
+            LogLog.warn("Unable to construct ServiceInfo instance", e);
+        }
+        return null;
+    }
+
+    private Object buildServiceInfoVersion3(String zone, int port, String name, Map properties)
{
+        try {
+            Class[] args = new Class[6];
+            args[0] = String.class; //zone/type
+            args[1] = String.class; //display name
+            args[2] = int.class; //port
+            args[3] = int.class; //weight (0)
+            args[4] = int.class; //priority (0)
+            args[5] = Map.class;
+            Method serviceInfoCreateMethod = serviceInfoClass.getMethod("create", args);
+            Object[] values = new Object[6];
+            values[0] = zone;
+            values[1] = name;
+            values[2] = new Integer(port);
+            values[3] = new Integer(0);
+            values[4] = new Integer(0);
+            values[5] = properties;
+            Object result = serviceInfoCreateMethod.invoke(null, values);
+            LogLog.debug("created serviceinfo: " + result);
+            return result;
+        } catch (IllegalAccessException e) {
+            LogLog.warn("Unable to invoke create method", e);
+        } catch (NoSuchMethodException e) {
+            LogLog.warn("Unable to find create method", e);
+        } catch (InvocationTargetException e) {
+                LogLog.warn("Unable to invoke create method", e);
+        }
+        return null;
+    }
+
+    public void advertise() {
+        try {
+            Method method = jmDNSClass.getMethod("registerService", new Class[]{serviceInfoClass});
+            method.invoke(jmDNS, new Object[]{serviceInfo});
+            LogLog.debug("registered serviceInfo: " + serviceInfo);
+        } catch(IllegalAccessException e) {
+            LogLog.warn("Unable to invoke registerService method", e);
+        } catch(NoSuchMethodException e) {
+            LogLog.warn("No registerService method", e);
+        } catch(InvocationTargetException e) {
+            LogLog.warn("Unable to invoke registerService method", e);
+        }
+    }
+
+    public void unadvertise() {
+        try {
+            Method method = jmDNSClass.getMethod("unregisterService", new Class[]{serviceInfoClass});
+            method.invoke(jmDNS, new Object[]{serviceInfo});
+            LogLog.debug("unregistered serviceInfo: " + serviceInfo);
+        } catch(IllegalAccessException e) {
+            LogLog.warn("Unable to invoke unregisterService method", e);
+        } catch(NoSuchMethodException e) {
+            LogLog.warn("No unregisterService method", e);
+        } catch(InvocationTargetException e) {
+            LogLog.warn("Unable to invoke unregisterService method", e);
+       }
+    }
+
+    private static Object initializeJMDNS() {
+        try {
+            jmDNSClass = Class.forName("javax.jmdns.JmDNS");
+            serviceInfoClass = Class.forName("javax.jmdns.ServiceInfo");
+        } catch (ClassNotFoundException e) {
+            LogLog.warn("JmDNS or serviceInfo class not found", e);
+        }
+
+        //if version 3 is available, use it to constuct a serviceInfo instance, otherwise
support the version1 API
+        boolean isVersion3 = false;
+        try {
+            //create method is in version 3, not version 1
+            jmDNSClass.getMethod("create", null);
+            isVersion3 = true;
+        } catch (NoSuchMethodException e) {
+            //no-op
+        }
+
+        if (isVersion3) {
+            return createJmDNSVersion3();
+        } else {
+            return createJmDNSVersion1();
+        }
+    }
+
+    public static Object getJMDNSInstance() {
+        return jmDNS;
+    }
+}



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


Mime
View raw message