On 07/11/2007, at 7:15 PM, Curt Arnold wrote:


On Nov 7, 2007, at 1:55 AM, Curt Arnold wrote:

Okay, I'm about ready to fall over, but I looked at zeroconf and see your motivation for moving the binding onto the main thread so you continue the set up in ZeroConfSocketHubAppender.activateOptions. However instead of changing the behavior, I'd be more inclined to make a few changes to SocketHubAppender to make it more amenable to extension but keep the old behavior. I'd suggest:

Moving ServerMonitor out to a separate public class.
Move the self-start in ServerMonitor to SocketHubAppender.startServer. Make SocketHubAppender.startServer protected and overload it in ZeroConf. Add a protected SocketHubAppender.stopServer and move the ServerMonitor.stopMonitor code there.


I'd suggest that you not take code suggestions from me too seriously this late at night. There is probably a cleaner way to be able to provide a hook for the zeroconf registration code to run. Maybe adding like adding

protected void socketBound(ServerSocket s) {
}


Ok, i'm a bit tired, so I ended up just going simple and pretty much following Curt's idea, see below. Thoughts?

Index: src/main/java/org/apache/log4j/net/SocketHubAppender.java
===================================================================
--- src/main/java/org/apache/log4j/net/SocketHubAppender.java (revision 592202) +++ src/main/java/org/apache/log4j/net/SocketHubAppender.java (working copy)
@@ -17,18 +17,18 @@

 package org.apache.log4j.net;

-import java.util.Vector;
-import java.net.Socket;
-import java.net.ServerSocket;
-import java.net.SocketException;
-import java.io.ObjectOutputStream;
 import java.io.IOException;
 import java.io.InterruptedIOException;
+import java.io.ObjectOutputStream;
 import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.Vector;

+import org.apache.log4j.AppenderSkeleton;
 import org.apache.log4j.helpers.LogLog;
 import org.apache.log4j.spi.LoggingEvent;
-import org.apache.log4j.AppenderSkeleton;

 /**
   Sends [EMAIL PROTECTED] LoggingEvent} objects to a set of remote log servers,
@@ -271,6 +271,19 @@
   }

   /**
+ * Once the [EMAIL PROTECTED] ServerSocket} is created and bound, this method is called to notify the class the details + * of the address and port chosen. In the standard SocketHubAppender case this will be the + * requested port, but if the port is specified as 0, then a random port is chosen by the underlying OS. + * Child classes may be interested in the actual port chosen, and this method may be overridden + * and used to gain the details of the actual socket being listened on.
+   * @param address
+   * @param actualPortUsed
+   */
+  protected void socketBound(InetAddress address, int actualPortUsed) {
+
+  }
+
+  /**
     This class is used internally to monitor a ServerSocket
     and register new connections in a vector passed in the
     constructor. */
@@ -280,6 +293,7 @@
     private Vector oosList;
     private boolean keepRunning;
     private Thread monitorThread;
+    private ServerSocket serverSocket;

     /**
       Create a thread and start the monitor. */
@@ -288,9 +302,15 @@
       port = _port;
       oosList = _oosList;
       keepRunning = true;
+      try {
+        serverSocket = new ServerSocket(port);
+      } catch (IOException e) {
+ throw new RuntimeException("Failed to create a ServerSocket", e);
+      }
       monitorThread = new Thread(this);
       monitorThread.setDaemon(true);
       monitorThread.start();
+ socketBound(serverSocket.getInetAddress(), serverSocket.getLocalPort());
     }

     /**
@@ -320,9 +340,8 @@
       they connect to the socket. */
     public
     void run() {
-      ServerSocket serverSocket = null;
+
       try {
-        serverSocket = new ServerSocket(port);
         serverSocket.setSoTimeout(1000);
       }
       catch (Exception e) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to