📄 auditorimpl.java
字号:
/**
* $RCSfile$
* $Revision: 3186 $
* $Date: 2005-12-11 00:07:52 -0300 (Sun, 11 Dec 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.audit.spi;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.jivesoftware.util.*;
import org.jivesoftware.wildfire.Session;
import org.jivesoftware.wildfire.audit.AuditManager;
import org.jivesoftware.wildfire.audit.Auditor;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
import java.io.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class AuditorImpl implements Auditor {
private AuditManager auditManager;
private File currentAuditFile;
private Writer writer;
private org.jivesoftware.util.XMLWriter xmlWriter;
/**
* Limit date used to detect when we need to rollover files. This date will be
* configured as the last second of the day.
*/
private Date currentDateLimit;
/**
* Max size in bytes that all audit log files may have. When the limit is reached
* oldest audit log files will be removed until total size is under the limit.
*/
private int maxTotalSize;
/**
* Max size in bytes that each audit log file may have. Once the limit has been
* reached a new audit file will be created.
*/
private int maxFileSize;
/**
* Max number of days to keep audit information. Once the limit has been reached
* audit files that contain information that exceed the limit will be deleted.
*/
private int maxDays;
/**
* Flag that indicates if packets can still be accepted to be saved to the audit log.
*/
private boolean closed = false;
/**
* Directoty (absolute path) where the audit files will be saved.
*/
private String logDir;
/**
* File (or better say directory) of the folder that contains the audit logs.
*/
private File baseFolder;
/**
* Queue that holds the audited packets that will be later saved to an XML file.
*/
private BlockingQueue<AuditPacket> logQueue = new LinkedBlockingQueue<AuditPacket>();
/**
* Timer to save queued logs to the XML file.
*/
private Timer timer = new Timer("Auditor");
private SaveQueuedPacketsTask saveQueuedPacketsTask;
private FastDateFormat dateFormat;
public AuditorImpl(AuditManager manager) {
auditManager = manager;
dateFormat = FastDateFormat.getInstance("yyyyMMdd", TimeZone.getTimeZone("UTC"));
}
protected void setMaxValues(int totalSize, int fileSize, int days) {
maxTotalSize = totalSize * 1024*1024;
maxFileSize = fileSize * 1024*1024;
maxDays = days;
}
public void setLogTimeout(int logTimeout) {
// Cancel any existing task because the timeout has changed
if (saveQueuedPacketsTask != null) {
saveQueuedPacketsTask.cancel();
}
// Create a new task and schedule it with the new timeout
saveQueuedPacketsTask = new SaveQueuedPacketsTask();
timer.schedule(saveQueuedPacketsTask, logTimeout, logTimeout);
}
public void setLogDir(String logDir) {
this.logDir = logDir;
// Create and catch file of the base folder that will contain audit files
baseFolder = new File(logDir);
// Create the folder if it does not exist
if (!baseFolder.exists()) {
baseFolder.mkdir();
}
}
public int getQueuedPacketsNumber() {
return logQueue.size();
}
public void audit(Packet packet, Session session) {
if (auditManager.isEnabled()) {
if (packet instanceof Message) {
if (auditManager.isAuditMessage()) {
writePacket(packet, session);
}
}
else if (packet instanceof Presence) {
if (auditManager.isAuditPresence()) {
writePacket(packet, session);
}
}
else if (packet instanceof IQ) {
if (auditManager.isAuditIQ()) {
writePacket(packet, session);
}
}
}
}
private void writePacket(Packet packet, Session session) {
if (!closed) {
// Add to the logging queue this new entry that will be saved later
logQueue.add(new AuditPacket(packet.createCopy(), session));
}
}
public void stop() {
// Stop queuing packets since we are being stopped
closed = true;
// Stop the scheduled task for saving queued packets to the XML file
timer.cancel();
// Save all remaining queued packets to the XML file
saveQueuedPackets();
close();
}
private void close() {
if (xmlWriter != null) {
try {
xmlWriter.flush();
writer.write("</jive>");
xmlWriter.close();
writer = null;
xmlWriter = null;
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
private void prepareAuditFile(Date auditDate) throws IOException {
ensureMaxTotalSize();
// Rotate file if: we just started, current file size exceeded limit or date has changed
if (currentAuditFile == null || currentAuditFile.length() > maxFileSize ||
xmlWriter == null || currentDateLimit == null || auditDate.after(currentDateLimit))
{
createAuditFile(auditDate);
}
}
/**
* Ensures that max total size limit is not exceeded. If total size of audit files
* exceed the limit then oldest audit files will be removed until total size does
* not exceed limit.
*/
private void ensureMaxTotalSize() {
// Get list of existing audit files
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("jive.audit-") && name.endsWith(".log");
}
};
File[] files = baseFolder.listFiles(filter);
long totalLength = 0;
for (File file : files) {
totalLength = totalLength + file.length();
}
// Check if total size has been exceeded
if (totalLength > maxTotalSize) {
// Sort files by name (chronological order)
List<File> sortedFiles = new ArrayList<File>(Arrays.asList(files));
Collections.sort(sortedFiles, new Comparator() {
public int compare(Object o1, Object o2) {
return ((File)o1).getName().compareTo(((File)o2).getName());
}
});
// Delete as many old files as required to be under the limit
while (totalLength > maxTotalSize && !sortedFiles.isEmpty()) {
File fileToDelete = sortedFiles.remove(0);
totalLength = totalLength - fileToDelete.length();
if (fileToDelete.equals(currentAuditFile)) {
// Close current file
close();
}
// Delete oldest file
fileToDelete.delete();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -