1 16 17 package org.apache.log4j.net; 18 19 import java.util.Vector ; 20 import java.net.Socket ; 21 import java.net.ServerSocket ; 22 import java.net.SocketException ; 23 import java.io.ObjectOutputStream ; 24 import java.io.IOException ; 25 import java.io.InterruptedIOException ; 26 import java.net.InetAddress ; 27 28 import org.apache.log4j.helpers.LogLog; 29 import org.apache.log4j.spi.LoggingEvent; 30 import org.apache.log4j.AppenderSkeleton; 31 32 105 106 public class SocketHubAppender extends AppenderSkeleton { 107 108 110 static final int DEFAULT_PORT = 4560; 111 112 private int port = DEFAULT_PORT; 113 private Vector oosList = new Vector (); 114 private ServerMonitor serverMonitor = null; 115 private boolean locationInfo = false; 116 117 public SocketHubAppender() { } 118 119 121 public 122 SocketHubAppender(int _port) { 123 port = _port; 124 startServer(); 125 } 126 127 129 public 130 void activateOptions() { 131 startServer(); 132 } 133 134 138 synchronized 139 public 140 void close() { 141 if(closed) 142 return; 143 144 LogLog.debug("closing SocketHubAppender " + getName()); 145 this.closed = true; 146 cleanUp(); 147 LogLog.debug("SocketHubAppender " + getName() + " closed"); 148 } 149 150 153 public 154 void cleanUp() { 155 LogLog.debug("stopping ServerSocket"); 157 serverMonitor.stopMonitor(); 158 serverMonitor = null; 159 160 LogLog.debug("closing client connections"); 162 while (oosList.size() != 0) { 163 ObjectOutputStream oos = (ObjectOutputStream )oosList.elementAt(0); 164 if(oos != null) { 165 try { 166 oos.close(); 167 } 168 catch(IOException e) { 169 LogLog.error("could not close oos.", e); 170 } 171 172 oosList.removeElementAt(0); 173 } 174 } 175 } 176 177 179 public 180 void append(LoggingEvent event) { 181 if(event == null || oosList.size() == 0) 183 return; 184 185 if (locationInfo) { 187 event.getLocationInformation(); 188 } 189 190 for (int streamCount = 0; streamCount < oosList.size(); streamCount++) { 192 193 ObjectOutputStream oos = null; 194 try { 195 oos = (ObjectOutputStream )oosList.elementAt(streamCount); 196 } 197 catch (ArrayIndexOutOfBoundsException e) { 198 } 202 203 if (oos == null) 205 break; 206 207 try { 208 oos.writeObject(event); 209 oos.flush(); 210 oos.reset(); 214 } 215 catch(IOException e) { 216 oosList.removeElementAt(streamCount); 218 LogLog.debug("dropped connection"); 219 220 streamCount--; 222 } 223 } 224 } 225 226 229 public 230 boolean requiresLayout() { 231 return false; 232 } 233 234 237 public 238 void setPort(int _port) { 239 port = _port; 240 } 241 242 244 public 245 int getPort() { 246 return port; 247 } 248 249 253 public 254 void setLocationInfo(boolean _locationInfo) { 255 locationInfo = _locationInfo; 256 } 257 258 260 public 261 boolean getLocationInfo() { 262 return locationInfo; 263 } 264 265 267 private 268 void startServer() { 269 serverMonitor = new ServerMonitor(port, oosList); 270 } 271 272 276 private 277 class ServerMonitor implements Runnable { 278 private int port; 279 private Vector oosList; 280 private boolean keepRunning; 281 private Thread monitorThread; 282 283 285 public 286 ServerMonitor(int _port, Vector _oosList) { 287 port = _port; 288 oosList = _oosList; 289 keepRunning = true; 290 monitorThread = new Thread (this); 291 monitorThread.setDaemon(true); 292 monitorThread.start(); 293 } 294 295 298 public 299 synchronized 300 void stopMonitor() { 301 if (keepRunning) { 302 LogLog.debug("server monitor thread shutting down"); 303 keepRunning = false; 304 try { 305 monitorThread.join(); 306 } 307 catch (InterruptedException e) { 308 } 310 311 monitorThread = null; 313 LogLog.debug("server monitor thread shut down"); 314 } 315 } 316 317 320 public 321 void run() { 322 ServerSocket serverSocket = null; 323 try { 324 serverSocket = new ServerSocket (port); 325 serverSocket.setSoTimeout(1000); 326 } 327 catch (Exception e) { 328 LogLog.error("exception setting timeout, shutting down server socket.", e); 329 keepRunning = false; 330 return; 331 } 332 333 try { 334 try { 335 serverSocket.setSoTimeout(1000); 336 } 337 catch (SocketException e) { 338 LogLog.error("exception setting timeout, shutting down server socket.", e); 339 return; 340 } 341 342 while (keepRunning) { 343 Socket socket = null; 344 try { 345 socket = serverSocket.accept(); 346 } 347 catch (InterruptedIOException e) { 348 } 350 catch (SocketException e) { 351 LogLog.error("exception accepting socket, shutting down server socket.", e); 352 keepRunning = false; 353 } 354 catch (IOException e) { 355 LogLog.error("exception accepting socket.", e); 356 } 357 358 if (socket != null) { 360 try { 361 InetAddress remoteAddress = socket.getInetAddress(); 362 LogLog.debug("accepting connection from " + remoteAddress.getHostName() 363 + " (" + remoteAddress.getHostAddress() + ")"); 364 365 ObjectOutputStream oos = new ObjectOutputStream (socket.getOutputStream()); 367 368 oosList.addElement(oos); 370 } 371 catch (IOException e) { 372 LogLog.error("exception creating output stream on socket.", e); 373 } 374 } 375 } 376 } 377 finally { 378 try { 380 serverSocket.close(); 381 } 382 catch (IOException e) { 383 } 385 } 386 } 387 } 388 } 389 390 | Popular Tags |