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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.service.protocol;
import java.util.*;
import net.java.sip.communicator.service.credentialsstorage.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.Base64; //disambiguate from java.util.Base64
import org.jitsi.service.configuration.*;
import org.osgi.framework.*;
/**
* Represents an implementation of <tt>AccountManager</tt> which loads the
* accounts in a separate thread.
*
* @author Lyubomir Marinov
* @author Yana Stamcheva
*/
public class AccountManager
{
/**
* The delay in milliseconds the background <tt>Thread</tt> loading the
* stored accounts should wait before dying so that it doesn't get recreated
* for each <tt>ProtocolProviderFactory</tt> registration.
*/
private static final long LOAD_STORED_ACCOUNTS_TIMEOUT = 30000;
/**
* The <tt>BundleContext</tt> this service is registered in.
*/
private final BundleContext bundleContext;
/**
* The <tt>AccountManagerListener</tt>s currently interested in the
* events fired by this manager.
*/
private final List<AccountManagerListener> listeners =
new LinkedList<AccountManagerListener>();
/**
* The queue of <tt>ProtocolProviderFactory</tt> services awaiting their
* stored accounts to be loaded.
*/
private final Queue<ProtocolProviderFactory> loadStoredAccountsQueue =
new LinkedList<ProtocolProviderFactory>();
/**
* The <tt>Thread</tt> loading the stored accounts of the
* <tt>ProtocolProviderFactory</tt> services waiting in
* {@link #loadStoredAccountsQueue}.
*/
private Thread loadStoredAccountsThread;
/**
* The <tt>Logger</tt> used by this <tt>AccountManagerImpl</tt> instance for
* logging output.
*/
private final Logger logger = Logger.getLogger(AccountManager.class);
/**
* The list of <tt>AccountID</tt>s, corresponding to all stored accounts.
*/
private final Vector<AccountID> storedAccounts = new Vector<AccountID>();
/**
* The prefix of the account unique identifier.
*/
private static final String ACCOUNT_UID_PREFIX = "acc";
/**
* Initializes a new <tt>AccountManagerImpl</tt> instance loaded in a
* specific <tt>BundleContext</tt> (in which the caller will usually
* later register it).
*
* @param bundleContext the <tt>BundleContext</tt> in which the new
* instance is loaded (and in which the caller will usually later
* register it as a service)
*/
public AccountManager(BundleContext bundleContext)
{
this.bundleContext = bundleContext;
this.bundleContext.addServiceListener(new ServiceListener()
{
public void serviceChanged(ServiceEvent serviceEvent)
{
AccountManager.this.serviceChanged(serviceEvent);
}
});
}
/**
* Implements AccountManager#addListener(AccountManagerListener).
* @param listener the <tt>AccountManagerListener</tt> to add
*/
public void addListener(AccountManagerListener listener)
{
synchronized (listeners)
{
if (!listeners.contains(listener))
listeners.add(listener);
}
}
/**
* Loads the accounts stored for a specific
* <tt>ProtocolProviderFactory</tt>.
*
* @param factory the <tt>ProtocolProviderFactory</tt> to load the
* stored accounts of
*/
private void doLoadStoredAccounts(ProtocolProviderFactory factory)
{
ConfigurationService configService
= ProtocolProviderActivator.getConfigurationService();
String factoryPackage = getFactoryImplPackageName(factory);
List<String> accounts
= configService.getPropertyNamesByPrefix(factoryPackage, true);
if (logger.isDebugEnabled())
logger.debug("Discovered " + accounts.size() + " stored "
+ factoryPackage + " accounts");
for (Iterator<String> storedAccountIter = accounts.iterator();
storedAccountIter.hasNext();)
{
String storedAccount = storedAccountIter.next();
// If the property is not related to an account we skip it.
int dotIndex = storedAccount.lastIndexOf(".");
if (!storedAccount.substring(dotIndex + 1)
.startsWith(ACCOUNT_UID_PREFIX))
continue;
if (logger.isDebugEnabled())
logger.debug("Loading account " + storedAccount);
List<String> storedAccountProperties =
configService.getPropertyNamesByPrefix(storedAccount, false);
Map<String, String> accountProperties =
new Hashtable<String, String>();
boolean disabled = false;
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
int prefLen = storedAccount.length() + 1;
for (Iterator<String> storedAccountPropertyIter
= storedAccountProperties.iterator();
storedAccountPropertyIter.hasNext();)
{
String property = storedAccountPropertyIter.next();
String value = configService.getString(property);
//strip the package prefix
if(prefLen > property.length())
continue;
property = property.substring(prefLen);
if (ProtocolProviderFactory.IS_ACCOUNT_DISABLED.equals(property))
disabled = Boolean.parseBoolean(value);
// Decode passwords.
else if (ProtocolProviderFactory.PASSWORD.equals(property)
&& !credentialsStorage.isStoredEncrypted(storedAccount))
{
if ((value != null) && value.length() != 0)
{
/*
* TODO Converting byte[] to String using the platform's
* default charset may result in an invalid password.
*/
value = new String(Base64.decode(value));
}
}
if (value != null)
accountProperties.put(property, value);
}
try
{
AccountID accountID = factory.createAccount(accountProperties);
// If for some reason the account id is not created we move to
// the next account.
if (accountID == null)
continue;
synchronized (storedAccounts)
{
storedAccounts.add(accountID);
}
if (!disabled)
factory.loadAccount(accountID);
}
catch (Exception ex)
{
/*
* Swallow the exception in order to prevent a single account
* from halting the loading of subsequent accounts.
*/
logger.error("Failed to load account " + accountProperties, ex);
}
catch (ExceptionInInitializerError ex)
{
// In case we fail to instantiate the ProtocolProviderService.
logger.error(
"Failed to create account service instance for account "
+ accountProperties, ex);
}
}
}
/**
* Notifies the registered {@link #listeners} that the stored accounts of a
* specific <tt>ProtocolProviderFactory</tt> have just been loaded.
*
* @param factory the <tt>ProtocolProviderFactory</tt> which had its
* stored accounts just loaded
*/
private void fireStoredAccountsLoaded(ProtocolProviderFactory factory)
{
AccountManagerListener[] listeners;
synchronized (this.listeners)
{
listeners =
this.listeners
.toArray(new AccountManagerListener[this.listeners.size()]);
}
int listenerCount = listeners.length;
if (listenerCount > 0)
{
AccountManagerEvent event =
new AccountManagerEvent(this,
AccountManagerEvent.STORED_ACCOUNTS_LOADED, factory);
for (int listenerIndex = 0;
listenerIndex < listenerCount; listenerIndex++)
{
listeners[listenerIndex].handleAccountManagerEvent(event);
}
}
}
/**
* Returns the package name of the <tt>factory</tt>.
* @param factory the factory which package will be returned.
* @return the package name of the <tt>factory</tt>.
*/
public String getFactoryImplPackageName(ProtocolProviderFactory factory)
{
String className = factory.getClass().getName();
return className.substring(0, className.lastIndexOf('.'));
}
/**
* Check for stored accounts for the supplied <tt>protocolName</tt>.
* @param protocolName the protocol name to check for
* @param includeHidden whether to include hidden providers
* @return <tt>true</tt> if there is any account stored in configuration
* service with <tt>protocolName</tt>, <tt>false</tt> otherwise.
*/
public boolean hasStoredAccounts(String protocolName, boolean includeHidden)
{
return hasStoredAccount(protocolName, includeHidden, null);
}
/**
* Checks whether a stored account with <tt>userID</tt> is stored
* in configuration.
*
* @param protocolName the protocol name
* @param includeHidden whether to check hidden providers
* @param userID the user id to check.
* @return <tt>true</tt> if there is any account stored in configuration
* service with <tt>protocolName</tt> and <tt>userID</tt>,
* <tt>false</tt> otherwise.
*/
public boolean hasStoredAccount(String protocolName,
boolean includeHidden,
String userID)
{
Collection<ServiceReference<ProtocolProviderFactory>> factoryRefs
= ServiceUtils.getServiceReferences(
bundleContext,
ProtocolProviderFactory.class);
boolean hasStoredAccounts = false;
if (!factoryRefs.isEmpty())
{
ConfigurationService configService
= ProtocolProviderActivator.getConfigurationService();
for (ServiceReference<ProtocolProviderFactory> factoryRef
: factoryRefs)
{
ProtocolProviderFactory factory
= bundleContext.getService(factoryRef);
if ((protocolName != null)
&& !protocolName.equals(factory.getProtocolName()))
{
continue;
}
String factoryPackage = getFactoryImplPackageName(factory);
List<String> storedAccounts
= configService
.getPropertyNamesByPrefix(factoryPackage + ".acc",
false);
/* Ignore the hidden accounts. */
for (Iterator<String> storedAccountIter =
storedAccounts.iterator(); storedAccountIter.hasNext();)
{
String storedAccount = storedAccountIter.next();
List<String> storedAccountProperties =
configService.getPropertyNamesByPrefix(storedAccount,
true);
boolean hidden = false;
String accountUserID = null;
if (!includeHidden || userID != null)
{
for (Iterator<String> storedAccountPropertyIter =
storedAccountProperties.iterator();
storedAccountPropertyIter.hasNext();)
{
String property = storedAccountPropertyIter.next();
String value = configService.getString(property);
property = stripPackagePrefix(property);
if (ProtocolProviderFactory.IS_PROTOCOL_HIDDEN
.equals(property))
{
hidden = (value != null);
}
else if (ProtocolProviderFactory.USER_ID
.equals(property))
{
accountUserID = value;
}
}
}
if (includeHidden || !hidden)
{
if(accountUserID != null
&& userID != null
&& userID.equals(accountUserID))
{
hasStoredAccounts = true;
break;
}
else if(userID == null)
{
hasStoredAccounts = true;
break;
}
}
}
if (hasStoredAccounts || (protocolName != null))
{
break;
}
}
}
return hasStoredAccounts;
}
/**
* Searches for stored account with <tt>uid</tt> in stored
* configuration. The <tt>uid</tt> is the one generated when creating
* accounts with prefix <tt>ACCOUNT_UID_PREFIX</tt>.
*
* @return <tt>AccountID</tt> if there is any account stored in configuration
* service with <tt>uid</tt>,
* <tt>null</tt> otherwise.
*/
public AccountID findAccountID(String uid)
{
Collection<ServiceReference<ProtocolProviderFactory>> factoryRefs
= ServiceUtils.getServiceReferences(
bundleContext,
ProtocolProviderFactory.class);
if (!factoryRefs.isEmpty())
{
ConfigurationService configService
= ProtocolProviderActivator.getConfigurationService();
for (ServiceReference<ProtocolProviderFactory> factoryRef
: factoryRefs)
{
ProtocolProviderFactory factory
= bundleContext.getService(factoryRef);
String factoryPackage = getFactoryImplPackageName(factory);
List<String> storedAccountsProps
= configService
.getPropertyNamesByPrefix(factoryPackage, true);
for (Iterator<String> storedAccountIter =
storedAccountsProps.iterator();
storedAccountIter.hasNext();)
{
String storedAccount = storedAccountIter.next();
if(!storedAccount.endsWith(uid))
continue;
String accountUID = configService.getString(
storedAccount //node id
+ "." + ProtocolProviderFactory.ACCOUNT_UID);// propname
for(AccountID acc : storedAccounts)
{
if(acc.getAccountUniqueID().equals(accountUID))
return acc;
}
}
}
}
return null;
}
/**
* Loads the accounts stored for a specific
* <tt>ProtocolProviderFactory</tt> and notifies the registered
* {@link #listeners} that the stored accounts of the specified
* <tt>factory</tt> have just been loaded
*
* @param factory the <tt>ProtocolProviderFactory</tt> to load the
* stored accounts of
*/
private void loadStoredAccounts(ProtocolProviderFactory factory)
{
doLoadStoredAccounts(factory);
fireStoredAccountsLoaded(factory);
}
/**
* Notifies this manager that a specific
* <tt>ProtocolProviderFactory</tt> has been registered as a service.
* The current implementation queues the specified <tt>factory</tt> to
* have its stored accounts as soon as possible.
*
* @param factory the <tt>ProtocolProviderFactory</tt> which has been
* registered as a service.
*/
private void protocolProviderFactoryRegistered(
ProtocolProviderFactory factory)
{
queueLoadStoredAccounts(factory);
}
/**
* Queues a specific <tt>ProtocolProviderFactory</tt> to have its stored
* accounts loaded as soon as possible.
*
* @param factory the <tt>ProtocolProviderFactory</tt> to be queued for
* loading its stored accounts as soon as possible
*/
private void queueLoadStoredAccounts(ProtocolProviderFactory factory)
{
synchronized (loadStoredAccountsQueue)
{
loadStoredAccountsQueue.add(factory);
loadStoredAccountsQueue.notifyAll();
if (loadStoredAccountsThread == null)
{
loadStoredAccountsThread = new Thread()
{
@Override
public void run()
{
runInLoadStoredAccountsThread();
}
};
loadStoredAccountsThread.setDaemon(true);
loadStoredAccountsThread.setName(
"AccountManager.loadStoredAccounts");
loadStoredAccountsThread.start();
}
}
}
/**
* Implements AccountManager#removeListener(AccountManagerListener).
* @param listener the <tt>AccountManagerListener</tt> to remove
*/
public void removeListener(AccountManagerListener listener)
{
synchronized (listeners)
{
listeners.remove(listener);
}
}
/**
* Running in {@link #loadStoredAccountsThread}, loads the stored accounts
* of the <tt>ProtocolProviderFactory</tt> services waiting in
* {@link #loadStoredAccountsQueue}
*/
private void runInLoadStoredAccountsThread()
{
boolean interrupted = false;
while (!interrupted)
{
try
{
ProtocolProviderFactory factory;
synchronized (loadStoredAccountsQueue)
{
factory = loadStoredAccountsQueue.poll();
if (factory == null)
{
/*
* Technically, we should be handing spurious wakeups.
* However, we cannot check the condition in a queue.
* Anyway, we just want to keep this Thread alive long
* enough to allow it to not be re-created multiple
* times and not handing a spurious wakeup will just
* cause such an inconvenience.
*/
try
{
loadStoredAccountsQueue
.wait(LOAD_STORED_ACCOUNTS_TIMEOUT);
}
catch (InterruptedException ex)
{
logger
.warn(
"The loading of the stored accounts has"
+ " been interrupted",
ex);
interrupted = true;
break;
}
factory = loadStoredAccountsQueue.poll();
}
if (factory != null)
loadStoredAccountsQueue.notifyAll();
}
if (factory != null)
{
try
{
loadStoredAccounts(factory);
}
catch (Exception ex)
{
/*
* Swallow the exception in order to prevent a single
* factory from halting the loading of subsequent
* factories.
*/
logger.error("Failed to load accounts for " + factory,
ex);
}
}
}
finally
{
synchronized (loadStoredAccountsQueue)
{
if (!interrupted && (loadStoredAccountsQueue.size() <= 0))
{
if (loadStoredAccountsThread == Thread.currentThread())
{
loadStoredAccountsThread = null;
loadStoredAccountsQueue.notifyAll();
}
break;
}
}
}
}
}
/**
* Notifies this manager that an OSGi service has changed. The current
* implementation tracks the registrations of
* <tt>ProtocolProviderFactory</tt> services in order to queue them for
* loading their stored accounts.
*
* @param serviceEvent the <tt>ServiceEvent</tt> containing the event
* data
*/
private void serviceChanged(ServiceEvent serviceEvent)
{
switch (serviceEvent.getType())
{
case ServiceEvent.REGISTERED:
Object service
= bundleContext.getService(serviceEvent.getServiceReference());
if (service instanceof ProtocolProviderFactory)
{
protocolProviderFactoryRegistered(
(ProtocolProviderFactory) service);
}
break;
default:
break;
}
}
/**
* Stores an account represented in the form of an <tt>AccountID</tt>
* created by a specific <tt>ProtocolProviderFactory</tt>.
*
* @param factory the <tt>ProtocolProviderFactory</tt> which created the
* account to be stored
* @param accountID the account in the form of <tt>AccountID</tt> to be
* stored
* @throws OperationFailedException if anything goes wrong while storing the
* account
*/
public void storeAccount(
ProtocolProviderFactory factory,
AccountID accountID)
throws OperationFailedException
{
synchronized (storedAccounts)
{
if (!storedAccounts.contains(accountID))
storedAccounts.add(accountID);
}
ConfigurationService configurationService
= ProtocolProviderActivator.getConfigurationService();
String factoryPackage = getFactoryImplPackageName(factory);
String accountNodeName
= getAccountNodeName( factory,
accountID.getAccountUniqueID() );
Map<String, Object> configurationProperties
= new HashMap<String, Object>();
// Create a unique node name of the properties node that will contain
// this account's properties.
if (accountNodeName == null)
{
accountNodeName
= ACCOUNT_UID_PREFIX + Long.toString(System.currentTimeMillis());
// set a value for the persistent node so that we could later
// retrieve it as a property
configurationProperties.put(
factoryPackage /* prefix */ + "." + accountNodeName,
accountNodeName);
// register the account in the configuration service.
// we register all the properties in the following hierarchy
//net.java.sip.communicator.impl.protocol.PROTO_NAME.ACC_ID.PROP_NAME
configurationProperties.put(factoryPackage// prefix
+ "." + accountNodeName // node name for the account id
+ "." + ProtocolProviderFactory.ACCOUNT_UID, // propname
accountID.getAccountUniqueID()); // value
}
// store the rest of the properties
Map<String, String> accountProperties = accountID.getAccountProperties();
for (Map.Entry<String, String> entry : accountProperties.entrySet())
{
String property = entry.getKey();
String value = entry.getValue();
String secureStorePrefix = null;
// If the property is a password, store it securely.
if (property.equals(ProtocolProviderFactory.PASSWORD))
{
String accountPrefix = factoryPackage + "." + accountNodeName;
secureStorePrefix = accountPrefix;
}
else if(property.endsWith("." + ProtocolProviderFactory.PASSWORD))
{
secureStorePrefix = factoryPackage + "." + accountNodeName +
"." + property.substring(0, property.lastIndexOf("."));
}
if(secureStorePrefix != null)
{
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
// encrypt and store
if ((value != null)
&& (value.length() != 0)
&& !credentialsStorage.storePassword(
secureStorePrefix,
value))
{
throw
new OperationFailedException(
"CredentialsStorageService failed to"
+ " storePassword",
OperationFailedException.GENERAL_ERROR);
}
}
else
{
configurationProperties.put(
factoryPackage // prefix
+ "." + accountNodeName // a unique node name for the account id
+ "." + property, // propname
value); // value
}
}
// clear the password if missing property, modification can request
// password delete
if(!accountProperties.containsKey(ProtocolProviderFactory.PASSWORD)
&& // And only if it's not stored already in encrypted form.
// Account registration object clears also this property
// in order to forget the password
!configurationProperties.containsKey(
factoryPackage+"."+accountNodeName+".ENCRYPTED_PASSWORD"))
{
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
credentialsStorage.removePassword(
factoryPackage + "." + accountNodeName);
}
if (configurationProperties.size() > 0)
configurationService.setProperties(configurationProperties);
if (logger.isDebugEnabled())
logger.debug("Stored account for id " + accountID.getAccountUniqueID()
+ " for package " + factoryPackage);
}
/**
* Gets account node name under which account configuration properties are
* stored.
*
* @param factory account's protocol provider factory
* @param accountUID account for which the prefix will be returned
* @return configuration prefix for given <tt>accountID</tt> if exists or
* <tt>null</tt> otherwise
*/
public String getAccountNodeName( ProtocolProviderFactory factory,
String accountUID )
{
ConfigurationService configurationService
= ProtocolProviderActivator.getConfigurationService();
String factoryPackage = getFactoryImplPackageName(factory);
// First check if such accountID already exists in the configuration.
List<String> storedAccounts =
configurationService.getPropertyNamesByPrefix(factoryPackage, true);
String accountNodeName = null;
for (Iterator<String> storedAccountIter = storedAccounts.iterator();
storedAccountIter.hasNext();)
{
String storedAccount = storedAccountIter.next();
// If the property is not related to an account we skip it.
int dotIndex = storedAccount.lastIndexOf(".");
if (!storedAccount.substring(dotIndex + 1)
.startsWith(ACCOUNT_UID_PREFIX))
continue;
String storedAccountUID
= configurationService.getString(
storedAccount + "." + ProtocolProviderFactory.ACCOUNT_UID);
if(storedAccountUID == null)
continue;
if (storedAccountUID.equals(accountUID))
accountNodeName = configurationService.getString(storedAccount);
}
return accountNodeName;
}
/**
* Removes the account with <tt>accountID</tt> from the set of accounts
* that are persistently stored inside the configuration service.
*
* @param factory the <tt>ProtocolProviderFactory</tt> which created the
* account to be stored
* @param accountID the AccountID of the account to remove.
* @return true if an account has been removed and false otherwise.
*/
public boolean removeStoredAccount(ProtocolProviderFactory factory,
AccountID accountID)
{
synchronized (storedAccounts)
{
if (storedAccounts.contains(accountID))
storedAccounts.remove(accountID);
}
/*
* We're already doing it in #unloadAccount(AccountID) - we're figuring
* out the ProtocolProviderFactory by the AccountID.
*/
if (factory == null)
{
factory
= ProtocolProviderActivator.getProtocolProviderFactory(
accountID.getProtocolName());
}
String factoryPackage = getFactoryImplPackageName(factory);
// remove the stored password explicitly using credentials service
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
String accountPrefix =
ProtocolProviderFactory.findAccountPrefix(bundleContext, accountID,
factoryPackage);
credentialsStorage.removePassword(accountPrefix);
ConfigurationService configurationService
= ServiceUtils.getService(
bundleContext,
ConfigurationService.class);
//first retrieve all accounts that we've registered
List<String> storedAccounts
= configurationService.getPropertyNamesByPrefix(
factoryPackage, true);
//find an account with the corresponding id.
for (String accountRootPropertyName : storedAccounts)
{
//unregister the account in the configuration service.
//all the properties must have been registered in the following
//hierarchy:
//net.java.sip.communicator.impl.protocol.PROTO_NAME.ACC_ID.PROP_NAME
String accountUID = configurationService.getString(
accountRootPropertyName //node id
+ "." + ProtocolProviderFactory.ACCOUNT_UID); // propname
if (accountID.getAccountUniqueID().equals(accountUID))
{
//retrieve the names of all properties registered for the
//current account.
List<String> accountPropertyNames
= configurationService.getPropertyNamesByPrefix(
accountRootPropertyName, false);
//set all account properties to null in order to remove them.
for (String propName : accountPropertyNames)
configurationService.setProperty(propName, null);
//and now remove the parent too.
configurationService.setProperty(accountRootPropertyName, null);
return true;
}
}
return false;
}
/**
* Removes all accounts which have been persistently stored.
*
* @see #removeStoredAccount(ProtocolProviderFactory, AccountID)
*/
public void removeStoredAccounts()
{
synchronized (loadStoredAccountsQueue)
{
/*
* Wait for the Thread which loads the stored account to complete so
* that we can be sure later on that it will not load a stored
* account while we are deleting it or another one for that matter.
*/
boolean interrupted = false;
while (loadStoredAccountsThread != null)
try
{
loadStoredAccountsQueue.wait(LOAD_STORED_ACCOUNTS_TIMEOUT);
}
catch (InterruptedException ie)
{
interrupted = true;
}
if (interrupted)
Thread.currentThread().interrupt();
synchronized (this.storedAccounts)
{
AccountID[] storedAccounts
= this.storedAccounts.toArray(
new AccountID[this.storedAccounts.size()]);
for (AccountID storedAccount : storedAccounts)
{
ProtocolProviderFactory ppf
= ProtocolProviderActivator.getProtocolProviderFactory(
storedAccount.getProtocolName());
if (ppf != null)
ppf.uninstallAccount(storedAccount);
}
}
}
}
/**
* Returns an <tt>Iterator</tt> over a list of all stored
* <tt>AccountID</tt>s. The list of stored accounts include all registered
* accounts and all disabled accounts. In other words in this list we could
* find accounts that aren't loaded.
* <p>
* In order to check if an account is already loaded please use the
* #isAccountLoaded(AccountID accountID) method. To load an account use the
* #loadAccount(AccountID accountID) method.
*
* @return an <tt>Iterator</tt> over a list of all stored
* <tt>AccountID</tt>s
*/
public Collection<AccountID> getStoredAccounts()
{
synchronized (storedAccounts)
{
return new Vector<AccountID>(storedAccounts);
}
}
/**
* Loads the account corresponding to the given <tt>AccountID</tt>. An
* account is loaded when its <tt>ProtocolProviderService</tt> is registered
* in the bundle context. This method is meant to load the account through
* the corresponding <tt>ProtocolProviderFactory</tt>.
*
* @param accountID the identifier of the account to load
* @throws OperationFailedException if anything goes wrong while loading the
* account corresponding to the specified <tt>accountID</tt>
*/
public void loadAccount(AccountID accountID)
throws OperationFailedException
{
// If the account with the given id is already loaded we have nothing
// to do here.
if (isAccountLoaded(accountID))
return;
ProtocolProviderFactory providerFactory
= ProtocolProviderActivator.getProtocolProviderFactory(
accountID.getProtocolName());
if(providerFactory.loadAccount(accountID))
{
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(false));
// Finally store the modified properties.
storeAccount(providerFactory, accountID);
}
}
/**
* Unloads the account corresponding to the given <tt>AccountID</tt>. An
* account is unloaded when its <tt>ProtocolProviderService</tt> is
* unregistered in the bundle context. This method is meant to unload the
* account through the corresponding <tt>ProtocolProviderFactory</tt>.
*
* @param accountID the identifier of the account to load
* @throws OperationFailedException if anything goes wrong while unloading
* the account corresponding to the specified <tt>accountID</tt>
*/
public void unloadAccount(AccountID accountID)
throws OperationFailedException
{
// If the account with the given id is already unloaded we have nothing
// to do here.
if (!isAccountLoaded(accountID))
return;
ProtocolProviderFactory providerFactory
= ProtocolProviderActivator.getProtocolProviderFactory(
accountID.getProtocolName());
// Obtain the protocol provider.
ServiceReference<ProtocolProviderService> serRef
= providerFactory.getProviderForAccount(accountID);
// If there's no such provider we have nothing to do here.
if (serRef == null)
return;
ProtocolProviderService protocolProvider
= bundleContext.getService(serRef);
// Set the account icon path for unloaded accounts.
String iconPathProperty = accountID.getAccountPropertyString(
ProtocolProviderFactory.ACCOUNT_ICON_PATH);
if (iconPathProperty == null)
{
accountID.putAccountProperty(
ProtocolProviderFactory.ACCOUNT_ICON_PATH,
protocolProvider.getProtocolIcon()
.getIconPath(ProtocolIcon.ICON_SIZE_32x32));
}
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(true));
if (!providerFactory.unloadAccount(accountID))
{
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(false));
}
// Finally store the modified properties.
storeAccount(providerFactory, accountID);
}
/**
* Checks if the account corresponding to the given <tt>accountID</tt> is
* loaded. An account is loaded if its <tt>ProtocolProviderService</tt> is
* registered in the bundle context. By default all accounts are loaded.
* However the user could manually unload an account, which would be
* unregistered from the bundle context, but would remain in the
* configuration file.
*
* @param accountID the identifier of the account to load
* @return <tt>true</tt> to indicate that the account with the given
* <tt>accountID</tt> is loaded, <tt>false</tt> - otherwise
*/
public boolean isAccountLoaded(AccountID accountID)
{
return storedAccounts.contains(accountID) && accountID.isEnabled();
}
private String stripPackagePrefix(String property)
{
int packageEndIndex = property.lastIndexOf('.');
if (packageEndIndex != -1)
property = property.substring(packageEndIndex + 1);
return property;
}
}
|