001: /**
002: * $RCSfile$
003: * $Revision: 1632 $
004: * $Date: 2005-07-15 02:49:00 -0300 (Fri, 15 Jul 2005) $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.audit.spi;
011:
012: import org.jivesoftware.util.JiveGlobals;
013: import org.jivesoftware.openfire.XMPPServer;
014: import org.jivesoftware.openfire.audit.AuditManager;
015: import org.jivesoftware.openfire.audit.Auditor;
016: import org.jivesoftware.openfire.container.BasicModule;
017: import org.jivesoftware.openfire.interceptor.InterceptorManager;
018: import org.jivesoftware.openfire.interceptor.PacketInterceptor;
019: import org.jivesoftware.openfire.session.Session;
020: import org.xmpp.packet.JID;
021: import org.xmpp.packet.Packet;
022:
023: import java.io.File;
024: import java.util.*;
025:
026: /**
027: * Implementation of the AuditManager interface.
028: */
029: public class AuditManagerImpl extends BasicModule implements
030: AuditManager {
031:
032: private boolean enabled;
033: private boolean auditMessage;
034: private boolean auditPresence;
035: private boolean auditIQ;
036: private boolean auditXPath;
037: private List xpath = new LinkedList();
038: private AuditorImpl auditor = null;
039: /**
040: * Max size in bytes that all audit log files may have. When the limit is reached
041: * oldest audit log files will be removed until total size is under the limit.
042: */
043: private int maxTotalSize;
044: /**
045: * Max size in bytes that each audit log file may have. Once the limit has been
046: * reached a new audit file will be created.
047: */
048: private int maxFileSize;
049: /**
050: * Max number of days to keep audit information. Once the limit has been reached
051: * audit files that contain information that exceed the limit will be deleted.
052: */
053: private int maxDays;
054: private int logTimeout;
055: private String logDir;
056: private Collection<String> ignoreList = new ArrayList<String>();
057: private static final int MAX_TOTAL_SIZE = 1000;
058: private static final int MAX_FILE_SIZE = 10;
059: private static final int MAX_DAYS = -1;
060: private static final int DEFAULT_LOG_TIMEOUT = 120000;
061: private AuditorInterceptor interceptor;
062:
063: public AuditManagerImpl() {
064: super ("Audit Manager");
065: }
066:
067: public boolean isEnabled() {
068: return enabled;
069: }
070:
071: public void setEnabled(boolean enabled) {
072: this .enabled = enabled;
073: JiveGlobals.setProperty("xmpp.audit.active", enabled ? "true"
074: : "false");
075: // Add or remove the auditor interceptor depending on the enabled status
076: if (enabled) {
077: InterceptorManager.getInstance()
078: .addInterceptor(interceptor);
079: } else {
080: InterceptorManager.getInstance().removeInterceptor(
081: interceptor);
082: }
083: }
084:
085: public Auditor getAuditor() {
086: if (auditor == null) {
087: throw new IllegalStateException(
088: "Must initialize audit manager first");
089: }
090: return auditor;
091: }
092:
093: public int getMaxTotalSize() {
094: return maxTotalSize;
095: }
096:
097: public void setMaxTotalSize(int size) {
098: maxTotalSize = size;
099: auditor.setMaxValues(maxTotalSize, maxFileSize, maxDays);
100: JiveGlobals.setProperty("xmpp.audit.totalsize", Integer
101: .toString(size));
102: }
103:
104: public int getMaxFileSize() {
105: return maxFileSize;
106: }
107:
108: public void setMaxFileSize(int size) {
109: maxFileSize = size;
110: auditor.setMaxValues(maxTotalSize, maxFileSize, maxDays);
111: JiveGlobals.setProperty("xmpp.audit.filesize", Integer
112: .toString(size));
113: }
114:
115: public int getMaxDays() {
116: return maxDays;
117: }
118:
119: public void setMaxDays(int count) {
120: if (count < -1) {
121: count = -1;
122: }
123: if (count == 0) {
124: count = 1;
125: }
126: maxDays = count;
127: auditor.setMaxValues(maxTotalSize, maxFileSize, maxDays);
128: JiveGlobals.setProperty("xmpp.audit.days", Integer
129: .toString(count));
130: }
131:
132: public int getLogTimeout() {
133: return logTimeout;
134: }
135:
136: public void setLogTimeout(int logTimeout) {
137: this .logTimeout = logTimeout;
138: auditor.setLogTimeout(logTimeout);
139: JiveGlobals.setProperty("xmpp.audit.logtimeout", Integer
140: .toString(logTimeout));
141: }
142:
143: public String getLogDir() {
144: return logDir;
145: }
146:
147: public void setLogDir(String logDir) {
148: this .logDir = logDir;
149: auditor.setLogDir(logDir);
150: JiveGlobals.setProperty("xmpp.audit.logdir", logDir);
151: }
152:
153: public boolean isAuditMessage() {
154: return auditMessage;
155: }
156:
157: public void setAuditMessage(boolean auditMessage) {
158: this .auditMessage = auditMessage;
159: JiveGlobals.setProperty("xmpp.audit.message",
160: auditMessage ? "true" : "false");
161: }
162:
163: public boolean isAuditPresence() {
164: return auditPresence;
165: }
166:
167: public void setAuditPresence(boolean auditPresence) {
168: this .auditPresence = auditPresence;
169: JiveGlobals.setProperty("xmpp.audit.presence",
170: auditPresence ? "true" : "false");
171: }
172:
173: public boolean isAuditIQ() {
174: return auditIQ;
175: }
176:
177: public void setAuditIQ(boolean auditIQ) {
178: this .auditIQ = auditIQ;
179: JiveGlobals.setProperty("xmpp.audit.iq", Boolean
180: .toString(auditIQ));
181: }
182:
183: public boolean isAuditXPath() {
184: return auditXPath;
185: }
186:
187: public void setAuditXPath(boolean auditXPath) {
188: this .auditXPath = auditXPath;
189: JiveGlobals.setProperty("xmpp.audit.xpath", Boolean
190: .toString(auditXPath));
191: }
192:
193: public void addXPath(String xpathExpression) {
194: xpath.add(xpathExpression);
195: saveXPath();
196: }
197:
198: public void removeXPath(String xpathExpression) {
199: xpath.remove(xpathExpression);
200: saveXPath();
201: }
202:
203: private void saveXPath() {
204: String[] filters = new String[xpath.size()];
205: filters = (String[]) xpath.toArray(filters);
206: // TODO: save XPath values!
207: }
208:
209: public Iterator getXPathFilters() {
210: return xpath.iterator();
211: }
212:
213: public void setIgnoreList(Collection<String> usernames) {
214: if (ignoreList.equals(usernames)) {
215: return;
216: }
217: ignoreList = usernames;
218: // Encode the collection
219: StringBuilder ignoreString = new StringBuilder();
220: for (String username : ignoreList) {
221: if (ignoreString.length() == 0) {
222: ignoreString.append(username);
223: } else {
224: ignoreString.append(",").append(username);
225: }
226: }
227: JiveGlobals.setProperty("xmpp.audit.ignore", ignoreString
228: .toString());
229: }
230:
231: public Collection<String> getIgnoreList() {
232: return Collections.unmodifiableCollection(ignoreList);
233: }
234:
235: // #########################################################################
236: // Basic module methods
237: // #########################################################################
238:
239: public void initialize(XMPPServer server) {
240: super .initialize(server);
241: enabled = JiveGlobals.getBooleanProperty("xmpp.audit.active");
242: auditMessage = JiveGlobals
243: .getBooleanProperty("xmpp.audit.message");
244: auditPresence = JiveGlobals
245: .getBooleanProperty("xmpp.audit.presence");
246: auditIQ = JiveGlobals.getBooleanProperty("xmpp.audit.iq");
247: auditXPath = JiveGlobals.getBooleanProperty("xmpp.audit.xpath");
248: // TODO: load xpath values!
249: // String[] filters = context.getProperties("xmpp.audit.filter.xpath");
250: // for (int i = 0; i < filters.length; i++) {
251: // xpath.add(filters[i]);
252: // }
253: maxTotalSize = JiveGlobals.getIntProperty(
254: "xmpp.audit.totalsize", MAX_TOTAL_SIZE);
255: maxFileSize = JiveGlobals.getIntProperty("xmpp.audit.filesize",
256: MAX_FILE_SIZE);
257: maxDays = JiveGlobals.getIntProperty("xmpp.audit.days",
258: MAX_DAYS);
259: logTimeout = JiveGlobals.getIntProperty(
260: "xmpp.audit.logtimeout", DEFAULT_LOG_TIMEOUT);
261: logDir = JiveGlobals.getProperty("xmpp.audit.logdir",
262: JiveGlobals.getHomeDirectory() + File.separator
263: + "logs");
264: String ignoreString = JiveGlobals.getProperty(
265: "xmpp.audit.ignore", "");
266: // Decode the ignore list
267: StringTokenizer tokenizer = new StringTokenizer(ignoreString,
268: ", ");
269: while (tokenizer.hasMoreTokens()) {
270: String username = tokenizer.nextToken();
271: ignoreList.add(username);
272: }
273:
274: auditor = new AuditorImpl(this );
275: auditor.setMaxValues(maxTotalSize, maxFileSize, maxDays);
276: auditor.setLogDir(logDir);
277: auditor.setLogTimeout(logTimeout);
278:
279: interceptor = new AuditorInterceptor();
280: if (enabled) {
281: InterceptorManager.getInstance()
282: .addInterceptor(interceptor);
283: }
284: }
285:
286: public void stop() {
287: if (auditor != null) {
288: auditor.stop();
289: }
290: }
291:
292: private class AuditorInterceptor implements PacketInterceptor {
293:
294: public void interceptPacket(Packet packet, Session session,
295: boolean read, boolean processed) {
296: if (!processed) {
297: // Ignore packets sent or received by users that are present in the ignore list
298: JID from = packet.getFrom();
299: JID to = packet.getTo();
300: if ((from == null || !ignoreList.contains(from
301: .getNode()))
302: && (to == null || !ignoreList.contains(to
303: .getNode()))) {
304: auditor.audit(packet, session);
305: }
306: }
307: }
308: }
309: }
|