Commit 85fba0ad authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@606 b35dd754-fafc-0310-a699-88a17e54d16e
parent 3a31141c
...@@ -14,7 +14,6 @@ package org.jivesoftware.messenger.audit.spi; ...@@ -14,7 +14,6 @@ package org.jivesoftware.messenger.audit.spi;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.audit.AuditEvent;
import org.jivesoftware.messenger.audit.AuditManager; import org.jivesoftware.messenger.audit.AuditManager;
import org.jivesoftware.messenger.audit.Auditor; import org.jivesoftware.messenger.audit.Auditor;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
...@@ -22,6 +21,8 @@ import org.xmpp.packet.Message; ...@@ -22,6 +21,8 @@ import org.xmpp.packet.Message;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.dom4j.io.XMLWriter; import org.dom4j.io.XMLWriter;
import org.dom4j.Element;
import org.dom4j.DocumentFactory;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
...@@ -32,9 +33,6 @@ import java.util.Queue; ...@@ -32,9 +33,6 @@ import java.util.Queue;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
public class AuditorImpl implements Auditor { public class AuditorImpl implements Auditor {
...@@ -66,17 +64,17 @@ public class AuditorImpl implements Auditor { ...@@ -66,17 +64,17 @@ public class AuditorImpl implements Auditor {
if (auditManager.isEnabled()) { if (auditManager.isEnabled()) {
if (packet instanceof Message) { if (packet instanceof Message) {
if (auditManager.isAuditMessage()) { if (auditManager.isAuditMessage()) {
writePacket(packet, false); writePacket(packet);
} }
} }
else if (packet instanceof Presence) { else if (packet instanceof Presence) {
if (auditManager.isAuditPresence()) { if (auditManager.isAuditPresence()) {
writePacket(packet, false); writePacket(packet);
} }
} }
else if (packet instanceof IQ) { else if (packet instanceof IQ) {
if (auditManager.isAuditIQ()) { if (auditManager.isAuditIQ()) {
writePacket(packet, false); writePacket(packet);
} }
} }
} }
...@@ -91,11 +89,11 @@ public class AuditorImpl implements Auditor { ...@@ -91,11 +89,11 @@ public class AuditorImpl implements Auditor {
} }
private void close() { private void close() {
if (xmlSerializer != null) { if (xmlWriter != null) {
try { try {
xmlSerializer.writeEndElement(); xmlWriter.flush();
xmlSerializer.flush(); xmlWriter.close();
xmlSerializer = null; writer.write("</jive>");
writer.close(); writer.close();
writer = null; writer = null;
} }
...@@ -105,14 +103,14 @@ public class AuditorImpl implements Auditor { ...@@ -105,14 +103,14 @@ public class AuditorImpl implements Auditor {
} }
} }
private void writePacket(Packet packet, boolean dropped) { private void writePacket(Packet packet) {
if (!closed) { if (!closed) {
// Add to the logging queue this new entry that will be saved later // Add to the logging queue this new entry that will be saved later
logQueue.add(new AuditPacket(packet.createCopy(), dropped)); logQueue.add(new AuditPacket(packet.createCopy()));
} }
} }
private void prepareAuditFile() throws IOException, XMLStreamException { private void prepareAuditFile() throws IOException {
if (currentAuditFile == null || currentAuditFile.length() > maxSize) { if (currentAuditFile == null || currentAuditFile.length() > maxSize) {
rotateFiles(); rotateFiles();
} }
...@@ -139,7 +137,7 @@ public class AuditorImpl implements Auditor { ...@@ -139,7 +137,7 @@ public class AuditorImpl implements Auditor {
return logQueue.size(); return logQueue.size();
} }
private void rotateFiles() throws IOException, XMLStreamException { private void rotateFiles() throws IOException {
close(); close();
int i; int i;
// Find the next available log file name // Find the next available log file name
...@@ -173,10 +171,8 @@ public class AuditorImpl implements Auditor { ...@@ -173,10 +171,8 @@ public class AuditorImpl implements Auditor {
} }
writer = new FileWriter(currentAuditFile); writer = new FileWriter(currentAuditFile);
xmlSerializer = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
xmlSerializer.setDefaultNamespace("jabber:client"); xmlWriter = new XMLWriter(writer);
xmlSerializer.writeStartElement("jive", "jive", "http://jivesoftware.org");
xmlSerializer.writeNamespace("jive", "http://jivesoftware.org");
} }
/** /**
...@@ -194,28 +190,27 @@ public class AuditorImpl implements Auditor { ...@@ -194,28 +190,27 @@ public class AuditorImpl implements Auditor {
} }
private void saveQueuedPackets() { private void saveQueuedPackets() {
AuditPacket entry;
int batchSize = logQueue.size(); int batchSize = logQueue.size();
for (int index = 0; index < batchSize; index++) { for (int index = 0; index < batchSize; index++) {
entry = logQueue.poll(); AuditPacket auditPacket = logQueue.poll();
if (entry != null) { if (auditPacket != null) {
try { try {
prepareAuditFile(); prepareAuditFile();
entry.send(xmlSerializer); xmlWriter.write(auditPacket.getElement());
xmlSerializer.flush();
} }
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
logQueue.add(entry); logQueue.add(auditPacket);
}
catch (XMLStreamException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
// Add again the entry to the queue to save it later
logQueue.add(entry);
} }
} }
} }
try {
xmlWriter.flush();
}
catch (IOException ioe) {
}
} }
/** /**
...@@ -225,64 +220,48 @@ public class AuditorImpl implements Auditor { ...@@ -225,64 +220,48 @@ public class AuditorImpl implements Auditor {
* The idea is to wrap every packet that is needed to be audited and then add the * The idea is to wrap every packet that is needed to be audited and then add the
* wrapper to a queue that will be later processed (i.e. saved to the XML file). * wrapper to a queue that will be later processed (i.e. saved to the XML file).
*/ */
private class AuditPacket { private static class AuditPacket {
private Packet packet; private static DocumentFactory docFactory = DocumentFactory.getInstance();
private String streamID;
private String sessionStatus;
private Date timestamp;
private boolean dropped;
public AuditPacket(Packet packet, boolean dropped) { private Element element;
this.packet = packet;
this.dropped = dropped; public AuditPacket(Packet packet) {
this.timestamp = new Date(); element = docFactory.createElement("packet", "http://www.jivesoftware.org");
Session session = SessionManager.getInstance().getSessions(packet.getFrom()); Session session = SessionManager.getInstance().getSession(packet.getFrom());
if (session != null) { if (session != null) {
if (session.getStreamID() != null) { if (session.getStreamID() != null) {
this.streamID = session.getStreamID().toString(); element.addAttribute("streamID", session.getStreamID().toString());
} }
switch (session.getStatus()) { switch (session.getStatus()) {
case Session.STATUS_AUTHENTICATED: case Session.STATUS_AUTHENTICATED:
this.sessionStatus = "auth"; element.addAttribute("status", "auth");
break; break;
case Session.STATUS_CLOSED: case Session.STATUS_CLOSED:
this.sessionStatus = "closed"; element.addAttribute("status", "closed");
break; break;
case Session.STATUS_CONNECTED: case Session.STATUS_CONNECTED:
this.sessionStatus = "connected"; element.addAttribute("status", "connected");
break; break;
case Session.STATUS_STREAMING: case Session.STATUS_STREAMING:
this.sessionStatus = "stream"; element.addAttribute("status", "stream");
break; break;
default: default:
this.sessionStatus = "unknown"; element.addAttribute("status", "unknown");
break; break;
} }
} }
element.addAttribute("timestap", new Date().toString());
element.add(packet.getElement());
} }
public void send(XMLStreamWriter xmlSerializer) { /**
try { * Returns the Element associated with this audit packet.
xmlSerializer.writeStartElement("packet"); *
xmlSerializer.writeDefaultNamespace("http://jivesoftware.org"); * @return the Element.
*/
if (streamID != null) { public Element getElement() {
xmlSerializer.writeAttribute("session", streamID); return element;
}
if (sessionStatus != null) {
xmlSerializer.writeAttribute("status", sessionStatus);
}
xmlSerializer.writeAttribute("timestamp", timestamp.toString());
if (dropped) {
xmlSerializer.writeAttribute("dropped", "true");
}
packet.send(xmlSerializer, 0);
xmlSerializer.writeEndElement();
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
} }
} }
} }
\ No newline at end of file
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