1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.service.protocol;
import java.util.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
/**
* Represents a default implementation of
* <tt>OperationSetContactCapabilities</tt> which attempts to make it easier for
* implementers to provide complete solutions while focusing on
* implementation-specific functionality.
*
* @param <T> the type of the <tt>ProtocolProviderService</tt> implementation
* providing the <tt>AbstractOperationSetContactCapabilities</tt> implementation
*
* @author Lubomir Marinov
*/
public abstract class AbstractOperationSetContactCapabilities<
T extends ProtocolProviderService>
implements OperationSetContactCapabilities
{
/**
* The <tt>Logger</tt> used by the
* <tt>AbstractOperationSetContactCapabilities</tt> class and its instances
* for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractOperationSetContactCapabilities.class);
/**
* The list of <tt>ContactCapabilitiesListener</tt>s registered to be
* notified about changes in the list of <tt>OperationSet</tt> capabilities
* of <tt>Contact</tt>s.
*/
private final List<ContactCapabilitiesListener> contactCapabilitiesListeners
= new LinkedList<ContactCapabilitiesListener>();
/**
* The <tt>ProtocolProviderService</tt> which provides this
* <tt>OperationSetContactCapabilities</tt>.
*/
protected final T parentProvider;
/**
* Initializes a new <tt>AbstractOperationSetContactCapabilities</tt>
* instance which is to be provided by a specific
* <tt>ProtocolProviderService</tt> implementation.
*
* @param parentProvider the <tt>ProtocolProviderService</tt> implementation
* which will provide the new instance
*/
protected AbstractOperationSetContactCapabilities(T parentProvider)
{
if (parentProvider == null)
throw new NullPointerException("parentProvider");
this.parentProvider = parentProvider;
}
/**
* Registers a specific <tt>ContactCapabilitiesListener</tt> to be notified
* about changes in the list of <tt>OperationSet</tt> capabilities of
* <tt>Contact</tt>s. If the specified <tt>listener</tt> has already been
* registered, adding it again has no effect.
*
* @param listener the <tt>ContactCapabilitiesListener</tt> which is to be
* notified about changes in the list of <tt>OperationSet</tt> capabilities
* of <tt>Contact</tt>s
* @see OperationSetContactCapabilities#addContactCapabilitiesListener(
* ContactCapabilitiesListener)
*/
public void addContactCapabilitiesListener(
ContactCapabilitiesListener listener)
{
if (listener == null)
throw new NullPointerException("listener");
synchronized (contactCapabilitiesListeners)
{
if (!contactCapabilitiesListeners.contains(listener))
contactCapabilitiesListeners.add(listener);
}
}
/**
* Fires a new <tt>ContactCapabilitiesEvent</tt> to notify the registered
* <tt>ContactCapabilitiesListener</tt>s that a specific <tt>Contact</tt>
* has changed its list of <tt>OperationSet</tt> capabilities.
*
* @param sourceContact the <tt>Contact</tt> which is the source/cause of
* the event to be fired
* @param eventID the ID of the event to be fired which indicates the
* specifics of the change of the list of <tt>OperationSet</tt> capabilities
* of the specified <tt>sourceContact</tt> and the details of the event
* @param opSets the new set of operation sets for the given source contact
*/
protected void fireContactCapabilitiesEvent(
Contact sourceContact,
int eventID,
Map<String, ? extends OperationSet> opSets)
{
ContactCapabilitiesListener[] listeners;
synchronized (contactCapabilitiesListeners)
{
listeners
= contactCapabilitiesListeners.toArray(
new ContactCapabilitiesListener[
contactCapabilitiesListeners.size()]);
}
if (listeners.length != 0)
{
ContactCapabilitiesEvent event
= new ContactCapabilitiesEvent(sourceContact, eventID, opSets);
for (ContactCapabilitiesListener listener : listeners)
{
switch (eventID)
{
case ContactCapabilitiesEvent.SUPPORTED_OPERATION_SETS_CHANGED:
listener.supportedOperationSetsChanged(event);
break;
default:
if (logger.isDebugEnabled())
{
logger.debug(
"Cannot fire ContactCapabilitiesEvent with"
+ " unsupported eventID: "
+ eventID);
}
throw new IllegalArgumentException("eventID");
}
}
}
}
/**
* Gets the <tt>OperationSet</tt> corresponding to the specified
* <tt>Class</tt> and supported by the specified <tt>Contact</tt>. If the
* returned value is non-<tt>null</tt>, it indicates that the
* <tt>Contact</tt> is considered by the associated protocol provider to
* possess the <tt>opsetClass</tt> capability. Otherwise, the associated
* protocol provider considers <tt>contact</tt> to not have the
* <tt>opsetClass</tt> capability.
* <tt>AbstractOperationSetContactCapabilities</tt> looks for the name of
* the specified <tt>opsetClass</tt> in the <tt>Map</tt> returned by
* {@link #getSupportedOperationSets(Contact)} and returns the associated
* <tt>OperationSet</tt>. Since the implementation is suboptimal due to the
* temporary <tt>Map</tt> allocations and loopups, extenders are advised to
* override {@link #getOperationSet(Contact, Class, boolean)}.
*
* @param <U> the type extending <tt>OperationSet</tt> for which the
* specified <tt>contact</tt> is to be checked whether it possesses it as a
* capability
* @param contact the <tt>Contact</tt> for which the <tt>opsetClass</tt>
* capability is to be queried
* @param opsetClass the <tt>OperationSet</tt> <tt>Class</tt> for which the
* specified <tt>contact</tt> is to be checked whether it possesses it as a
* capability
* @return the <tt>OperationSet</tt> corresponding to the specified
* <tt>opsetClass</tt> which is considered by the associated protocol
* provider to be possessed as a capability by the specified
* <tt>contact</tt>; otherwise, <tt>null</tt>
* @see OperationSetContactCapabilities#getOperationSet(Contact, Class)
*/
public <U extends OperationSet> U getOperationSet(
Contact contact,
Class<U> opsetClass)
{
return getOperationSet(contact, opsetClass, isOnline(contact));
}
/**
* Gets the <tt>OperationSet</tt> corresponding to the specified
* <tt>Class</tt> and supported by the specified <tt>Contact</tt>. If the
* returned value is non-<tt>null</tt>, it indicates that the
* <tt>Contact</tt> is considered by the associated protocol provider to
* possess the <tt>opsetClass</tt> capability. Otherwise, the associated
* protocol provider considers <tt>contact</tt> to not have the
* <tt>opsetClass</tt> capability.
* <tt>AbstractOperationSetContactCapabilities</tt> looks for the name of
* the specified <tt>opsetClass</tt> in the <tt>Map</tt> returned by
* {@link #getSupportedOperationSets(Contact)} and returns the associated
* <tt>OperationSet</tt>. Since the implementation is suboptimal due to the
* temporary <tt>Map</tt> allocations and loopups, extenders are advised to
* override.
*
* @param <U> the type extending <tt>OperationSet</tt> for which the
* specified <tt>contact</tt> is to be checked whether it possesses it as a
* capability
* @param contact the <tt>Contact</tt> for which the <tt>opsetClass</tt>
* capability is to be queried
* @param opsetClass the <tt>OperationSet</tt> <tt>Class</tt> for which the
* specified <tt>contact</tt> is to be checked whether it possesses it as a
* capability
* @param online <tt>true</tt> if <tt>contact</tt> is online; otherwise,
* <tt>false</tt>
* @return the <tt>OperationSet</tt> corresponding to the specified
* <tt>opsetClass</tt> which is considered by the associated protocol
* provider to be possessed as a capability by the specified
* <tt>contact</tt>; otherwise, <tt>null</tt>
* @see OperationSetContactCapabilities#getOperationSet(Contact, Class)
*/
@SuppressWarnings("unchecked")
protected <U extends OperationSet> U getOperationSet(
Contact contact,
Class<U> opsetClass,
boolean online)
{
Map<String, OperationSet> supportedOperationSets
= getSupportedOperationSets(contact, online);
if (supportedOperationSets != null)
{
OperationSet opset
= supportedOperationSets.get(opsetClass.getName());
if (opsetClass.isInstance(opset))
return (U) opset;
}
return null;
}
/**
* Gets the <tt>OperationSet</tt>s supported by a specific <tt>Contact</tt>.
* The returned <tt>OperationSet</tt>s are considered by the associated
* protocol provider to capabilities possessed by the specified
* <tt>contact</tt>. The default implementation returns the result of
* calling {@link ProtocolProviderService#getSupportedOperationSets()} on
* the associated <tt>ProtocolProviderService</tt> implementation. Extenders
* have to override the default implementation of
* {@link #getSupportedOperationSets(Contact, boolean)} in order to provide
* actual capability detection for the specified <tt>contact</tt>.
*
* @param contact the <tt>Contact</tt> for which the supported
* <tt>OperationSet</tt> capabilities are to be retrieved
* @return a <tt>Map</tt> listing the <tt>OperationSet</tt>s considered by
* the associated protocol provider to be supported by the specified
* <tt>contact</tt> (i.e. to be possessed as capabilities). Each supported
* <tt>OperationSet</tt> capability is represented by a <tt>Map.Entry</tt>
* with key equal to the <tt>OperationSet</tt> class name and value equal to
* the respective <tt>OperationSet</tt> instance
* @see OperationSetContactCapabilities#getSupportedOperationSets(Contact)
*/
public Map<String, OperationSet> getSupportedOperationSets(Contact contact)
{
return getSupportedOperationSets(contact, isOnline(contact));
}
/**
* Gets the <tt>OperationSet</tt>s supported by a specific <tt>Contact</tt>.
* The returned <tt>OperationSet</tt>s are considered by the associated
* protocol provider to capabilities possessed by the specified
* <tt>contact</tt>. The default implementation returns the result of
* calling {@link ProtocolProviderService#getSupportedOperationSets()} on
* the associated <tt>ProtocolProviderService</tt> implementation. Extenders
* have to override the default implementation in order to provide actual
* capability detection for the specified <tt>contact</tt>.
*
* @param contact the <tt>Contact</tt> for which the supported
* <tt>OperationSet</tt> capabilities are to be retrieved
* @param online <tt>true</tt> if <tt>contact</tt> is online; otherwise,
* <tt>false</tt>
* @return a <tt>Map</tt> listing the <tt>OperationSet</tt>s considered by
* the associated protocol provider to be supported by the specified
* <tt>contact</tt> (i.e. to be possessed as capabilities). Each supported
* <tt>OperationSet</tt> capability is represented by a <tt>Map.Entry</tt>
* with key equal to the <tt>OperationSet</tt> class name and value equal to
* the respective <tt>OperationSet</tt> instance
* @see OperationSetContactCapabilities#getSupportedOperationSets(Contact)
*/
protected Map<String, OperationSet> getSupportedOperationSets(
Contact contact,
boolean online)
{
return parentProvider.getSupportedOperationSets();
}
/**
* Determines whether a specific <tt>Contact</tt> is online (in contrast to
* offline).
*
* @param contact the <tt>Contact</tt> which is to be determines whether it
* is online
* @return <tt>true</tt> if the specified <tt>contact</tt> is online;
* otherwise, <tt>false</tt>
*/
protected boolean isOnline(Contact contact)
{
OperationSetPresence opsetPresence
= parentProvider.getOperationSet(OperationSetPresence.class);
if (opsetPresence == null)
{
/*
* Presence is not implemented so we cannot really know and thus
* we'll give it the benefit of the doubt and declare it online.
*/
return true;
}
else
{
PresenceStatus presenceStatus = null;
Throwable exception = null;
try
{
presenceStatus
= opsetPresence.queryContactStatus(contact.getAddress());
}
catch (IllegalArgumentException iaex)
{
exception = iaex;
}
catch (IllegalStateException isex)
{
exception = isex;
}
catch (OperationFailedException ofex)
{
exception = ofex;
}
if (presenceStatus == null)
presenceStatus = contact.getPresenceStatus();
if (presenceStatus == null)
{
if ((exception != null) && logger.isDebugEnabled())
{
logger.debug(
"Failed to query PresenceStatus of Contact "
+ contact,
exception);
}
/*
* For whatever reason the PresenceStatus wasn't retrieved, it's
* a fact that presence was advertised and the contacts wasn't
* reported online.
*/
return false;
}
else
return presenceStatus.isOnline();
}
}
/**
* Unregisters a specific <tt>ContactCapabilitiesListener</tt> to no longer
* be notified about changes in the list of <tt>OperationSet</tt>
* capabilities of <tt>Contact</tt>s. If the specified <tt>listener</tt> has
* already been unregistered or has never been registered, removing it has
* no effect.
*
* @param listener the <tt>ContactCapabilitiesListener</tt> which is to no
* longer be notified about changes in the list of <tt>OperationSet</tt>
* capabilities of <tt>Contact</tt>s
* @see OperationSetContactCapabilities#removeContactCapabilitiesListener(
* ContactCapabilitiesListener)
*/
public void removeContactCapabilitiesListener(
ContactCapabilitiesListener listener)
{
if (listener != null)
{
synchronized (contactCapabilitiesListeners)
{
contactCapabilitiesListeners.remove(listener);
}
}
}
}
|