in src/main/java/org/apache/log4j/receivers/net/XMLSocketNode.java [106:207]
public void run() {
Logger remoteLogger;
Exception listenerException = null;
InputStream is = null;
if ((this.receiver == null) || (this.decoder == null)) {
is = null;
listenerException =
new Exception(
"No receiver or decoder provided. Cannot process xml socket events");
getLogger().error(
"Exception constructing XML Socket Receiver", listenerException);
}
try {
is = socket.getInputStream();
} catch (Exception e) {
is = null;
listenerException = e;
getLogger().error("Exception opening ObjectInputStream to " + socket, e);
}
if (is != null) {
String hostName = socket.getInetAddress().getHostName();
String remoteInfo = hostName + ":" + socket.getPort();
try {
//read data from the socket
//it's up to the individual decoder to handle incomplete event data
while (true) {
byte[] b = new byte[1024];
int length = is.read(b);
if (length == -1) {
getLogger().info(
"no bytes read from stream - closing connection.");
break;
}
List v = decoder.decodeEvents(new String(b, 0, length));
if (v != null) {
Iterator iter = v.iterator();
while (iter.hasNext()) {
LoggingEvent e = (LoggingEvent) iter.next();
e.setProperty(Constants.HOSTNAME_KEY, hostName);
// store the known remote info in an event property
e.setProperty("log4j.remoteSourceInfo", remoteInfo);
// if configured with a receiver, tell it to post the event
if (receiver != null) {
receiver.doPost(e);
// else post it via the hierarchy
} else {
// get a logger from the hierarchy. The name of the logger
// is taken to be the name contained in the event.
remoteLogger = repository.getLogger(e.getLoggerName());
//event.logger = remoteLogger;
// apply the logger-level filter
if (
e.getLevel().isGreaterOrEqual(
remoteLogger.getEffectiveLevel())) {
// finally log the event as if was generated locally
remoteLogger.callAppenders(e);
}
}
}
}
}
} catch (java.io.EOFException e) {
getLogger().info("Caught java.io.EOFException closing connection.");
listenerException = e;
} catch (java.net.SocketException e) {
getLogger().info(
"Caught java.net.SocketException closing connection.");
listenerException = e;
} catch (IOException e) {
getLogger().info("Caught java.io.IOException: " + e);
getLogger().info("Closing connection.");
listenerException = e;
} catch (Exception e) {
getLogger().error("Unexpected exception. Closing connection.", e);
listenerException = e;
}
}
// close the socket
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
//logger.info("Could not close connection.", e);
}
// send event to listener, if configured
if (listener != null) {
listener.socketClosedEvent(listenerException);
}
}