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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
|
/*
* 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.lang.reflect.*;
import java.util.*;
import net.java.sip.communicator.service.credentialsstorage.*;
import net.java.sip.communicator.util.*;
import org.jitsi.service.configuration.*;
import org.osgi.framework.*;
/**
* The ProtocolProviderFactory is what actually creates instances of a
* ProtocolProviderService implementation. A provider factory would register,
* persistently store, and remove when necessary, ProtocolProviders. The way
* things are in the SIP Communicator, a user account is represented (in a 1:1
* relationship) by an AccountID and a ProtocolProvider. In other words - one
* would have as many protocol providers installed in a given moment as they
* would user account registered through the various services.
*
* @author Emil Ivov
* @author Lubomir Marinov
*/
public abstract class ProtocolProviderFactory
{
/**
* The <tt>Logger</tt> used by the <tt>ProtocolProviderFactory</tt> class
* and its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(ProtocolProviderFactory.class);
/**
* Then name of a property which represents a password.
*/
public static final String PASSWORD = "PASSWORD";
/**
* The name of a property representing the name of the protocol for an
* ProtocolProviderFactory.
*/
public static final String PROTOCOL = "PROTOCOL_NAME";
/**
* The name of a property representing the path to protocol icons.
*/
public static final String PROTOCOL_ICON_PATH = "PROTOCOL_ICON_PATH";
/**
* The name of a property representing the path to the account icon to
* be used in the user interface, when the protocol provider service is not
* available.
*/
public static final String ACCOUNT_ICON_PATH = "ACCOUNT_ICON_PATH";
/**
* The name of a property which represents the AccountID of a
* ProtocolProvider and that, together with a password is used to login
* on the protocol network..
*/
public static final String USER_ID = "USER_ID";
/**
* The name that should be displayed to others when we are calling or
* writing them.
*/
public static final String DISPLAY_NAME = "DISPLAY_NAME";
/**
* The name that should be displayed to the user on call via and chat via
* lists.
*/
public static final String ACCOUNT_DISPLAY_NAME = "ACCOUNT_DISPLAY_NAME";
/**
* The name of the property under which we store protocol AccountID-s.
*/
public static final String ACCOUNT_UID = "ACCOUNT_UID";
/**
* The name of the property under which we store protocol the address of
* a protocol centric entity (any protocol server).
*/
public static final String SERVER_ADDRESS = "SERVER_ADDRESS";
/**
* The name of the property under which we store the number of the port
* where the server stored against the SERVER_ADDRESS property is expecting
* connections to be made via this protocol.
*/
public static final String SERVER_PORT = "SERVER_PORT";
/**
* The name of the property under which we store the name of the transport
* protocol that needs to be used to access the server.
*/
public static final String SERVER_TRANSPORT = "SERVER_TRANSPORT";
/**
* The name of the property under which we store protocol the address of
* a protocol proxy.
*/
public static final String PROXY_ADDRESS = "PROXY_ADDRESS";
/**
* The name of the property under which we store the number of the port
* where the proxy stored against the PROXY_ADDRESS property is expecting
* connections to be made via this protocol.
*/
public static final String PROXY_PORT = "PROXY_PORT";
/**
* The name of the property which defines whether proxy is auto configured
* by the protocol by using known methods such as specific DNS queries.
*/
public static final String PROXY_AUTO_CONFIG = "PROXY_AUTO_CONFIG";
/**
* The property indicating the preferred UDP and TCP
* port to bind to for clear communications.
*/
public static final String PREFERRED_CLEAR_PORT_PROPERTY_NAME
= "net.java.sip.communicator.SIP_PREFERRED_CLEAR_PORT";
/**
* The property indicating the preferred TLS (TCP)
* port to bind to for secure communications.
*/
public static final String PREFERRED_SECURE_PORT_PROPERTY_NAME
= "net.java.sip.communicator.SIP_PREFERRED_SECURE_PORT";
/**
* The name of the property under which we store the the type of the proxy
* stored against the PROXY_ADDRESS property. Exact type values depend on
* protocols and among them are socks4, socks5, http and possibly others.
*/
public static final String PROXY_TYPE = "PROXY_TYPE";
/**
* The name of the property under which we store the the username for the
* proxy stored against the PROXY_ADDRESS property.
*/
public static final String PROXY_USERNAME = "PROXY_USERNAME";
/**
* The name of the property under which we store the the authorization name
* for the proxy stored against the PROXY_ADDRESS property.
*/
public static final String AUTHORIZATION_NAME = "AUTHORIZATION_NAME";
/**
* The name of the property under which we store the password for the proxy
* stored against the PROXY_ADDRESS property.
*/
public static final String PROXY_PASSWORD = "PROXY_PASSWORD";
/**
* The name of the property under which we store the name of the transport
* protocol that needs to be used to access the proxy.
*/
public static final String PROXY_TRANSPORT = "PROXY_TRANSPORT";
/**
* The name of the property that indicates whether loose routing should be
* forced for all traffic in an account, rather than routing through an
* outbound proxy which is the default for Jitsi.
*/
public static final String FORCE_PROXY_BYPASS = "FORCE_PROXY_BYPASS";
/**
* The name of the property under which we store the user preference for a
* transport protocol to use (i.e. tcp or udp).
*/
public static final String PREFERRED_TRANSPORT = "PREFERRED_TRANSPORT";
/**
* The name of the property under which we store whether we generate
* resource values or we just use the stored one.
*/
public static final String AUTO_GENERATE_RESOURCE = "AUTO_GENERATE_RESOURCE";
/**
* The name of the property under which we store resources such as the
* jabber resource property.
*/
public static final String RESOURCE = "RESOURCE";
/**
* The name of the property under which we store resource priority.
*/
public static final String RESOURCE_PRIORITY = "RESOURCE_PRIORITY";
/**
* The name of the property which defines that the call is encrypted by
* default
*/
public static final String DEFAULT_ENCRYPTION = "DEFAULT_ENCRYPTION";
/**
* The name of the property that indicates the encryption protocols for this
* account.
*/
public static final String ENCRYPTION_PROTOCOL = "ENCRYPTION_PROTOCOL";
/**
* The name of the property that indicates the status (enabed or disabled)
* encryption protocols for this account.
*/
public static final String ENCRYPTION_PROTOCOL_STATUS
= "ENCRYPTION_PROTOCOL_STATUS";
/**
* The name of the property which defines if to include the ZRTP attribute
* to SIP/SDP
*/
public static final String DEFAULT_SIPZRTP_ATTRIBUTE =
"DEFAULT_SIPZRTP_ATTRIBUTE";
/**
* The name of the property which defines the ID of the client TLS
* certificate configuration entry.
*/
public static final String CLIENT_TLS_CERTIFICATE =
"CLIENT_TLS_CERTIFICATE";
/**
* The name of the property under which we store the boolean value
* indicating if the user name should be automatically changed if the
* specified name already exists. This property is meant to be used by IRC
* implementations.
*/
public static final String AUTO_CHANGE_USER_NAME = "AUTO_CHANGE_USER_NAME";
/**
* The name of the property under which we store the boolean value
* indicating if a password is required. Initially this property is meant to
* be used by IRC implementations.
*/
public static final String NO_PASSWORD_REQUIRED = "NO_PASSWORD_REQUIRED";
/**
* The name of the property under which we store if the presence is enabled.
*/
public static final String IS_PRESENCE_ENABLED = "IS_PRESENCE_ENABLED";
/**
* The name of the property under which we store if the p2p mode for SIMPLE
* should be forced.
*/
public static final String FORCE_P2P_MODE = "FORCE_P2P_MODE";
/**
* The name of the property under which we store the offline contact polling
* period for SIMPLE.
*/
public static final String POLLING_PERIOD = "POLLING_PERIOD";
/**
* The name of the property under which we store the chosen default
* subscription expiration value for SIMPLE.
*/
public static final String SUBSCRIPTION_EXPIRATION
= "SUBSCRIPTION_EXPIRATION";
/**
* Indicates if the server address has been validated.
*/
public static final String SERVER_ADDRESS_VALIDATED
= "SERVER_ADDRESS_VALIDATED";
/**
* Indicates if the server settings are over
*/
public static final String IS_SERVER_OVERRIDDEN
= "IS_SERVER_OVERRIDDEN";
/**
* Indicates if the proxy address has been validated.
*/
public static final String PROXY_ADDRESS_VALIDATED
= "PROXY_ADDRESS_VALIDATED";
/**
* Indicates the search strategy chosen for the DICT protocole.
*/
public static final String STRATEGY = "STRATEGY";
/**
* Indicates a protocol that would not be shown in the user interface as an
* account.
*/
public static final String IS_PROTOCOL_HIDDEN = "IS_PROTOCOL_HIDDEN";
/**
* Indicates if the given account is the preferred account.
*/
public static final String IS_PREFERRED_PROTOCOL = "IS_PREFERRED_PROTOCOL";
/**
* The name of the property that would indicate if a given account is
* currently enabled or disabled.
*/
public static final String IS_ACCOUNT_DISABLED = "IS_ACCOUNT_DISABLED";
/**
* The name of the property that would indicate if a given account
* configuration form is currently hidden.
*/
public static final String IS_ACCOUNT_CONFIG_HIDDEN = "IS_CONFIG_HIDDEN";
/**
* The name of the property that would indicate if a given account
* status menu is currently hidden.
*/
public static final String IS_ACCOUNT_STATUS_MENU_HIDDEN =
"IS_STATUS_MENU_HIDDEN";
/**
* The name of the property that would indicate if a given account
* configuration is read only.
*/
public static final String IS_ACCOUNT_READ_ONLY = "IS_READ_ONLY";
/**
* The name of the property that would indicate if a given account
* groups are readonly, values can be all or a comma separated
* group names including root.
*/
public static final String ACCOUNT_READ_ONLY_GROUPS = "READ_ONLY_GROUPS";
/**
* Indicates if ICE should be used.
*/
public static final String IS_USE_ICE = "ICE_ENABLED";
/**
* Indicates if STUN server should be automatically discovered.
*/
public static final String AUTO_DISCOVER_STUN = "AUTO_DISCOVER_STUN";
/**
* Indicates if default STUN server would be used if no other STUN/TURN
* server are available.
*/
public static final String USE_DEFAULT_STUN_SERVER
= "USE_DEFAULT_STUN_SERVER";
/**
* The name of the boolean account property which indicates whether Jitsi
* Videobridge is to be used, if available and supported, for conference
* calls.
*/
public static final String USE_JITSI_VIDEO_BRIDGE
= "USE_JITSI_VIDEO_BRIDGE";
/**
* The property name prefix for all stun server properties. We generally use
* this prefix in conjunction with an index which is how we store multiple
* servers.
*/
public static final String STUN_PREFIX = "STUN";
/**
* The base property name for address of additional STUN servers specified.
*/
public static final String STUN_ADDRESS = "ADDRESS";
/**
* The base property name for port of additional STUN servers specified.
*/
public static final String STUN_PORT = "PORT";
/**
* The base property name for username of additional STUN servers specified.
*/
public static final String STUN_USERNAME = "USERNAME";
/**
* The base property name for password of additional STUN servers specified.
*/
public static final String STUN_PASSWORD = "PASSWORD";
/**
* The base property name for the turn supported property of additional
* STUN servers specified.
*/
public static final String STUN_IS_TURN_SUPPORTED = "IS_TURN_SUPPORTED";
/**
* Indicates if JingleNodes should be used with ICE.
*/
public static final String IS_USE_JINGLE_NODES = "JINGLE_NODES_ENABLED";
/**
* Indicates if JingleNodes should be used with ICE.
*/
public static final String AUTO_DISCOVER_JINGLE_NODES
= "AUTO_DISCOVER_JINGLE_NODES";
/**
* Indicates if JingleNodes should use buddies to search for nodes.
*/
public static final String JINGLE_NODES_SEARCH_BUDDIES
= "JINGLE_NODES_SEARCH_BUDDIES";
/**
* Indicates if UPnP should be used with ICE.
*/
public static final String IS_USE_UPNP = "UPNP_ENABLED";
/**
* Indicates if we allow non-TLS connection.
*/
public static final String IS_ALLOW_NON_SECURE = "ALLOW_NON_SECURE";
/**
* Enable notifications for new voicemail messages.
*/
public static final String VOICEMAIL_ENABLED = "VOICEMAIL_ENABLED";
/**
* Address used to reach voicemail box, by services able to
* subscribe for voicemail new messages notifications.
*/
public static final String VOICEMAIL_URI = "VOICEMAIL_URI";
/**
* Address used to call to hear your messages stored on the server
* for your voicemail.
*/
public static final String VOICEMAIL_CHECK_URI = "VOICEMAIL_CHECK_URI";
/**
* Indicates if calling is disabled for a certain account.
*/
public static final String IS_CALLING_DISABLED_FOR_ACCOUNT
= "CALLING_DISABLED";
/**
* Indicates if desktop streaming/sharing is disabled for a certain account.
*/
public static final String IS_DESKTOP_STREAMING_DISABLED
= "DESKTOP_STREAMING_DISABLED";
/**
* Indicates if desktop remote control is disabled for a certain account.
*/
public static final String IS_DESKTOP_REMOTE_CONTROL_DISABLED
= "DESKTOP_REMOTE_CONTROL_DISABLED";
/**
* The sms default server address.
*/
public static final String SMS_SERVER_ADDRESS = "SMS_SERVER_ADDRESS";
/**
* Keep-alive method used by the protocol.
*/
public static final String KEEP_ALIVE_METHOD = "KEEP_ALIVE_METHOD";
/**
* The interval for keep-alives if any.
*/
public static final String KEEP_ALIVE_INTERVAL = "KEEP_ALIVE_INTERVAL";
/**
* The name of the property holding DTMF method.
*/
public static final String DTMF_METHOD = "DTMF_METHOD";
/**
* The minimal DTMF tone duration.
*/
public static final String DTMF_MINIMAL_TONE_DURATION
= "DTMF_MINIMAL_TONE_DURATION";
/**
* Paranoia mode when turned on requires all calls to be secure and
* indicated as such.
*/
public static final String MODE_PARANOIA = "MODE_PARANOIA";
/**
* The name of the "override encodings" property
*/
public static final String OVERRIDE_ENCODINGS = "OVERRIDE_ENCODINGS";
/**
* The prefix used to store account encoding properties
*/
public static final String ENCODING_PROP_PREFIX = "Encodings";
/**
* An account property to provide a connected account to check for
* its status. Used when the current provider need to reject calls
* but is missing presence operation set and need to check other
* provider for status.
*/
public static final String CUSAX_PROVIDER_ACCOUNT_PROP
= "cusax.XMPP_ACCOUNT_ID";
/**
* The <code>BundleContext</code> containing (or to contain) the service
* registration of this factory.
*/
private final BundleContext bundleContext;
/**
* The name of the protocol this factory registers its
* <code>ProtocolProviderService</code>s with and to be placed in the
* properties of the accounts created by this factory.
*/
private final String protocolName;
/**
* The table that we store our accounts in.
* <p>
* TODO Synchronize the access to the field which may in turn be better
* achieved by also hiding it from protected into private access.
* </p>
*/
protected final Map<AccountID, ServiceRegistration<ProtocolProviderService>>
registeredAccounts
= new HashMap<AccountID, ServiceRegistration<ProtocolProviderService>>();
/**
* The name of the property that indicates the AVP type.
* <ul>
* <li>{@link #SAVP_OFF}</li>
* <li>{@link #SAVP_MANDATORY}</li>
* <li>{@link #SAVP_OPTIONAL}</li>
* </ul>
*/
public static final String SAVP_OPTION = "SAVP_OPTION";
/**
* Always use RTP/AVP
*/
public static final int SAVP_OFF = 0;
/**
* Always use RTP/SAVP
*/
public static final int SAVP_MANDATORY = 1;
/**
* Sends two media description, with RTP/SAVP being first.
*/
public static final int SAVP_OPTIONAL = 2;
/**
* The name of the property that defines the enabled SDES cipher suites.
* Enabled suites are listed as CSV by their RFC name.
*/
public static final String SDES_CIPHER_SUITES = "SDES_CIPHER_SUITES";
/**
* The name of the property that defines the enabled/disabled state of
* message carbons.
*/
public static final String IS_CARBON_DISABLED = "CARBON_DISABLED";
/**
* Creates a new <tt>ProtocolProviderFactory</tt>.
*
* @param bundleContext the bundle context reference of the service
* @param protocolName the name of the protocol
*/
protected ProtocolProviderFactory(BundleContext bundleContext,
String protocolName)
{
this.bundleContext = bundleContext;
this.protocolName = protocolName;
}
/**
* Gets the <code>BundleContext</code> containing (or to contain) the
* service registration of this factory.
*
* @return the <code>BundleContext</code> containing (or to contain) the
* service registration of this factory
*/
public BundleContext getBundleContext()
{
return bundleContext;
}
/**
* Initializes and creates an account corresponding to the specified
* accountProperties and registers the resulting ProtocolProvider in the
* <tt>context</tt> BundleContext parameter. Note that account
* registration is persistent and accounts that are registered during
* a particular sip-communicator session would be automatically reloaded
* during all following sessions until they are removed through the
* removeAccount method.
*
* @param userID the user identifier uniquely representing the newly
* created account within the protocol namespace.
* @param accountProperties a set of protocol (or implementation) specific
* properties defining the new account.
* @return the AccountID of the newly created account.
* @throws java.lang.IllegalArgumentException if userID does not correspond
* to an identifier in the context of the underlying protocol or if
* accountProperties does not contain a complete set of account installation
* properties.
* @throws java.lang.IllegalStateException if the account has already been
* installed.
* @throws java.lang.NullPointerException if any of the arguments is null.
*/
public abstract AccountID installAccount(String userID,
Map<String, String> accountProperties)
throws IllegalArgumentException,
IllegalStateException,
NullPointerException;
/**
* Modifies the account corresponding to the specified accountID. This
* method is meant to be used to change properties of already existing
* accounts. Note that if the given accountID doesn't correspond to any
* registered account this method would do nothing.
*
* @param protocolProvider the protocol provider service corresponding to
* the modified account.
* @param accountProperties a set of protocol (or implementation) specific
* properties defining the new account.
*
* @throws java.lang.NullPointerException if any of the arguments is null.
*/
public abstract void modifyAccount(
ProtocolProviderService protocolProvider,
Map<String, String> accountProperties)
throws NullPointerException;
/**
* Returns a copy of the list containing the <tt>AccountID</tt>s of all
* accounts currently registered in this protocol provider.
* @return a copy of the list containing the <tt>AccountID</tt>s of all
* accounts currently registered in this protocol provider.
*/
public ArrayList<AccountID> getRegisteredAccounts()
{
synchronized (registeredAccounts)
{
return new ArrayList<AccountID>(registeredAccounts.keySet());
}
}
/**
* Returns the ServiceReference for the protocol provider corresponding to
* the specified accountID or null if the accountID is unknown.
* @param accountID the accountID of the protocol provider we'd like to get
* @return a ServiceReference object to the protocol provider with the
* specified account id and null if the account id is unknown to the
* provider factory.
*/
public ServiceReference<ProtocolProviderService> getProviderForAccount(
AccountID accountID)
{
ServiceRegistration<ProtocolProviderService> registration;
synchronized (registeredAccounts)
{
registration = registeredAccounts.get(accountID);
}
try
{
if (registration != null)
return registration.getReference();
}
catch (IllegalStateException ise)
{
synchronized (registeredAccounts)
{
registeredAccounts.remove(accountID);
}
}
return null;
}
/**
* Removes the specified account from the list of accounts that this
* provider factory is handling. If the specified accountID is unknown to
* the ProtocolProviderFactory, the call has no effect and false is
* returned. This method is persistent in nature and once called the account
* corresponding to the specified ID will not be loaded during future runs
* of the project.
*
* @param accountID the ID of the account to remove.
* @return true if an account with the specified ID existed and was removed
* and false otherwise.
*/
public boolean uninstallAccount(AccountID accountID)
{
// Unregister the protocol provider.
ServiceReference<ProtocolProviderService> serRef
= getProviderForAccount(accountID);
boolean wasAccountExisting = false;
// If the protocol provider service is registered, first unregister the
// service.
if (serRef != null)
{
BundleContext bundleContext = getBundleContext();
ProtocolProviderService protocolProvider
= bundleContext.getService(serRef);
try
{
protocolProvider.unregister();
}
catch (OperationFailedException ex)
{
logger.error(
"Failed to unregister protocol provider for account: "
+ accountID + " caused by: " + ex);
}
}
ServiceRegistration<ProtocolProviderService> registration;
synchronized (registeredAccounts)
{
registration = registeredAccounts.remove(accountID);
}
// first remove the stored account so when PP is unregistered we can
// distinguish between deleted or just disabled account
wasAccountExisting = removeStoredAccount(accountID);
if (registration != null)
{
// Kill the service.
registration.unregister();
}
return wasAccountExisting;
}
/**
* The method stores the specified account in the configuration service
* under the package name of the source factory. The restore and remove
* account methods are to be used to obtain access to and control the stored
* accounts.
* <p>
* In order to store all account properties, the method would create an
* entry in the configuration service corresponding (beginning with) the
* <tt>sourceFactory</tt>'s package name and add to it a unique identifier
* (e.g. the current miliseconds.)
* </p>
*
* @param accountID the AccountID corresponding to the account that we would
* like to store.
*/
protected void storeAccount(AccountID accountID)
{
this.storeAccount(accountID, true);
}
/**
* The method stores the specified account in the configuration service
* under the package name of the source factory. The restore and remove
* account methods are to be used to obtain access to and control the stored
* accounts.
* <p>
* In order to store all account properties, the method would create an
* entry in the configuration service corresponding (beginning with) the
* <tt>sourceFactory</tt>'s package name and add to it a unique identifier
* (e.g. the current miliseconds.)
* </p>
*
* @param accountID the AccountID corresponding to the account that we would
* like to store.
* @param isModification if <tt>false</tt> there must be no such already
* loaded account, it <tt>true</tt> ist modification of an existing account.
* Usually we use this method with <tt>false</tt> in method installAccount
* and with <tt>true</tt> or the overridden method in method
* modifyAccount.
*/
protected void storeAccount(AccountID accountID, boolean isModification)
{
if(!isModification
&& getAccountManager().getStoredAccounts().contains(accountID))
{
throw new IllegalStateException(
"An account for id " + accountID.getUserID()
+ " was already loaded!");
}
try
{
getAccountManager().storeAccount(this, accountID);
}
catch (OperationFailedException ofex)
{
throw new UndeclaredThrowableException(ofex);
}
}
/**
* Saves the password for the specified account after scrambling it a bit so
* that it is not visible from first sight. (The method remains highly
* insecure).
*
* @param accountID the AccountID for the account whose password we're
* storing
* @param password the password itself
*
* @throws IllegalArgumentException if no account corresponding to
* <code>accountID</code> has been previously stored
*/
public void storePassword(AccountID accountID, String password)
throws IllegalArgumentException
{
try
{
storePassword(getBundleContext(), accountID, password);
}
catch (OperationFailedException ofex)
{
throw new UndeclaredThrowableException(ofex);
}
}
/**
* Saves the password for the specified account after scrambling it a bit
* so that it is not visible from first sight (Method remains highly
* insecure).
* <p>
* TODO Delegate the implementation to {@link AccountManager} because it
* knows the format in which the password (among the other account
* properties) is to be saved.
* </p>
*
* @param bundleContext a currently valid bundle context.
* @param accountID the <tt>AccountID</tt> of the account whose password is
* to be stored
* @param password the password to be stored
*
* @throws IllegalArgumentException if no account corresponding to
* <tt>accountID</tt> has been previously stored.
* @throws OperationFailedException if anything goes wrong while storing the
* specified <tt>password</tt>
*/
protected void storePassword(BundleContext bundleContext,
AccountID accountID,
String password)
throws IllegalArgumentException,
OperationFailedException
{
String accountPrefix
= findAccountPrefix(
bundleContext,
accountID,
getFactoryImplPackageName());
if (accountPrefix == null)
{
throw
new IllegalArgumentException(
"No previous records found for account ID: "
+ accountID.getAccountUniqueID()
+ " in package"
+ getFactoryImplPackageName());
}
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
if (!credentialsStorage.storePassword(accountPrefix, password))
{
throw
new OperationFailedException(
"CredentialsStorageService failed to storePassword",
OperationFailedException.GENERAL_ERROR);
}
// Update password property also in the AccountID
// to prevent it from being removed during account reload
// in some cases.
accountID.setPassword(password);
}
/**
* Returns the password last saved for the specified account.
*
* @param accountID the AccountID for the account whose password we're
* looking for
*
* @return a String containing the password for the specified accountID
*/
public String loadPassword(AccountID accountID)
{
return loadPassword(getBundleContext(), accountID);
}
/**
* Returns the password last saved for the specified account.
* <p>
* TODO Delegate the implementation to {@link AccountManager} because it
* knows the format in which the password (among the other account
* properties) was saved.
* </p>
*
* @param bundleContext a currently valid bundle context.
* @param accountID the AccountID for the account whose password we're
* looking for..
*
* @return a String containing the password for the specified accountID.
*/
protected String loadPassword(BundleContext bundleContext,
AccountID accountID)
{
String accountPrefix = findAccountPrefix(
bundleContext, accountID, getFactoryImplPackageName());
if (accountPrefix == null)
return null;
CredentialsStorageService credentialsStorage
= ServiceUtils.getService(
bundleContext,
CredentialsStorageService.class);
return credentialsStorage.loadPassword(accountPrefix);
}
/**
* Initializes and creates an account corresponding to the specified
* accountProperties and registers the resulting ProtocolProvider in the
* <tt>context</tt> BundleContext parameter. This method has a persistent
* effect. Once created the resulting account will remain installed until
* removed through the uninstallAccount method.
*
* @param accountProperties a set of protocol (or implementation) specific
* properties defining the new account.
* @return the AccountID of the newly loaded account
*/
public AccountID loadAccount(Map<String, String> accountProperties)
{
AccountID accountID = createAccount(accountProperties);
loadAccount(accountID);
return accountID;
}
/**
* Creates a protocol provider for the given <tt>accountID</tt> and
* registers it in the bundle context. This method has a persistent
* effect. Once created the resulting account will remain installed until
* removed through the uninstallAccount method.
*
* @param accountID the account identifier
* @return <tt>true</tt> if the account with the given <tt>accountID</tt> is
* successfully loaded, otherwise returns <tt>false</tt>
*/
public boolean loadAccount(AccountID accountID)
{
// Need to obtain the original user id property, instead of calling
// accountID.getUserID(), because this method could return a modified
// version of the user id property.
String userID
= accountID.getAccountPropertyString(
ProtocolProviderFactory.USER_ID);
ProtocolProviderService service = createService(userID, accountID);
Dictionary<String, String> properties = new Hashtable<String, String>();
properties.put(PROTOCOL, protocolName);
properties.put(USER_ID, userID);
ServiceRegistration<ProtocolProviderService> serviceRegistration
= bundleContext.registerService(
ProtocolProviderService.class,
service,
properties);
if (serviceRegistration == null)
{
return false;
}
else
{
synchronized (registeredAccounts)
{
registeredAccounts.put(accountID, serviceRegistration);
}
return true;
}
}
/**
* Unloads the account corresponding to the given <tt>accountID</tt>.
* Unregisters the corresponding protocol provider, but keeps the account in
* contrast to the uninstallAccount method.
*
* @param accountID the account identifier
* @return true if an account with the specified ID existed and was unloaded
* and false otherwise.
*/
public boolean unloadAccount(AccountID accountID)
{
// Unregister the protocol provider.
ServiceReference<ProtocolProviderService> serRef
= getProviderForAccount(accountID);
if (serRef == null)
{
return false;
}
BundleContext bundleContext = getBundleContext();
ProtocolProviderService protocolProvider
= bundleContext.getService(serRef);
try
{
protocolProvider.unregister();
}
catch (OperationFailedException ex)
{
logger.error(
"Failed to unregister protocol provider for account: "
+ accountID + " caused by: " + ex);
}
ServiceRegistration<ProtocolProviderService> registration;
synchronized (registeredAccounts)
{
registration = registeredAccounts.remove(accountID);
}
if (registration == null)
{
return false;
}
// Kill the service.
registration.unregister();
return true;
}
/**
* Initializes and creates an account corresponding to the specified
* accountProperties.
*
* @param accountProperties a set of protocol (or implementation) specific
* properties defining the new account.
* @return the AccountID of the newly created account
*/
public AccountID createAccount(Map<String, String> accountProperties)
{
BundleContext bundleContext = getBundleContext();
if (bundleContext == null)
throw new NullPointerException(
"The specified BundleContext was null");
if (accountProperties == null)
throw new NullPointerException(
"The specified property map was null");
String userID = accountProperties.get(USER_ID);
if (userID == null)
throw new NullPointerException(
"The account properties contained no user id.");
String protocolName = getProtocolName();
if (!accountProperties.containsKey(PROTOCOL))
accountProperties.put(PROTOCOL, protocolName);
return createAccountID(userID, accountProperties);
}
/**
* Creates a new <code>AccountID</code> instance with a specific user ID to
* represent a given set of account properties.
* <p>
* The method is a pure factory allowing implementers to specify the runtime
* type of the created <code>AccountID</code> and customize the instance.
* The returned <code>AccountID</code> will later be associated with a
* <code>ProtocolProviderService</code> by the caller (e.g. using
* {@link #createService(String, AccountID)}).
* </p>
*
* @param userID the user ID of the new instance
* @param accountProperties the set of properties to be represented by the
* new instance
* @return a new <code>AccountID</code> instance with the specified user ID
* representing the given set of account properties
*/
protected abstract AccountID createAccountID(
String userID, Map<String, String> accountProperties);
/**
* Gets the name of the protocol this factory registers its
* <code>ProtocolProviderService</code>s with and to be placed in the
* properties of the accounts created by this factory.
*
* @return the name of the protocol this factory registers its
* <code>ProtocolProviderService</code>s with and to be placed in
* the properties of the accounts created by this factory
*/
public String getProtocolName()
{
return protocolName;
}
/**
* Initializes a new <code>ProtocolProviderService</code> instance with a
* specific user ID to represent a specific <code>AccountID</code>.
* <p>
* The method is a pure factory allowing implementers to specify the runtime
* type of the created <code>ProtocolProviderService</code> and customize
* the instance. The caller will later register the returned service with
* the <code>BundleContext</code> of this factory.
* </p>
*
* @param userID the user ID to initialize the new instance with
* @param accountID the <code>AccountID</code> to be represented by the new
* instance
* @return a new <code>ProtocolProviderService</code> instance with the
* specific user ID representing the specified
* <code>AccountID</code>
*/
protected abstract ProtocolProviderService createService(String userID,
AccountID accountID);
/**
* Removes the account with <tt>accountID</tt> from the set of accounts
* that are persistently stored inside the configuration service.
*
* @param accountID the AccountID of the account to remove.
*
* @return true if an account has been removed and false otherwise.
*/
protected boolean removeStoredAccount(AccountID accountID)
{
return getAccountManager().removeStoredAccount(this, accountID);
}
/**
* Returns the prefix for all persistently stored properties of the account
* with the specified id.
* @param bundleContext a currently valid bundle context.
* @param accountID the AccountID of the account whose properties we're
* looking for.
* @param sourcePackageName a String containing the package name of the
* concrete factory class that extends us.
* @return a String indicating the ConfigurationService property name
* prefix under which all account properties are stored or null if no
* account corresponding to the specified id was found.
*/
public static String findAccountPrefix(BundleContext bundleContext,
AccountID accountID,
String sourcePackageName)
{
ServiceReference<ConfigurationService> confReference
= bundleContext.getServiceReference(ConfigurationService.class);
ConfigurationService configurationService
= bundleContext.getService(confReference);
//first retrieve all accounts that we've registered
List<String> storedAccounts =
configurationService.getPropertyNamesByPrefix(sourcePackageName,
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
+ "." + ACCOUNT_UID); // propname
if (accountID.getAccountUniqueID().equals(accountUID))
{
return accountRootPropertyName;
}
}
return null;
}
/**
* Returns the name of the package that we're currently running in (i.e.
* the name of the package containing the proto factory that extends us).
*
* @return a String containing the package name of the concrete factory
* class that extends us.
*/
private String getFactoryImplPackageName()
{
String className = getClass().getName();
return className.substring(0, className.lastIndexOf('.'));
}
/**
* Prepares the factory for bundle shutdown.
*/
public void stop()
{
if (logger.isTraceEnabled())
logger.trace("Preparing to stop all protocol providers of" + this);
synchronized (registeredAccounts)
{
for (ServiceRegistration<ProtocolProviderService> reg
: registeredAccounts.values())
{
stop(reg);
reg.unregister();
}
registeredAccounts.clear();
}
}
/**
* Shuts down the <code>ProtocolProviderService</code> representing an
* account registered with this factory.
*
* @param registeredAccount the <code>ServiceRegistration</code> of the
* <code>ProtocolProviderService</code> representing an account
* registered with this factory
*/
protected void stop(
ServiceRegistration<ProtocolProviderService> registeredAccount)
{
ProtocolProviderService protocolProviderService
= getBundleContext().getService(registeredAccount.getReference());
protocolProviderService.shutdown();
}
/**
* Get the <tt>AccountManager</tt> of the protocol.
*
* @return <tt>AccountManager</tt> of the protocol
*/
private AccountManager getAccountManager()
{
BundleContext bundleContext = getBundleContext();
ServiceReference<AccountManager> serviceReference
= bundleContext.getServiceReference(AccountManager.class);
return bundleContext.getService(serviceReference);
}
/**
* Finds registered <tt>ProtocolProviderFactory</tt> for given
* <tt>protocolName</tt>.
* @param bundleContext the OSGI bundle context that will be used.
* @param protocolName the protocol name.
* @return Registered <tt>ProtocolProviderFactory</tt> for given protocol
* name or <tt>null</tt> if no provider was found.
*/
static public ProtocolProviderFactory getProtocolProviderFactory(
BundleContext bundleContext,
String protocolName)
{
Collection<ServiceReference<ProtocolProviderFactory>> serRefs;
String osgiFilter
= "(" + ProtocolProviderFactory.PROTOCOL + "=" + protocolName + ")";
try
{
serRefs
= bundleContext.getServiceReferences(
ProtocolProviderFactory.class,
osgiFilter);
}
catch (InvalidSyntaxException ex)
{
serRefs = null;
logger.error(ex);
}
if ((serRefs == null) || serRefs.isEmpty())
return null;
else
return bundleContext.getService(serRefs.iterator().next());
}
}
|