KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > audit > spi > AuditManagerImpl


1 /**
2  * $RCSfile: AuditManagerImpl.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/07/15 05:48:59 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.audit.spi;
13
14 import org.jivesoftware.messenger.Session;
15 import org.jivesoftware.messenger.XMPPServer;
16 import org.jivesoftware.messenger.audit.AuditManager;
17 import org.jivesoftware.messenger.audit.Auditor;
18 import org.jivesoftware.messenger.container.BasicModule;
19 import org.jivesoftware.messenger.interceptor.InterceptorManager;
20 import org.jivesoftware.messenger.interceptor.PacketInterceptor;
21 import org.jivesoftware.util.JiveGlobals;
22 import org.xmpp.packet.Packet;
23 import org.xmpp.packet.JID;
24
25 import java.io.File JavaDoc;
26 import java.util.*;
27
28 /**
29  * Implementation of the AuditManager interface.
30  */

31 public class AuditManagerImpl extends BasicModule implements AuditManager {
32
33     private boolean enabled;
34     private boolean auditMessage;
35     private boolean auditPresence;
36     private boolean auditIQ;
37     private boolean auditXPath;
38     private List xpath = new LinkedList();
39     private AuditorImpl auditor = null;
40     private int maxSize;
41     private int maxCount;
42     private int logTimeout;
43     private String JavaDoc logDir;
44     private Collection<String JavaDoc> ignoreList = new ArrayList<String JavaDoc>();
45     private static final int MAX_FILE_SIZE = 10;
46     private static final int MAX_FILE_COUNT = 10;
47     private static final int DEFAULT_LOG_TIMEOUT = 120000;
48     private AuditorInterceptor interceptor;
49
50     public AuditManagerImpl() {
51         super("Audit Manager");
52     }
53
54     public boolean isEnabled() {
55         return enabled;
56     }
57
58     public void setEnabled(boolean enabled) {
59         this.enabled = enabled;
60         JiveGlobals.setProperty("xmpp.audit.active", enabled ? "true" : "false");
61         // Add or remove the auditor interceptor depending on the enabled status
62
if (enabled) {
63             InterceptorManager.getInstance().addInterceptor(interceptor);
64         }
65         else {
66             InterceptorManager.getInstance().removeInterceptor(interceptor);
67         }
68     }
69
70     public Auditor getAuditor() {
71         if (auditor == null) {
72             throw new IllegalStateException JavaDoc("Must initialize audit manager first");
73         }
74         return auditor;
75     }
76
77     public int getMaxFileSize() {
78         return maxSize;
79     }
80
81     public void setMaxFileSize(int size) {
82         maxSize = size;
83         auditor.setMaxValues(maxSize, maxCount);
84         JiveGlobals.setProperty("xmpp.audit.maxsize", Integer.toString(size));
85     }
86
87     public int getLogTimeout() {
88         return logTimeout;
89     }
90
91     public void setLogTimeout(int logTimeout) {
92         this.logTimeout = logTimeout;
93         auditor.setLogTimeout(logTimeout);
94         JiveGlobals.setProperty("xmpp.audit.logtimeout", Integer.toString(logTimeout));
95     }
96
97     public String JavaDoc getLogDir() {
98         return logDir;
99     }
100
101     public void setLogDir(String JavaDoc logDir) {
102         this.logDir = logDir;
103         auditor.setLogDir(logDir);
104         JiveGlobals.setProperty("xmpp.audit.logdir", logDir);
105     }
106
107     public int getMaxFileCount() {
108         return maxCount;
109     }
110
111     public void setMaxFileCount(int count) {
112         maxCount = count;
113         auditor.setMaxValues(maxSize, maxCount);
114         JiveGlobals.setProperty("xmpp.audit.maxcount", Integer.toString(count));
115     }
116
117     public boolean isAuditMessage() {
118         return auditMessage;
119     }
120
121     public void setAuditMessage(boolean auditMessage) {
122         this.auditMessage = auditMessage;
123         JiveGlobals.setProperty("xmpp.audit.message", auditMessage ? "true" : "false");
124     }
125
126     public boolean isAuditPresence() {
127         return auditPresence;
128     }
129
130     public void setAuditPresence(boolean auditPresence) {
131         this.auditPresence = auditPresence;
132         JiveGlobals.setProperty("xmpp.audit.presence", auditPresence ? "true" : "false");
133     }
134
135     public boolean isAuditIQ() {
136         return auditIQ;
137     }
138
139     public void setAuditIQ(boolean auditIQ) {
140         this.auditIQ = auditIQ;
141         JiveGlobals.setProperty("xmpp.audit.iq", Boolean.toString(auditIQ));
142     }
143
144     public boolean isAuditXPath() {
145         return auditXPath;
146     }
147
148     public void setAuditXPath(boolean auditXPath) {
149         this.auditXPath = auditXPath;
150         JiveGlobals.setProperty("xmpp.audit.xpath", Boolean.toString(auditXPath));
151     }
152
153     public void addXPath(String JavaDoc xpathExpression) {
154         xpath.add(xpathExpression);
155         saveXPath();
156     }
157
158     public void removeXPath(String JavaDoc xpathExpression) {
159         xpath.remove(xpathExpression);
160         saveXPath();
161     }
162
163     private void saveXPath() {
164         String JavaDoc[] filters = new String JavaDoc[xpath.size()];
165         filters = (String JavaDoc[]) xpath.toArray(filters);
166         // TODO: save XPath values!
167
}
168
169     public Iterator getXPathFilters() {
170         return xpath.iterator();
171     }
172
173     public void setIgnoreList(Collection<String JavaDoc> usernames) {
174         if (ignoreList.equals(usernames)) {
175             return;
176         }
177         ignoreList = usernames;
178         // Encode the collection
179
StringBuilder JavaDoc ignoreString = new StringBuilder JavaDoc();
180         for (String JavaDoc username : ignoreList) {
181             if (ignoreString.length() == 0) {
182                 ignoreString.append(username);
183             }
184             else {
185                 ignoreString.append(",").append(username);
186             }
187         }
188         JiveGlobals.setProperty("xmpp.audit.ignore", ignoreString.toString());
189     }
190
191     public Collection<String JavaDoc> getIgnoreList() {
192         return Collections.unmodifiableCollection(ignoreList);
193     }
194
195     // #########################################################################
196
// Basic module methods
197
// #########################################################################
198

199     public void initialize(XMPPServer server) {
200         super.initialize(server);
201         enabled = JiveGlobals.getBooleanProperty("xmpp.audit.active");
202         auditMessage = JiveGlobals.getBooleanProperty("xmpp.audit.message");
203         auditPresence = JiveGlobals.getBooleanProperty("xmpp.audit.presence");
204         auditIQ = JiveGlobals.getBooleanProperty("xmpp.audit.iq");
205         auditXPath = JiveGlobals.getBooleanProperty("xmpp.audit.xpath");
206         // TODO: load xpath values!
207
// String[] filters = context.getProperties("xmpp.audit.filter.xpath");
208
// for (int i = 0; i < filters.length; i++) {
209
// xpath.add(filters[i]);
210
// }
211
maxSize = JiveGlobals.getIntProperty("xmpp.audit.maxsize", MAX_FILE_SIZE);
212         maxCount = JiveGlobals.getIntProperty("xmpp.audit.maxcount", MAX_FILE_COUNT);
213         logTimeout = JiveGlobals.getIntProperty("xmpp.audit.logtimeout", DEFAULT_LOG_TIMEOUT);
214         logDir = JiveGlobals.getProperty("xmpp.audit.logdir", JiveGlobals.getHomeDirectory() +
215                 File.separator + "logs");
216         String JavaDoc ignoreString = JiveGlobals.getProperty("xmpp.audit.ignore", "");
217         // Decode the ignore list
218
StringTokenizer tokenizer = new StringTokenizer(ignoreString, ", ");
219         while (tokenizer.hasMoreTokens()) {
220             String JavaDoc username = tokenizer.nextToken();
221             ignoreList.add(username);
222         }
223
224         auditor = new AuditorImpl(this);
225         auditor.setMaxValues(maxSize, maxCount);
226         auditor.setLogTimeout(logTimeout);
227         auditor.setLogDir(logDir);
228
229         interceptor = new AuditorInterceptor();
230         if (enabled) {
231             InterceptorManager.getInstance().addInterceptor(interceptor);
232         }
233     }
234
235     public void stop() {
236         if (auditor != null) {
237             auditor.stop();
238         }
239     }
240
241     private class AuditorInterceptor implements PacketInterceptor {
242
243         public void interceptPacket(Packet packet, Session session, boolean read, boolean processed) {
244             if (!processed) {
245                 // Ignore packets sent or received by users that are present in the ignore list
246
JID from = packet.getFrom();
247                 JID to = packet.getTo();
248                 if ((from == null || !ignoreList.contains(from.getNode())) &&
249                         (to == null || !ignoreList.contains(to.getNode()))) {
250                     auditor.audit(packet, session);
251                 }
252             }
253         }
254     }
255 }
256
Popular Tags