1 16 17 package com.systemsunion.LoggingServer; 18 19 import java.net.InetAddress ; 20 import java.net.Socket ; 21 import java.net.ServerSocket ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.ObjectInputStream ; 25 26 27 import org.apache.log4j.Category; 28 import org.apache.log4j.spi.LoggingEvent; 29 import org.apache.log4j.Priority; 30 import org.apache.log4j.NDC; 31 32 34 46 public class SocketNode2 implements Runnable { 47 48 Socket socket; 49 ObjectInputStream ois; 50 51 static Category cat = Category.getInstance(SocketNode2.class.getName()); 52 53 public 54 SocketNode2(Socket socket) { 55 this.socket = socket; 56 try { 57 ois = new ObjectInputStream (socket.getInputStream()); 58 } 59 catch(Exception e) { 60 cat.error("Could not open ObjectInputStream to "+socket, e); 61 } 62 } 63 64 70 public void run() { 71 LoggingEvent event; 72 Category remoteCategory; 73 String strClientName; 74 75 InetAddress addr = socket.getInetAddress(); 77 strClientName = addr.getHostName(); 78 if(strClientName == null || strClientName.length() == 0) 79 { 80 strClientName = addr.getHostAddress(); 81 } 82 83 try { 84 while(true) { 85 event = (LoggingEvent) ois.readObject(); 86 87 if(event.ndc != null) 88 { 89 event.ndc = strClientName + ":" + event.ndc; 90 } 91 else 92 { 93 event.ndc = strClientName; 94 } 95 96 remoteCategory = Category.getInstance(event.categoryName); 97 remoteCategory.callAppenders(event); 98 } 99 } 100 catch(java.io.EOFException e) { 101 cat.info("Caught java.io.EOFException will close conneciton.", e); 102 } 103 catch(java.net.SocketException e) { 104 cat.info("Caught java.net.SocketException, will close conneciton.", e); 105 } 106 catch(Exception e) { 107 cat.error("Unexpected exception. Closing conneciton.", e); 108 } 109 110 try { 111 ois.close(); 112 } 113 catch(Exception e) { 114 cat.info("Could not close connection.", e); 115 } 116 } 117 } 118 | Popular Tags |