KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > interceptor > InterceptorManager


1 /**
2  * $RCSfile: InterceptorManager.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/04/24 08:03:47 $
5  *
6  * Copyright (C) 2005 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.interceptor;
13
14 import org.jivesoftware.messenger.Session;
15 import org.jivesoftware.util.Log;
16 import org.xmpp.packet.Packet;
17
18 import java.util.*;
19 import java.util.concurrent.ConcurrentHashMap JavaDoc;
20 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
21
22 /**
23  * An InterceptorManager manages the list of global interceptors and per-user
24  * interceptors that are invoked before and after packets are read and sent.
25  * If an interceptor is installed for a user then it will receive all packets
26  * sent or received for <b>any</b> connection of that user.<p>
27  *
28  * PacketInterceptors that are invoked before the packet is sent or processed
29  * (when read) may change the original packet or reject the packet by throwing
30  * a {@link PacketRejectedException}. If the interceptor rejects a received packet
31  * then the sender of the packet receive a
32  * {@link org.xmpp.packet.PacketError.Condition#not_allowed not_allowed} error.
33  *
34  * @see PacketInterceptor
35  * @author Gaston Dombiak
36  */

37 public class InterceptorManager {
38
39     private static InterceptorManager instance = new InterceptorManager();
40
41     private List JavaDoc<PacketInterceptor> globalInterceptors =
42             new CopyOnWriteArrayList JavaDoc<PacketInterceptor>();
43     private Map JavaDoc<String JavaDoc, List JavaDoc<PacketInterceptor>> usersInterceptors =
44             new ConcurrentHashMap JavaDoc<String JavaDoc, List JavaDoc<PacketInterceptor>>();
45
46     /**
47      * Returns a singleton instance of InterceptorManager.
48      *
49      * @return an instance of InterceptorManager.
50      */

51     public static InterceptorManager getInstance() {
52         return instance;
53     }
54
55     /**
56      * Returns an unmodifiable list of global packet interceptors. Global
57      * interceptors are applied to all packets read and sent by the server.
58      *
59      * @return an unmodifiable list of the global packet interceptors.
60      */

61     public List JavaDoc<PacketInterceptor> getInterceptors() {
62         return Collections.unmodifiableList(globalInterceptors);
63     }
64
65     /**
66      * Inserts a new interceptor at the end of the list of currently configured
67      * interceptors. This interceptor will be used for all the sent and received packets.
68      *
69      * @param interceptor the interceptor to add.
70      */

71     public void addInterceptor(PacketInterceptor interceptor) {
72         if (interceptor == null) {
73             throw new NullPointerException JavaDoc("Parameter interceptor was null.");
74         }
75         // Remove the interceptor from the list since the position might have changed
76
if (globalInterceptors.contains(interceptor)) {
77             globalInterceptors.remove(interceptor);
78         }
79         globalInterceptors.add(interceptor);
80     }
81
82     /**
83      * Inserts a new interceptor at specified index in the list of currently configured
84      * interceptors. This interceptor will be used for all the sent and received packets.
85      *
86      * @param index the index in the list to insert the new interceptor at.
87      * @param interceptor the interceptor to add.
88      */

89     public void addInterceptor(int index, PacketInterceptor interceptor) {
90         if (index < 0 || (index > globalInterceptors.size())) {
91             throw new IndexOutOfBoundsException JavaDoc("Index " + index + " invalid.");
92         }
93         if (interceptor == null) {
94             throw new NullPointerException JavaDoc("Parameter interceptor was null.");
95         }
96         // Remove the interceptor from the list since the position might have changed
97
if (globalInterceptors.contains(interceptor)) {
98             int oldIndex = globalInterceptors.indexOf(interceptor);
99             if (oldIndex < index) {
100                 index -= 1;
101             }
102             globalInterceptors.remove(interceptor);
103         }
104
105         globalInterceptors.add(index, interceptor);
106     }
107
108     /**
109      * Removes the global interceptor from the list.
110      *
111      * @param interceptor the interceptor to remove.
112      * @return true if the item was present in the list
113      */

114     public boolean removeInterceptor(PacketInterceptor interceptor) {
115         return globalInterceptors.remove(interceptor);
116     }
117
118     /**
119      * Returns an unmodifable list of packet interceptors that are related to the
120      * specified username.
121      *
122      * @param username the name of the user.
123      * @return an unmodifiable list of packet interceptors that are related to
124      * the specified username.
125      */

126     public List JavaDoc<PacketInterceptor> getUserInterceptors(String JavaDoc username) {
127         List JavaDoc<PacketInterceptor> userInterceptors = usersInterceptors.get(username);
128         if (userInterceptors == null) {
129             return Collections.emptyList();
130         }
131         else {
132             return Collections.unmodifiableList(userInterceptors);
133         }
134     }
135
136     /**
137      * Inserts a new interceptor at specified index in the list of currently configured
138      * interceptors for a specific username. This interceptor will be used only when a packet
139      * was sent or received by the specified username.
140      *
141      * @param username the name of the user.
142      * @param index the index in the list to insert the new interceptor at.
143      * @param interceptor the interceptor to add.
144      */

145     public void addUserInterceptor(String JavaDoc username, int index, PacketInterceptor interceptor) {
146         List JavaDoc<PacketInterceptor> userInterceptors = usersInterceptors.get(username);
147         if (userInterceptors == null) {
148             userInterceptors = new CopyOnWriteArrayList JavaDoc<PacketInterceptor>();
149             usersInterceptors.put(username, userInterceptors);
150         }
151         else {
152             if (index < 0 || (index > userInterceptors.size())) {
153                 throw new IndexOutOfBoundsException JavaDoc("Index " + index + " invalid.");
154             }
155             if (interceptor == null) {
156                 throw new NullPointerException JavaDoc("Parameter interceptor was null.");
157             }
158
159             // Remove the interceptor from the list since the position might have changed
160
if (userInterceptors.contains(interceptor)) {
161                 int oldIndex = userInterceptors.indexOf(interceptor);
162                 if (oldIndex < index) {
163                     index -= 1;
164                 }
165                 userInterceptors.remove(interceptor);
166             }
167         }
168         userInterceptors.add(index, interceptor);
169     }
170
171     /**
172      * Removes the interceptor from the list of interceptors that are related to a specific
173      * username.
174      *
175      * @param username the name of the user.
176      * @param interceptor the interceptor to remove.
177      * @return true if the item was present in the list
178      */

179     public boolean removeUserInterceptor(String JavaDoc username, PacketInterceptor interceptor) {
180         boolean answer = false;
181         List JavaDoc<PacketInterceptor> userInterceptors = usersInterceptors.get(username);
182         if (userInterceptors != null) {
183             answer = userInterceptors.remove(interceptor);
184             // Remove the entry for this username if the list is now empty
185
if (userInterceptors.isEmpty()) {
186                 usersInterceptors.remove(username);
187             }
188         }
189         return answer;
190     }
191
192     /**
193      * Invokes all currently-installed interceptors on the specified packet.
194      * All global interceptors will be invoked as well as interceptors that
195      * are related to the address of the session that received or is sending
196      * the packet.<p>
197      *
198      * Interceptors are executed before and after processing an incoming packet
199      * and sending a packet to a user. This means that interceptors are able to alter or
200      * reject packets before they are processed further. If possible, interceptors
201      * should perform their work in a short time so that overall performance is not
202      * compromised.
203      *
204      * @param packet the packet that has been read or is about to be sent.
205      * @param session the session that received the packet or that the packet
206      * will be sent to.
207      * @param read true indicates that the packet was read. When false, the packet
208      * is being sent to a user.
209      * @param processed true if the packet has already processed (incoming or outgoing).
210      * If the packet hasn't already been processed, this flag will be false.
211      * @throws PacketRejectedException if the packet should be prevented from being processed.
212      */

213     public void invokeInterceptors(Packet packet, Session session, boolean read, boolean processed)
214             throws PacketRejectedException
215     {
216         // Invoke the global interceptors for this packet
217
for (PacketInterceptor interceptor : globalInterceptors) {
218             try {
219                 interceptor.interceptPacket(packet, session, read, processed);
220             }
221             catch (PacketRejectedException e) {
222                 if (processed) {
223                     Log.error("Post interceptor cannot reject packet.", e);
224                 }
225                 else {
226                     // Throw this exception since we don't really want to catch it
227
throw e;
228                 }
229             }
230             catch (Exception JavaDoc e) {
231                 Log.error("Error in interceptor", e);
232             }
233         }
234         // Invoke the interceptors that are related to the address of the session
235
String JavaDoc username = session.getAddress().getNode();
236         if (username != null) {
237             Collection<PacketInterceptor> userInterceptors = usersInterceptors.get(username);
238             if (userInterceptors != null && !userInterceptors.isEmpty()) {
239                 for (PacketInterceptor interceptor : userInterceptors) {
240                     try {
241                         interceptor.interceptPacket(packet, session, read, processed);
242                     }
243                     catch (PacketRejectedException e) {
244                         if (processed) {
245                             Log.error("Post interceptor cannot reject packet.", e);
246                         }
247                         else {
248                             // Throw this exception since we don't really want to catch it
249
throw e;
250                         }
251                     }
252                     catch (Exception JavaDoc e) {
253                         Log.error("Error in interceptor", e);
254                     }
255                 }
256             }
257         }
258     }
259 }
Popular Tags