r500753 | eross | 2007-01-28 02:20:30 -0600 (Sun, 28 Jan 2007) | 2 lines
Bug 16280 - Fix error output when server socket is closed on shutdown.
Add test to verify this appender works.
Index: src/java/org/apache/log4j/net/TelnetAppender.java
===================================================================
84a85
> super.activateOptions();
98c99,103
< sh.finalize();
---
> sh.close();
> try {
> sh.join();
> } catch (InterruptedException e) {
> }
131d135
< private boolean done = false;
140a145
> setName("TelnetAppender-" + getName() + "-" + port);
143d147
< /** make sure we close all network connections when this
handler is destroyed. */
144a149,153
> close();
> }
>
> /** make sure we close all network connections when this
handler is destroyed. */
> public void close() {
148c157
< } catch (Exception ex) {
---
> } catch (IOException ex) {
151a161
> interrupt();
154c164,165
< } catch (Exception ex) {
---
> } catch (IOException e) {
> e.printStackTrace();
156,157d166
<
< done = true;
187c196
< while (!done) {
---
> while (!Thread.interrupted()) {
208a218
> if (!Thread.interrupted())
209a220,221
> break;
> }
210a223,226
>
> try {
> serverSocket.close();
> } catch (IOException ex) {
211a228
>
Index: tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java
===================================================================
0a1,58
> 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");
> ta.close();
> Thread.sleep(200);
> t.interrupt();
> t.join();
> String s = bo.toString();
> assertEquals(true, s.endsWith("INFO - hi"));
> assertEquals(oldActive, Thread.activeCount());
> }
>
> }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]