1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.net;
19
20 import org.apache.log4j.AppenderSkeleton;
21 import org.apache.log4j.helpers.LogLog;
22 import org.apache.log4j.spi.LoggingEvent;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.InterruptedIOException;
27 import java.net.ServerSocket;
28 import java.net.Socket;
29 import java.util.Enumeration;
30 import java.util.Iterator;
31 import java.util.Vector;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class TelnetAppender extends AppenderSkeleton {
62
63 private SocketHandler sh;
64 private int port = 23;
65
66
67
68
69 public boolean requiresLayout() {
70 return true;
71 }
72
73
74
75 public void activateOptions() {
76 try {
77 sh = new SocketHandler(port);
78 sh.start();
79 }
80 catch(InterruptedIOException e) {
81 Thread.currentThread().interrupt();
82 e.printStackTrace();
83 } catch(IOException e) {
84 e.printStackTrace();
85 } catch(RuntimeException e) {
86 e.printStackTrace();
87 }
88 super.activateOptions();
89 }
90
91 public
92 int getPort() {
93 return port;
94 }
95
96 public
97 void setPort(int port) {
98 this.port = port;
99 }
100
101
102
103 public void close() {
104 if (sh != null) {
105 sh.close();
106 try {
107 sh.join();
108 } catch(InterruptedException ex) {
109 Thread.currentThread().interrupt();
110 }
111 }
112 }
113
114
115
116 protected void append(LoggingEvent event) {
117 if(sh != null) {
118 sh.send(layout.format(event));
119 if(layout.ignoresThrowable()) {
120 String[] s = event.getThrowableStrRep();
121 if (s != null) {
122 StringBuffer buf = new StringBuffer();
123 for(int i = 0; i < s.length; i++) {
124 buf.append(s[i]);
125 buf.append("\r\n");
126 }
127 sh.send(buf.toString());
128 }
129 }
130 }
131 }
132
133
134
135
136
137
138 protected class SocketHandler extends Thread {
139
140 private Vector writers = new Vector();
141 private Vector connections = new Vector();
142 private ServerSocket serverSocket;
143 private int MAX_CONNECTIONS = 20;
144
145 public void finalize() {
146 close();
147 }
148
149
150
151
152
153 public void close() {
154 synchronized(this) {
155 for(Enumeration e = connections.elements();e.hasMoreElements();) {
156 try {
157 ((Socket)e.nextElement()).close();
158 } catch(InterruptedIOException ex) {
159 Thread.currentThread().interrupt();
160 } catch(IOException ex) {
161 } catch(RuntimeException ex) {
162 }
163 }
164 }
165
166 try {
167 serverSocket.close();
168 } catch(InterruptedIOException ex) {
169 Thread.currentThread().interrupt();
170 } catch(IOException ex) {
171 } catch(RuntimeException ex) {
172 }
173 }
174
175
176 public synchronized void send(final String message) {
177 Iterator ce = connections.iterator();
178 for(Iterator e = writers.iterator();e.hasNext();) {
179 ce.next();
180 PrintWriter writer = (PrintWriter)e.next();
181 writer.print(message);
182 if(writer.checkError()) {
183 ce.remove();
184 e.remove();
185 }
186 }
187 }
188
189
190
191
192
193 public void run() {
194 while(!serverSocket.isClosed()) {
195 try {
196 Socket newClient = serverSocket.accept();
197 PrintWriter pw = new PrintWriter(newClient.getOutputStream());
198 if(connections.size() < MAX_CONNECTIONS) {
199 synchronized(this) {
200 connections.addElement(newClient);
201 writers.addElement(pw);
202 pw.print("TelnetAppender v1.0 (" + connections.size()
203 + " active connections)\r\n\r\n");
204 pw.flush();
205 }
206 } else {
207 pw.print("Too many connections.\r\n");
208 pw.flush();
209 newClient.close();
210 }
211 } catch(Exception e) {
212 if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
213 Thread.currentThread().interrupt();
214 }
215 if (!serverSocket.isClosed()) {
216 LogLog.error("Encountered error while in SocketHandler loop.", e);
217 }
218 break;
219 }
220 }
221
222 try {
223 serverSocket.close();
224 } catch(InterruptedIOException ex) {
225 Thread.currentThread().interrupt();
226 } catch(IOException ex) {
227 }
228 }
229
230 public SocketHandler(int port) throws IOException {
231 serverSocket = new ServerSocket(port);
232 setName("TelnetAppender-" + getName() + "-" + port);
233 }
234
235 }
236 }