You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2007/08/22 19:32:43 UTC

svn commit: r568709 - in /logging/log4j/trunk: src/changes/changes.xml src/main/java/org/apache/log4j/net/TelnetAppender.java tests/src/java/org/apache/log4j/CoreTestSuite.java tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java

Author: carnold
Date: Wed Aug 22 10:32:43 2007
New Revision: 568709

URL: http://svn.apache.org/viewvc?rev=568709&view=rev
Log:
Bug 16280: Error Message always logged to LogLog when calling close on TelnetAppender

Added:
    logging/log4j/trunk/tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java
Modified:
    logging/log4j/trunk/src/changes/changes.xml
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/TelnetAppender.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java

Modified: logging/log4j/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=568709&r1=568708&r2=568709&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Wed Aug 22 10:32:43 2007
@@ -22,6 +22,7 @@
   <body>
   
     <release version="1.2.15" date="2007-06-27" description="SyslogAppender enhancements, NTEventLogAppender and Maven build.">
+       <action action="fix" issue="14350">Error message always logged to LogLog when calling close on TelnetAppender.</action> 
        <action action="add" issue="32572">Add configurable triggeringPolicy for SMTPAppender.</action> 
        <action action="fix" issue="43181">NullPointerException in MDC on Tomcat reload.</action>
         <action action="fix" issue="34874">Notice to use UTF-8 or UTF-16 encoding added to XML and HTMLLayout javadoc.</action>

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/net/TelnetAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/TelnetAppender.java?rev=568709&r1=568708&r2=568709&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/TelnetAppender.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/TelnetAppender.java Wed Aug 22 10:32:43 2007
@@ -75,6 +75,7 @@
     catch(Exception e) {
       e.printStackTrace();
     }
+    super.activateOptions();
   }
 
   public
@@ -90,7 +91,13 @@
 
   /** shuts down the appender. */
   public void close() {
-    sh.finalize();
+    if (sh != null) {
+        sh.close();
+        try {
+            sh.join();
+        } catch(InterruptedException ex) {
+        }
+    }
   }
 
   /** Handles a log event.  For this appender, that means writing the
@@ -116,25 +123,28 @@
       asynchronously. */
   protected class SocketHandler extends Thread {
 
-    private boolean done = false;
     private Vector writers = new Vector();
     private Vector connections = new Vector();
     private ServerSocket serverSocket;
     private int MAX_CONNECTIONS = 20;
 
-    /** make sure we close all network connections when this handler is destroyed. */
     public void finalize() {
+        close();
+    }
+      
+    /** make sure we close all network connections when this handler is destroyed. */
+    public void close() {
       for(Enumeration e = connections.elements();e.hasMoreElements();) {
         try {
           ((Socket)e.nextElement()).close();
         } catch(Exception ex) {
         }
       }
+
       try {
         serverSocket.close();
       } catch(Exception ex) {
       }
-      done = true;
     }
 
     /** sends a message to each of the clients in telnet-friendly output. */
@@ -157,7 +167,7 @@
         are refused when MAX_CONNECTIONS is reached. 
     */
     public void run() {
-      while(!done) {
+      while(!serverSocket.isClosed()) {
         try {
           Socket newClient = serverSocket.accept();
           PrintWriter pw = new PrintWriter(newClient.getOutputStream());
@@ -173,13 +183,22 @@
             newClient.close();
           }
         } catch(Exception e) {
-          LogLog.error("Encountered error while in SocketHandler loop.", e);
+          if (!serverSocket.isClosed()) {
+            LogLog.error("Encountered error while in SocketHandler loop.", e);
+          }
+          break;
         }
       }
+
+      try {
+          serverSocket.close();
+      } catch(IOException ex) {
+      }
     }
 
     public SocketHandler(int port) throws IOException {
       serverSocket = new ServerSocket(port);
+      setName("TelnetAppender-" + getName() + "-" + port);
     }
 
   }

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java?rev=568709&r1=568708&r2=568709&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java Wed Aug 22 10:32:43 2007
@@ -51,6 +51,7 @@
         s.addTestSuite(org.apache.log4j.spi.LocationInfoTest.class);
         s.addTestSuite(org.apache.log4j.PropertyConfiguratorTest.class);
         s.addTestSuite(org.apache.log4j.net.SMTPAppenderTest.class);
+        s.addTestSuite(org.apache.log4j.net.TelnetAppenderTest.class);
         return s;
     }
 }

Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java?rev=568709&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java Wed Aug 22 10:32:43 2007
@@ -0,0 +1,75 @@
+/*
+ * 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.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Socket;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+
+import junit.framework.TestCase;
+
+public class TelnetAppenderTest extends TestCase {
+
+  int port = 54353;
+  ByteArrayOutputStream bo = new ByteArrayOutputStream();
+
+  public class ReadThread extends Thread {
+    public void run() {
+      try {
+        Socket s = new Socket("localhost", port);
+        InputStream i = s.getInputStream();
+        while (!Thread.interrupted()) {
+          int c = i.read();
+          if (c == -1)
+            break;
+          bo.write(c);
+        }
+        s.close();
+      } catch (IOException e) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  public void testIt() throws Exception {
+    int oldActive = Thread.activeCount();
+    TelnetAppender ta = new TelnetAppender();
+    ta.setName("ta");
+    ta.setPort(port);
+    ta.setLayout(new PatternLayout("%p - %m"));
+    ta.activateOptions();
+    Logger l = Logger.getLogger("x");
+    l.addAppender(ta);
+    Thread t = new ReadThread();
+    t.start();
+    Thread.sleep(200);
+    l.info("hi");
+    Thread.sleep(1000);
+    ta.close();
+    Thread.sleep(200);
+    t.interrupt();
+    t.join();
+    String s = bo.toString();
+    assertTrue(s.endsWith("INFO - hi"));
+    assertEquals(oldActive, Thread.activeCount());
+  }
+
+}



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