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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Jitsi, 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.util.*;
/**
* The AccountID is an account identifier that, uniquely represents a specific
* user account over a specific protocol. The class needs to be extended by
* every protocol implementation because of its protected
* constructor. The reason why this constructor is protected is mostly avoiding
* confusion and letting people (using the protocol provider service) believe
* that they are the ones who are supposed to instantiate the accountid class.
* <p>
* Every instance of the <tt>ProtocolProviderService</tt>, created through the
* ProtocolProviderFactory is assigned an AccountID instance, that uniquely
* represents it and whose string representation (obtained through the
* getAccountUID() method) can be used for identification of persistently stored
* account details.
* <p>
* Account id's are guaranteed to be different for different accounts and in the
* same time are bound to be equal for multiple installations of the same
* account.
*
* @author Emil Ivov
* @author Lubomir Marinov
*/
public abstract class AccountID
{
/**
* The <tt>Logger</tt> used by the <tt>AccountID</tt> class and its
* instances for logging output.
*/
private static final Logger logger = Logger.getLogger(AccountID.class);
/**
* The protocol display name. In the case of overridden protocol name this
* would be the new name.
*/
private final String protocolDisplayName;
/**
* The real protocol name.
*/
private final String protocolName;
/**
* Allows a specific set of account properties to override a given default
* protocol name (e.g. account registration wizards which want to present a
* well-known protocol name associated with the account that is different
* from the name of the effective protocol).
* <p>
* Note: The logic of the SIP protocol implementation at the time of this
* writing modifies <tt>accountProperties</tt> to contain the default
* protocol name if an override hasn't been defined. Since the desire is to
* enable all account registration wizards to override the protocol name,
* the current implementation places the specified
* <tt>defaultProtocolName</tt> in a similar fashion.
* </p>
*
* @param accountProperties a Map containing any other protocol and
* implementation specific account initialization properties
* @param defaultProtocolName the protocol name to be used in case
* <tt>accountProperties</tt> doesn't provide an overriding value
* @return the protocol name
*/
private static final String getOverriddenProtocolName(
Map<String, String> accountProperties, String defaultProtocolName)
{
String key = ProtocolProviderFactory.PROTOCOL;
String protocolName = accountProperties.get(key);
if ((protocolName == null) && (defaultProtocolName != null))
{
protocolName = defaultProtocolName;
accountProperties.put(key, protocolName);
}
return protocolName;
}
/**
* Contains all implementation specific properties that define the account.
* The exact names of the keys are protocol (and sometimes implementation)
* specific.
* Currently, only String property keys and values will get properly stored.
* If you need something else, please consider converting it through custom
* accessors (get/set) in your implementation.
*/
protected Map<String, String> accountProperties = null;
/**
* A String uniquely identifying the user for this particular account.
*/
private final String userID;
/**
* A String uniquely identifying this account, that can also be used for
* storing and unambiguously retrieving details concerning it.
*/
private final String accountUID;
/**
* The name of the service that defines the context for this account.
*/
private final String serviceName;
/**
* Creates an account id for the specified provider userid and
* accountProperties.
* If account uid exists in account properties, we are loading the account
* and so load its value from there, prevent changing account uid
* when server changed (serviceName has changed).
* @param userID a String that uniquely identifies the user.
* @param accountProperties a Map containing any other protocol and
* implementation specific account initialization properties
* @param protocolName the name of the protocol implemented by the provider
* that this id is meant for.
* @param serviceName the name of the service (e.g. iptel.org, jabber.org,
* icq.com) that this account is registered with.
*/
protected AccountID( String userID,
Map<String, String> accountProperties,
String protocolName,
String serviceName)
{
/*
* Allow account registration wizards to override the default protocol
* name through accountProperties for the purposes of presenting a
* well-known protocol name associated with the account that is
* different from the name of the effective protocol.
*/
this.protocolDisplayName
= getOverriddenProtocolName(accountProperties, protocolName);
this.protocolName = protocolName;
this.userID = userID;
this.accountProperties
= new HashMap<String, String>(accountProperties);
this.serviceName = serviceName;
String existingAccountUID =
accountProperties.get(ProtocolProviderFactory.ACCOUNT_UID);
if(existingAccountUID == null)
{
//create a unique identifier string
this.accountUID
= protocolDisplayName
+ ":"
+ userID
+ "@"
+ ((serviceName == null) ? "" : serviceName);
}
else
{
this.accountUID = existingAccountUID;
}
}
/**
* Returns the user id associated with this account.
*
* @return A String identifying the user inside this particular service.
*/
public String getUserID()
{
return userID;
}
/**
* Returns a name that can be displayed to the user when referring to this
* account.
*
* @return A String identifying the user inside this particular service.
*/
public String getDisplayName()
{
// If the ACCOUNT_DISPLAY_NAME property has been set for this account
// we'll be using it as a display name.
String key = ProtocolProviderFactory.ACCOUNT_DISPLAY_NAME;
String accountDisplayName = accountProperties.get(key);
if (accountDisplayName != null && accountDisplayName.length() > 0)
{
return accountDisplayName;
}
// Otherwise construct a display name.
String returnValue = getUserID();
String protocolName = getProtocolDisplayName();
if (protocolName != null && protocolName.trim().length() > 0)
returnValue += " (" + protocolName + ")";
return returnValue;
}
/**
* Returns the display name of the protocol.
*
* @return the display name of the protocol
*/
public String getProtocolDisplayName()
{
return protocolDisplayName;
}
/**
* Returns the name of the protocol.
*
* @return the name of the protocol
*/
public String getProtocolName()
{
return protocolName;
}
/**
* Returns a String uniquely identifying this account, guaranteed to remain
* the same across multiple installations of the same account and to always
* be unique for differing accounts.
* @return String
*/
public String getAccountUniqueID()
{
return accountUID;
}
/**
* Returns a Map containing protocol and implementation account
* initialization properties.
* @return a Map containing protocol and implementation account
* initialization properties.
*/
public Map<String, String> getAccountProperties()
{
return new HashMap<String, String>(accountProperties);
}
public Object getAccountProperty(Object key)
{
return accountProperties.get(key);
}
public boolean getAccountPropertyBoolean(Object key, boolean defaultValue)
{
String value = getAccountPropertyString(key);
return (value == null) ? defaultValue : Boolean.parseBoolean(value);
}
/**
* Gets the value of a specific property as a signed decimal integer. If the
* specified property key is associated with a value in this
* <tt>AccountID</tt>, the string representation of the value is parsed into
* a signed decimal integer according to the rules of
* {@link Integer#parseInt(String)} . If parsing the value as a signed
* decimal integer fails or there is no value associated with the specified
* property key, <tt>defaultValue</tt> is returned.
*
* @param key the key of the property to get the value of as a
* signed decimal integer
* @param defaultValue the value to be returned if parsing the value of the
* specified property key as a signed decimal integer fails or there is no
* value associated with the specified property key in this
* <tt>AccountID</tt>
* @return the value of the property with the specified key in this
* <tt>AccountID</tt> as a signed decimal integer; <tt>defaultValue</tt> if
* parsing the value of the specified property key fails or no value is
* associated in this <tt>AccountID</tt> with the specified property name
*/
public int getAccountPropertyInt(Object key, int defaultValue)
{
String stringValue = getAccountPropertyString(key);
int intValue = defaultValue;
if ((stringValue != null) && (stringValue.length() > 0))
{
try
{
intValue = Integer.parseInt(stringValue);
}
catch (NumberFormatException ex)
{
logger.error("Failed to parse account property " + key
+ " value " + stringValue + " as an integer", ex);
}
}
return intValue;
}
/**
* Returns the account property string corresponding to the given key.
*
* @param key the key, corresponding to the property string we're looking
* for
* @return the account property string corresponding to the given key
*/
public String getAccountPropertyString(Object key)
{
Object value = getAccountProperty(key);
return (value == null) ? null : value.toString();
}
/**
* Adds a property to the map of properties for this account identifier.
*
* @param key the key of the property
* @param value the property value
*/
public void putAccountProperty(String key, String value)
{
accountProperties.put(key, value);
}
/**
* Removes specified account property.
* @param key the key to remove.
*/
public void removeAccountProperty(String key)
{
accountProperties.remove(key);
}
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <tt>java.util.Hashtable</tt>.
* <p>
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public int hashCode()
{
return (accountUID == null)? 0 : accountUID.hashCode();
}
/**
* Indicates whether some other object is "equal to" this account id.
* <p>
* @param obj the reference object with which to compare.
* @return <tt>true</tt> if this object is the same as the obj
* argument; <tt>false</tt> otherwise.
* @see #hashCode()
* @see java.util.Hashtable
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
return (obj != null)
&& getClass().isInstance(obj)
&& userID.equals(((AccountID)obj).userID);
}
/**
* Returns a string representation of this account id (same as calling
* getAccountUniqueID()).
*
* @return a string representation of this account id.
*/
public String toString()
{
return getAccountUniqueID();
}
/**
* Returns the name of the service that defines the context for this
* account. Often this name would be an sqdn or even an ipaddress but this
* would not always be the case (e.g. p2p providers may return a name that
* does not directly correspond to an IP address or host name).
* <p>
* @return the name of the service that defines the context for this
* account.
*/
public String getService()
{
return this.serviceName;
}
/**
* Returns a string that could be directly used (or easily converted to) an
* address that other users of the protocol can use to communicate with us.
* By default this string is set to userid@servicename. Protocol
* implementors should override it if they'd need it to respect a different
* syntax.
*
* @return a String in the form of userid@service that other protocol users
* should be able to parse into a meaningful address and use it to
* communicate with us.
*/
public String getAccountAddress()
{
String userID = getUserID();
return (userID.indexOf('@') > 0) ? userID
: (userID + "@" + getService());
}
/**
* Indicates if this account is currently enabled.
* @return <tt>true</tt> if this account is enabled, <tt>false</tt> -
* otherwise.
*/
public boolean isEnabled()
{
return !getAccountPropertyBoolean(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED, false);
}
/**
* Set the account properties.
*
* @param accountProperties the properties of the account
*/
public void setAccountProperties(Map<String, String> accountProperties)
{
this.accountProperties = accountProperties;
}
}
|