Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
grobmeier committed Dec 23, 2023
1 parent 270ce55 commit 45cdc8c
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/main/java/org/apache/log4j/net/JsonReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
* @author Robert Middleton
*/
public class JsonReceiver extends ChainsawReceiverSkeleton implements Runnable, PortBased {
private ServerSocket m_serverSocket;
private Thread m_rxThread;
private static final Logger logger = LogManager.getLogger(JsonReceiver.class);

private ServerSocket serverSocket;
private Thread rxThread;
public static final int DEFAULT_PORT = 4449;
protected int m_port = DEFAULT_PORT;
protected int port = DEFAULT_PORT;
private boolean active = false;

private static final Logger logger = LogManager.getLogger();

/**
* The MulticastDNS zone advertised by an XMLSocketReceiver
*/
Expand All @@ -53,9 +53,9 @@ public void shutdown() {
// mark this as no longer running
active = false;

if (m_rxThread != null) {
m_rxThread.interrupt();
m_rxThread = null;
if (rxThread != null) {
rxThread.interrupt();
rxThread = null;
}
doShutdown();
}
Expand All @@ -80,23 +80,23 @@ private void closeServerSocket() {
logger.debug("{} closing server socket", getName());

try {
if (m_serverSocket != null) {
m_serverSocket.close();
if (serverSocket != null) {
serverSocket.close();
}
} catch (Exception e) {
// ignore for now
}

m_serverSocket = null;
serverSocket = null;
}

@Override
public void start() {
logger.debug("Starting receiver");
if (!isActive()) {
m_rxThread = new Thread(this);
m_rxThread.setDaemon(true);
m_rxThread.start();
rxThread = new Thread(this);
rxThread.setDaemon(true);
rxThread.start();

active = true;
}
Expand All @@ -105,19 +105,18 @@ public void start() {
@Override
@SuppressFBWarnings
public void run() {
/**
* Ensure we start fresh.
*/
logger.debug("performing socket cleanup prior to entering loop for {}", name);
/* Ensure we start fresh. */
closeServerSocket();
logger.debug("socket cleanup complete for {}", name);
active = true;

// start the server socket
try {
m_serverSocket = new ServerSocket(m_port, 1);
serverSocket = new ServerSocket(port, 1);
} catch (Exception e) {
logger.error("error starting JsonReceiver (" + this.getName() + "), receiver did not start", e);
logger.error("error starting JsonReceiver ({}), receiver did not start", this.getName());
logger.error(e, e);
active = false;
doShutdown();

Expand All @@ -129,9 +128,7 @@ public void run() {
try {
logger.debug("in run-about to enter while isactiveloop");

active = true;

while (!m_rxThread.isInterrupted()) {
while (!rxThread.isInterrupted()) {
// if we have a socket, start watching it
if (socket != null) {
logger.debug("socket not null - parsing data");
Expand All @@ -141,7 +138,7 @@ public void run() {
logger.debug("waiting to accept socket");

// wait for a socket to open, then loop to start it
socket = m_serverSocket.accept();
socket = serverSocket.accept();
logger.debug("accepted socket");
}

Expand All @@ -157,11 +154,11 @@ public void run() {

@Override
public int getPort() {
return m_port;
return port;
}

public void setPort(int portnum) {
m_port = portnum;
port = portnum;
}

@Override
Expand All @@ -175,8 +172,8 @@ private void parseIncomingData(Socket sock) {
try {
is = sock.getInputStream();
} catch (Exception e) {
is = null;
logger.error("Exception opening InputStream to " + sock, e);
logger.error("Exception opening InputStream to {}", sock);
logger.error(e,e);
return;
}

Expand All @@ -193,6 +190,7 @@ private void parseIncomingData(Socket sock) {
// Because the socket can be closed, if we don't have anything parsed
// assume that the socket is closed.
if (!iter.hasNext()) break;

while (iter.hasNext()) {
ECSLogEvent evt = iter.next();
append(evt.toChainsawLoggingEvent(build));
Expand All @@ -203,13 +201,12 @@ private void parseIncomingData(Socket sock) {
}
}

// close the socket
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
// logger.info("Could not close connection.", e);
logger.error("Could not close connection.", e);
}
}
}

0 comments on commit 45cdc8c

Please sign in to comment.