Commit d13236c7 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Protect against NullPointerException better.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3342 b35dd754-fafc-0310-a699-88a17e54d16e
parent 895dfa0f
...@@ -25,11 +25,9 @@ import org.xmpp.packet.Packet; ...@@ -25,11 +25,9 @@ import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import java.io.*; import java.io.*;
import java.util.Date; import java.util.*;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class AuditorImpl implements Auditor { public class AuditorImpl implements Auditor {
...@@ -39,7 +37,6 @@ public class AuditorImpl implements Auditor { ...@@ -39,7 +37,6 @@ public class AuditorImpl implements Auditor {
private org.jivesoftware.util.XMLWriter xmlWriter; private org.jivesoftware.util.XMLWriter xmlWriter;
private int maxSize; private int maxSize;
private long maxCount; private long maxCount;
private int logTimeout;
private boolean closed = false; private boolean closed = false;
/** /**
* Directoty (absolute path) where the audit files will be saved. * Directoty (absolute path) where the audit files will be saved.
...@@ -49,7 +46,7 @@ public class AuditorImpl implements Auditor { ...@@ -49,7 +46,7 @@ public class AuditorImpl implements Auditor {
/** /**
* Queue that holds the audited packets that will be later saved to an XML file. * Queue that holds the audited packets that will be later saved to an XML file.
*/ */
private Queue<AuditPacket> logQueue = new LinkedBlockingQueue<AuditPacket>(); private BlockingQueue<AuditPacket> logQueue = new LinkedBlockingQueue<AuditPacket>();
/** /**
* Timer to save queued logs to the XML file. * Timer to save queued logs to the XML file.
...@@ -121,12 +118,11 @@ public class AuditorImpl implements Auditor { ...@@ -121,12 +118,11 @@ public class AuditorImpl implements Auditor {
maxCount = count; maxCount = count;
} }
public void setLogTimeout(int newTimeout) { public void setLogTimeout(int logTimeout) {
// Cancel any existing task because the timeout has changed // Cancel any existing task because the timeout has changed
if (saveQueuedPacketsTask != null) { if (saveQueuedPacketsTask != null) {
saveQueuedPacketsTask.cancel(); saveQueuedPacketsTask.cancel();
} }
this.logTimeout = newTimeout;
// Create a new task and schedule it with the new timeout // Create a new task and schedule it with the new timeout
saveQueuedPacketsTask = new SaveQueuedPacketsTask(); saveQueuedPacketsTask = new SaveQueuedPacketsTask();
timer.schedule(saveQueuedPacketsTask, logTimeout, logTimeout); timer.schedule(saveQueuedPacketsTask, logTimeout, logTimeout);
...@@ -191,17 +187,21 @@ public class AuditorImpl implements Auditor { ...@@ -191,17 +187,21 @@ public class AuditorImpl implements Auditor {
} }
private void saveQueuedPackets() { private void saveQueuedPackets() {
int batchSize = logQueue.size(); List<AuditPacket> packets = new ArrayList<AuditPacket>(logQueue.size());
for (int index = 0; index < batchSize; index++) { logQueue.drainTo(packets);
AuditPacket auditPacket = logQueue.poll(); for (AuditPacket auditPacket : packets) {
if (auditPacket != null) {
try { try {
prepareAuditFile(); prepareAuditFile();
xmlWriter.write(auditPacket.getElement()); Element element = auditPacket.getElement();
// Protect against null elements.
if (element != null) {
xmlWriter.write(element);
}
} }
catch (IOException e) { catch (IOException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
// Add again the entry to the queue to save it later // Add again the entry to the queue to save it later
if (xmlWriter != null) {
logQueue.add(auditPacket); logQueue.add(auditPacket);
} }
} }
...@@ -212,7 +212,7 @@ public class AuditorImpl implements Auditor { ...@@ -212,7 +212,7 @@ public class AuditorImpl implements Auditor {
} }
} }
catch (IOException ioe) { catch (IOException ioe) {
Log.error(ioe);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment