1 | 1 | | /* |
| 2 | + | * RosterFlat.java |
| 3 | + | * |
2 | 4 | | * Tigase Jabber/XMPP Server |
3 | | - | * Copyright (C) 2004-2012 "Artur Hefczyc" <artur.hefczyc@tigase.org> |
| 5 | + | * Copyright (C) 2004-2013 "Tigase, Inc." <office@tigase.com> |
4 | 6 | | * |
5 | 7 | | * This program is free software: you can redistribute it and/or modify |
6 | 8 | | * it under the terms of the GNU Affero General Public License as published by |
7 | | - | * the Free Software Foundation, either version 3 of the License. |
| 9 | + | * the Free Software Foundation, either version 3 of the License, |
| 10 | + | * or (at your option) any later version. |
8 | 11 | | * |
9 | 12 | | * This program is distributed in the hope that it will be useful, |
10 | 13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| skipped 4 lines |
15 | 18 | | * along with this program. Look for COPYING file in the top folder. |
16 | 19 | | * If not, see http://www.gnu.org/licenses/. |
17 | 20 | | * |
18 | | - | * $Rev$ |
19 | | - | * Last modified by $Author$ |
20 | | - | * $Date$ |
21 | 21 | | */ |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | package tigase.xmpp.impl.roster; |
24 | 26 | | |
| skipped 15 lines |
40 | 42 | | |
41 | 43 | | import java.util.Arrays; |
42 | 44 | | import java.util.Comparator; |
| 45 | + | import java.util.concurrent.ConcurrentHashMap; |
43 | 46 | | import java.util.Date; |
44 | 47 | | import java.util.LinkedList; |
45 | 48 | | import java.util.List; |
46 | | - | import java.util.Map; |
47 | | - | import java.util.Queue; |
48 | | - | import java.util.concurrent.ConcurrentHashMap; |
49 | 49 | | import java.util.logging.Level; |
50 | 50 | | import java.util.logging.Logger; |
51 | | - | |
52 | | - | //~--- classes ---------------------------------------------------------------- |
| 51 | + | import java.util.Map; |
| 52 | + | import java.util.Queue; |
53 | 53 | | |
54 | 54 | | /** |
55 | 55 | | * Describe class RosterFlat here. |
| skipped 4 lines |
60 | 60 | | * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a> |
61 | 61 | | * @version $Rev$ |
62 | 62 | | */ |
63 | | - | public class RosterFlat extends RosterAbstract { |
64 | | - | |
| 63 | + | public class RosterFlat |
| 64 | + | extends RosterAbstract { |
65 | 65 | | /** |
66 | 66 | | * Private logger for class instances. |
67 | 67 | | */ |
68 | | - | private static final Logger log = Logger.getLogger(RosterFlat.class.getName()); |
| 68 | + | private static final Logger log = Logger.getLogger(RosterFlat.class.getName()); |
69 | 69 | | private static final SimpleParser parser = SingletonFactory.getParserInstance(); |
70 | | - | private static int maxRosterSize = new Long(Runtime.getRuntime().maxMemory() / 250000L) |
71 | | - | .intValue(); |
| 70 | + | private static int maxRosterSize = new Long(Runtime.getRuntime().maxMemory() / |
| 71 | + | 250000L).intValue(); |
| 72 | + | |
| 73 | + | //~--- methods -------------------------------------------------------------- |
72 | 74 | | |
73 | 75 | | // ~--- methods -------------------------------------------------------------- |
74 | 76 | | |
| skipped 6 lines |
81 | 83 | | * |
82 | 84 | | * @return |
83 | 85 | | */ |
84 | | - | public static boolean addBuddy(RosterElement relem, Map<BareJID, RosterElement> roster) { |
| 86 | + | public static boolean addBuddy(RosterElement relem, |
| 87 | + | Map<BareJID, RosterElement> roster) { |
85 | 88 | | if (roster.size() < maxRosterSize) { |
86 | 89 | | roster.put(relem.getJid().getBareJID(), relem); |
87 | 90 | | |
| skipped 3 lines |
91 | 94 | | return false; |
92 | 95 | | } |
93 | 96 | | |
| 97 | + | /** |
| 98 | + | * Method description |
| 99 | + | * |
| 100 | + | * |
| 101 | + | * @param buddy |
| 102 | + | * @param session |
| 103 | + | * |
| 104 | + | * @return |
| 105 | + | * |
| 106 | + | * @throws NotAuthorizedException |
| 107 | + | * @throws TigaseDBException |
| 108 | + | */ |
94 | 109 | | public RosterElement addTempBuddy(JID buddy, XMPPResourceConnection session) |
95 | | - | throws NotAuthorizedException, TigaseDBException { |
| 110 | + | throws NotAuthorizedException, TigaseDBException { |
96 | 111 | | RosterElement relem = getRosterElementInstance(buddy, null, null, session); |
| 112 | + | |
97 | 113 | | relem.setPersistent(false); |
98 | 114 | | addBuddy(relem, getUserRoster(session)); |
| 115 | + | |
99 | 116 | | return relem; |
100 | 117 | | } |
101 | 118 | | |
| skipped 8 lines |
110 | 127 | | * @return |
111 | 128 | | */ |
112 | 129 | | public static boolean parseRosterUtil(String roster_str, |
113 | | - | Map<BareJID, RosterElement> roster, XMPPResourceConnection session) { |
114 | | - | boolean result = false; |
| 130 | + | Map<BareJID, RosterElement> roster, XMPPResourceConnection session) { |
| 131 | + | boolean result = false; |
115 | 132 | | DomBuilderHandler domHandler = new DomBuilderHandler(); |
116 | 133 | | |
117 | 134 | | parser.parse(domHandler, roster_str.toCharArray(), 0, roster_str.length()); |
| skipped 6 lines |
124 | 141 | | RosterElement relem = new RosterElement(elem, session); |
125 | 142 | | |
126 | 143 | | result |= relem.isModified(); |
127 | | - | |
128 | 144 | | if (!addBuddy(relem, roster)) { |
129 | 145 | | break; |
130 | 146 | | } |
| skipped 21 lines |
152 | 168 | | */ |
153 | 169 | | @Override |
154 | 170 | | public void addBuddy(XMPPResourceConnection session, JID buddy, String name, |
155 | | - | String[] groups, String otherData) throws NotAuthorizedException, TigaseDBException { |
| 171 | + | String[] groups, String otherData) |
| 172 | + | throws NotAuthorizedException, TigaseDBException { |
156 | 173 | | |
157 | 174 | | // String buddy = JIDUtils.getNodeID(jid); |
158 | 175 | | RosterElement relem = getRosterElement(session, buddy); |
| skipped 3 lines |
162 | 179 | | |
163 | 180 | | relem = getRosterElementInstance(buddy, name, groups, session); |
164 | 181 | | relem.setOtherData(otherData); |
165 | | - | |
| 182 | + | if (log.isLoggable(Level.FINEST)) { |
| 183 | + | log.log(Level.FINEST, "1. Added buddy to roster: {0}, name: {1}, item: {2}", |
| 184 | + | new Object[] { relem.getJid(), |
| 185 | + | relem.getName(), relem.getRosterItem() }); |
| 186 | + | } |
166 | 187 | | if (addBuddy(relem, roster)) { |
167 | 188 | | saveUserRoster(session); |
168 | 189 | | } else { |
169 | 190 | | throw new TigaseDBException("Too many elements in the user roster."); |
170 | 191 | | } |
171 | | - | |
172 | 192 | | if (log.isLoggable(Level.FINEST)) { |
173 | | - | log.log(Level.FINEST, "Added buddy to roster: {0}", buddy); |
| 193 | + | log.log(Level.FINEST, "2. Added buddy to roster: {0}, name: {1}, item: {2}", |
| 194 | + | new Object[] { relem.getJid(), |
| 195 | + | relem.getName(), relem.getRosterItem() }); |
174 | 196 | | } |
175 | 197 | | } else { |
176 | | - | if ((name != null) && !name.isEmpty()) { |
| 198 | + | if ((name != null) &&!name.isEmpty()) { |
177 | 199 | | relem.setName(name); |
178 | 200 | | } |
179 | 201 | | |
| skipped 6 lines |
186 | 208 | | |
187 | 209 | | // } |
188 | 210 | | saveUserRoster(session); |
189 | | - | |
190 | 211 | | if (log.isLoggable(Level.FINEST)) { |
191 | 212 | | log.log(Level.FINEST, "Updated buddy in roster: {0}", buddy); |
192 | 213 | | } |
| skipped 14 lines |
207 | 228 | | * @throws TigaseDBException |
208 | 229 | | */ |
209 | 230 | | @Override |
210 | | - | public boolean |
211 | | - | addBuddyGroup(XMPPResourceConnection session, JID buddy, String[] groups) |
| 231 | + | public boolean addBuddyGroup(XMPPResourceConnection session, JID buddy, String[] groups) |
212 | 232 | | throws NotAuthorizedException, TigaseDBException { |
213 | 233 | | RosterElement relem = getRosterElement(session, buddy); |
214 | 234 | | |
| skipped 24 lines |
239 | 259 | | */ |
240 | 260 | | @Override |
241 | 261 | | public boolean containsBuddy(XMPPResourceConnection session, JID buddy) |
242 | | - | throws NotAuthorizedException, TigaseDBException { |
| 262 | + | throws NotAuthorizedException, TigaseDBException { |
243 | 263 | | RosterElement relem = getRosterElement(session, buddy); |
244 | | - | return relem != null && relem.isPersistent(); |
| 264 | + | |
| 265 | + | return (relem != null) && relem.isPersistent(); |
245 | 266 | | } |
| 267 | + | |
| 268 | + | //~--- get methods ---------------------------------------------------------- |
246 | 269 | | |
247 | 270 | | // ~--- get methods ---------------------------------------------------------- |
248 | 271 | | |
| skipped 9 lines |
258 | 281 | | * @throws TigaseDBException |
259 | 282 | | */ |
260 | 283 | | @Override |
| 284 | + | |
261 | 285 | | // public String[] getBuddies(XMPPResourceConnection session, |
262 | 286 | | // boolean onlineOnly) |
263 | | - | public |
264 | | - | JID[] getBuddies(XMPPResourceConnection session) throws NotAuthorizedException, |
265 | | - | TigaseDBException { |
| 287 | + | public JID[] getBuddies(XMPPResourceConnection session) |
| 288 | + | throws NotAuthorizedException, TigaseDBException { |
266 | 289 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
267 | 290 | | |
268 | 291 | | if (roster.size() == 0) { |
| skipped 1 lines |
270 | 293 | | } |
271 | 294 | | |
272 | 295 | | JID[] result = new JID[roster.size()]; |
273 | | - | int idx = 0; |
| 296 | + | int idx = 0; |
274 | 297 | | |
275 | 298 | | for (RosterElement rosterElement : roster.values()) { |
276 | 299 | | result[idx++] = rosterElement.getJid(); |
277 | 300 | | } |
| 301 | + | |
278 | 302 | | // TODO: this sorting should be optional as it may impact performance |
279 | 303 | | Arrays.sort(result, new RosterElemComparator(roster)); |
280 | 304 | | |
| skipped 25 lines |
306 | 330 | | */ |
307 | 331 | | @Override |
308 | 332 | | public String[] getBuddyGroups(XMPPResourceConnection session, JID buddy) |
309 | | - | throws NotAuthorizedException, TigaseDBException { |
| 333 | + | throws NotAuthorizedException, TigaseDBException { |
310 | 334 | | RosterElement relem = getRosterElement(session, buddy); |
311 | 335 | | |
312 | 336 | | if (relem == null) { |
| skipped 29 lines |
342 | 366 | | */ |
343 | 367 | | @Override |
344 | 368 | | public Element getBuddyItem(final XMPPResourceConnection session, JID buddy) |
345 | | - | throws NotAuthorizedException, TigaseDBException { |
| 369 | + | throws NotAuthorizedException, TigaseDBException { |
346 | 370 | | RosterElement relem = getRosterElement(session, buddy); |
347 | 371 | | |
348 | 372 | | if (relem == null) { |
| skipped 17 lines |
366 | 390 | | */ |
367 | 391 | | @Override |
368 | 392 | | public String getBuddyName(XMPPResourceConnection session, JID buddy) |
369 | | - | throws NotAuthorizedException, TigaseDBException { |
| 393 | + | throws NotAuthorizedException, TigaseDBException { |
370 | 394 | | RosterElement relem = getRosterElement(session, buddy); |
371 | 395 | | |
372 | 396 | | if (relem == null) { |
| skipped 17 lines |
390 | 414 | | */ |
391 | 415 | | @Override |
392 | 416 | | public SubscriptionType getBuddySubscription(XMPPResourceConnection session, JID buddy) |
393 | | - | throws NotAuthorizedException, TigaseDBException { |
| 417 | + | throws NotAuthorizedException, TigaseDBException { |
394 | 418 | | RosterElement relem = getRosterElement(session, buddy); |
395 | 419 | | |
396 | 420 | | if (relem == null) { |
| skipped 17 lines |
414 | 438 | | * @return |
415 | 439 | | */ |
416 | 440 | | public RosterElement getRosterElementInstance(JID buddy, String name, String[] groups, |
417 | | - | XMPPResourceConnection session) { |
| 441 | + | XMPPResourceConnection session) { |
418 | 442 | | return new RosterElement(buddy, name, groups, session); |
419 | 443 | | } |
420 | 444 | | |
| skipped 10 lines |
431 | 455 | | */ |
432 | 456 | | @Override |
433 | 457 | | public List<Element> getRosterItems(XMPPResourceConnection session) |
434 | | - | throws NotAuthorizedException, TigaseDBException { |
435 | | - | LinkedList<Element> items = new LinkedList<Element>(); |
| 458 | + | throws NotAuthorizedException, TigaseDBException { |
| 459 | + | LinkedList<Element> items = new LinkedList<Element>(); |
436 | 460 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
437 | 461 | | |
438 | 462 | | for (RosterElement relem : roster.values()) { |
| skipped 3 lines |
442 | 466 | | if (relem.isPersistent()) { |
443 | 467 | | items.add(getBuddyItem(relem)); |
444 | 468 | | } |
445 | | - | |
446 | 469 | | } |
447 | 470 | | |
448 | 471 | | return items; |
| skipped 13 lines |
462 | 485 | | */ |
463 | 486 | | @Override |
464 | 487 | | public boolean isOnline(XMPPResourceConnection session, JID buddy) |
465 | | - | throws NotAuthorizedException, TigaseDBException { |
| 488 | + | throws NotAuthorizedException, TigaseDBException { |
466 | 489 | | RosterElement relem = getRosterElement(session, buddy); |
467 | 490 | | |
468 | 491 | | return (relem != null) && relem.isOnline(); |
469 | 492 | | } |
| 493 | + | |
| 494 | + | //~--- methods -------------------------------------------------------------- |
470 | 495 | | |
471 | 496 | | // ~--- methods -------------------------------------------------------------- |
472 | 497 | | |
| skipped 8 lines |
481 | 506 | | * @return |
482 | 507 | | */ |
483 | 508 | | public boolean parseRoster(String roster_str, Map<BareJID, RosterElement> roster, |
484 | | - | XMPPResourceConnection session) { |
| 509 | + | XMPPResourceConnection session) { |
485 | 510 | | return parseRosterUtil(roster_str, roster, session); |
486 | 511 | | } |
487 | 512 | | |
| skipped 11 lines |
499 | 524 | | */ |
500 | 525 | | @Override |
501 | 526 | | public boolean presenceSent(XMPPResourceConnection session, JID buddy) |
502 | | - | throws NotAuthorizedException, TigaseDBException { |
| 527 | + | throws NotAuthorizedException, TigaseDBException { |
503 | 528 | | RosterElement relem = getRosterElement(session, buddy); |
504 | 529 | | |
505 | 530 | | return (relem != null) && relem.isPresence_sent(); |
| skipped 13 lines |
519 | 544 | | */ |
520 | 545 | | @Override |
521 | 546 | | public boolean removeBuddy(XMPPResourceConnection session, JID jid) |
522 | | - | throws NotAuthorizedException, TigaseDBException { |
| 547 | + | throws NotAuthorizedException, TigaseDBException { |
523 | 548 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
524 | 549 | | |
525 | 550 | | if (log.isLoggable(Level.FINEST)) { |
526 | 551 | | log.log(Level.FINEST, "Removing roster buddy: {0}, before removal: {1}", |
527 | | - | new Object[] { jid, roster }); |
| 552 | + | new Object[] { jid, |
| 553 | + | roster }); |
528 | 554 | | } |
529 | 555 | | roster.remove(jid.getBareJID()); |
530 | 556 | | if (log.isLoggable(Level.FINEST)) { |
531 | 557 | | log.log(Level.FINEST, "Removing roster buddy: {0}, after removal: {1}", |
532 | | - | new Object[] { jid, roster }); |
| 558 | + | new Object[] { jid, |
| 559 | + | roster }); |
533 | 560 | | } |
534 | 561 | | saveUserRoster(session); |
535 | 562 | | |
536 | 563 | | return true; |
537 | 564 | | } |
538 | 565 | | |
| 566 | + | //~--- set methods ---------------------------------------------------------- |
| 567 | + | |
539 | 568 | | // ~--- set methods ---------------------------------------------------------- |
540 | 569 | | |
541 | 570 | | /** |
| skipped 9 lines |
551 | 580 | | */ |
552 | 581 | | @Override |
553 | 582 | | public void setBuddyName(XMPPResourceConnection session, JID buddy, String name) |
554 | | - | throws NotAuthorizedException, TigaseDBException { |
| 583 | + | throws NotAuthorizedException, TigaseDBException { |
555 | 584 | | RosterElement relem = getRosterElement(session, buddy); |
556 | 585 | | |
557 | 586 | | if (relem != null) { |
558 | 587 | | if (log.isLoggable(Level.FINEST)) { |
559 | | - | log.log(Level.FINEST, "Setting name: ''{0}'' for buddy: {1}", new Object[] { |
560 | | - | name, buddy }); |
| 588 | + | log.log(Level.FINEST, "Setting name: ''{0}'' for buddy: {1}", new Object[] { name, |
| 589 | + | buddy }); |
561 | 590 | | } |
562 | | - | |
563 | | - | if ((name != null) && !name.isEmpty()) { |
| 591 | + | if ((name != null) &&!name.isEmpty()) { |
564 | 592 | | relem.setName(name); |
565 | 593 | | } |
566 | | - | |
567 | 594 | | saveUserRoster(session); |
568 | 595 | | } else { |
569 | 596 | | log.log(Level.WARNING, "Setting buddy name for non-existen contact: {0}", buddy); |
| skipped 13 lines |
583 | 610 | | */ |
584 | 611 | | @Override |
585 | 612 | | public void setBuddySubscription(XMPPResourceConnection session, |
586 | | - | SubscriptionType subscription, JID buddy) throws NotAuthorizedException, |
587 | | - | TigaseDBException { |
| 613 | + | SubscriptionType subscription, JID buddy) |
| 614 | + | throws NotAuthorizedException, TigaseDBException { |
588 | 615 | | RosterElement relem = getRosterElement(session, buddy); |
589 | 616 | | |
590 | 617 | | if (relem != null) { |
| skipped 17 lines |
608 | 635 | | */ |
609 | 636 | | @Override |
610 | 637 | | public void setOnline(XMPPResourceConnection session, JID buddy, boolean online) |
611 | | - | throws NotAuthorizedException, TigaseDBException { |
| 638 | + | throws NotAuthorizedException, TigaseDBException { |
612 | 639 | | RosterElement relem = getRosterElement(session, buddy); |
613 | 640 | | |
614 | 641 | | if (relem == null) { |
615 | 642 | | relem = addTempBuddy(buddy, session); |
616 | 643 | | } |
617 | | - | |
618 | 644 | | relem.setOnline(buddy.getResource(), online); |
619 | 645 | | } |
620 | 646 | | |
| skipped 10 lines |
631 | 657 | | */ |
632 | 658 | | @Override |
633 | 659 | | public void setPresenceSent(XMPPResourceConnection session, JID buddy, boolean sent) |
634 | | - | throws NotAuthorizedException, TigaseDBException { |
| 660 | + | throws NotAuthorizedException, TigaseDBException { |
635 | 661 | | RosterElement relem = getRosterElement(session, buddy); |
636 | 662 | | |
637 | 663 | | if (relem == null) { |
| skipped 1 lines |
639 | 665 | | } |
640 | 666 | | relem.setPresence_sent(sent); |
641 | 667 | | } |
| 668 | + | |
| 669 | + | //~--- get methods ---------------------------------------------------------- |
642 | 670 | | |
643 | 671 | | // ~--- get methods ---------------------------------------------------------- |
644 | 672 | | |
| 673 | + | /** |
| 674 | + | * Method description |
| 675 | + | * |
| 676 | + | * |
| 677 | + | * @param session |
| 678 | + | * @param buddy |
| 679 | + | * |
| 680 | + | * @return |
| 681 | + | * |
| 682 | + | * @throws NotAuthorizedException |
| 683 | + | * @throws TigaseDBException |
| 684 | + | */ |
645 | 685 | | public RosterElement getRosterElement(XMPPResourceConnection session, JID buddy) |
646 | | - | throws NotAuthorizedException, TigaseDBException { |
| 686 | + | throws NotAuthorizedException, TigaseDBException { |
647 | 687 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
648 | 688 | | |
649 | 689 | | return roster.get(buddy.getBareJID()); |
650 | 690 | | } |
651 | 691 | | |
| 692 | + | /** |
| 693 | + | * Method description |
| 694 | + | * |
| 695 | + | * |
| 696 | + | * @param session |
| 697 | + | * |
| 698 | + | * @return |
| 699 | + | * |
| 700 | + | * @throws NotAuthorizedException |
| 701 | + | * @throws TigaseDBException |
| 702 | + | */ |
652 | 703 | | @SuppressWarnings({ "unchecked" }) |
653 | 704 | | protected Map<BareJID, RosterElement> getUserRoster(XMPPResourceConnection session) |
654 | | - | throws NotAuthorizedException, TigaseDBException { |
| 705 | + | throws NotAuthorizedException, TigaseDBException { |
655 | 706 | | Map<BareJID, RosterElement> roster = null; |
656 | 707 | | |
657 | 708 | | // The method can be called from different plugins concurrently. |
| skipped 1 lines |
659 | 710 | | // access problems |
660 | 711 | | synchronized (session) { |
661 | 712 | | roster = (Map<BareJID, RosterElement>) session.getCommonSessionData(ROSTER); |
662 | | - | |
663 | 713 | | if (roster == null) { |
664 | 714 | | roster = loadUserRoster(session); |
665 | 715 | | } |
| skipped 2 lines |
668 | 718 | | return roster; |
669 | 719 | | } |
670 | 720 | | |
| 721 | + | //~--- methods -------------------------------------------------------------- |
| 722 | + | |
671 | 723 | | // ~--- methods -------------------------------------------------------------- |
672 | 724 | | |
| 725 | + | /** |
| 726 | + | * Method description |
| 727 | + | * |
| 728 | + | * |
| 729 | + | * @param session |
| 730 | + | * |
| 731 | + | * @throws NotAuthorizedException |
| 732 | + | * @throws TigaseDBException |
| 733 | + | */ |
673 | 734 | | protected void saveUserRoster(XMPPResourceConnection session) |
674 | | - | throws NotAuthorizedException, TigaseDBException { |
| 735 | + | throws NotAuthorizedException, TigaseDBException { |
675 | 736 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
676 | | - | StringBuilder sb = new StringBuilder(5000); |
| 737 | + | StringBuilder sb = new StringBuilder(5000); |
677 | 738 | | |
678 | 739 | | for (RosterElement relem : roster.values()) { |
679 | 740 | | if (relem.isPersistent()) { |
680 | 741 | | sb.append(relem.getRosterElement().toString()); |
681 | 742 | | } |
682 | 743 | | } |
683 | | - | |
684 | 744 | | if (log.isLoggable(Level.FINEST)) { |
685 | 745 | | log.log(Level.FINEST, "Saving user roster: {0}", sb); |
686 | 746 | | } |
687 | | - | |
688 | 747 | | session.setData(null, ROSTER, sb.toString()); |
689 | 748 | | } |
690 | 749 | | |
691 | 750 | | private Map<BareJID, RosterElement> loadUserRoster(XMPPResourceConnection session) |
692 | | - | throws NotAuthorizedException, TigaseDBException { |
| 751 | + | throws NotAuthorizedException, TigaseDBException { |
693 | 752 | | |
694 | 753 | | // In most times we just read from this data structure |
695 | 754 | | // From time to time there might be some modification, posibly concurrent |
696 | 755 | | // very unlikely by more than one thread |
697 | | - | Map<BareJID, RosterElement> roster = |
698 | | - | new ConcurrentHashMap<BareJID, RosterElement>(100, 0.25f, 1); |
| 756 | + | Map<BareJID, RosterElement> roster = new ConcurrentHashMap<BareJID, |
| 757 | + | RosterElement>(100, 0.25f, 1); |
699 | 758 | | |
700 | 759 | | session.putCommonSessionData(ROSTER, roster); |
701 | 760 | | |
| skipped 2 lines |
704 | 763 | | if (log.isLoggable(Level.FINEST)) { |
705 | 764 | | log.log(Level.FINEST, "Loaded user roster: {0}", roster_str); |
706 | 765 | | } |
707 | | - | |
708 | | - | if ((roster_str != null) && !roster_str.isEmpty()) { |
| 766 | + | if ((roster_str != null) &&!roster_str.isEmpty()) { |
709 | 767 | | updateRosterHash(roster_str, session); |
710 | 768 | | |
711 | 769 | | boolean modified = parseRoster(roster_str, roster, session); |
| skipped 6 lines |
718 | 776 | | // Try to load a roster from the 'old' style roster storage and |
719 | 777 | | // convert it the the flat roster storage |
720 | 778 | | Roster oldRoster = new Roster(); |
721 | | - | JID[] buddies = oldRoster.getBuddies(session); |
| 779 | + | JID[] buddies = oldRoster.getBuddies(session); |
722 | 780 | | |
723 | 781 | | if ((buddies != null) && (buddies.length > 0)) { |
724 | 782 | | for (JID buddy : buddies) { |
725 | | - | String name = oldRoster.getBuddyName(session, buddy); |
| 783 | + | String name = oldRoster.getBuddyName(session, buddy); |
726 | 784 | | SubscriptionType subscr = oldRoster.getBuddySubscription(session, buddy); |
727 | | - | String[] groups = oldRoster.getBuddyGroups(session, buddy); |
728 | | - | RosterElement relem = getRosterElementInstance(buddy, name, groups, session); |
| 785 | + | String[] groups = oldRoster.getBuddyGroups(session, buddy); |
| 786 | + | RosterElement relem = getRosterElementInstance(buddy, name, groups, |
| 787 | + | session); |
729 | 788 | | |
730 | 789 | | relem.setSubscription(subscr); |
731 | | - | |
732 | 790 | | if (!addBuddy(relem, roster)) { |
733 | 791 | | break; |
734 | 792 | | } |
735 | 793 | | } |
736 | | - | |
737 | 794 | | saveUserRoster(session); |
738 | 795 | | } |
739 | 796 | | } |
| skipped 1 lines |
741 | 798 | | return roster; |
742 | 799 | | } |
743 | 800 | | |
744 | | - | // @Override |
745 | | - | // public void setBuddyOnline(XMPPResourceConnection session, String buddy, |
746 | | - | // boolean online) |
747 | | - | // throws NotAuthorizedException, TigaseDBException { |
748 | | - | // RosterElement relem = getRosterElement(session, buddy); |
749 | | - | // if (relem != null) { |
750 | | - | // relem.setOnline(online); |
751 | | - | // } |
752 | | - | // } |
753 | | - | // |
754 | | - | // @Override |
755 | | - | // public boolean isBuddyOnline(XMPPResourceConnection session, String buddy) |
756 | | - | // throws NotAuthorizedException, TigaseDBException { |
757 | | - | // RosterElement relem = getRosterElement(session, buddy); |
758 | | - | // if (relem != null) { |
759 | | - | // return relem.isOnline(); |
760 | | - | // } |
761 | | - | // return false; |
762 | | - | // } |
| 801 | + | //~--- get methods ---------------------------------------------------------- |
763 | 802 | | |
764 | | - | private class RosterElemComparator implements Comparator<JID> { |
765 | | - | |
766 | | - | private Map<BareJID, RosterElement> roster = null; |
767 | | - | |
768 | | - | private RosterElemComparator(Map<BareJID, RosterElement> roster) { |
769 | | - | this.roster = roster; |
770 | | - | } |
771 | | - | |
772 | | - | /* |
773 | | - | * (non-Javadoc) |
774 | | - | * |
775 | | - | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
776 | | - | */ |
777 | | - | @Override |
778 | | - | public int compare(JID arg0, JID arg1) { |
779 | | - | double w0 = roster.get(arg0.getBareJID()).getWeight(); |
780 | | - | double w1 = roster.get(arg1.getBareJID()).getWeight(); |
781 | | - | return Double.compare(w0, w1); |
782 | | - | } |
783 | | - | |
784 | | - | } |
785 | | - | |
| 803 | + | /** |
| 804 | + | * Method description |
| 805 | + | * |
| 806 | + | * |
| 807 | + | * @param session |
| 808 | + | * @param buddy |
| 809 | + | * |
| 810 | + | * @return |
| 811 | + | * |
| 812 | + | * @throws NotAuthorizedException |
| 813 | + | * @throws TigaseDBException |
| 814 | + | */ |
786 | 815 | | public String getCustomStatus(XMPPResourceConnection session, JID buddy) |
787 | | - | throws NotAuthorizedException, TigaseDBException { |
| 816 | + | throws NotAuthorizedException, TigaseDBException { |
788 | 817 | | RosterElement rel = getRosterElement(session, buddy); |
789 | | - | String result = null; |
| 818 | + | String result = null; |
| 819 | + | |
790 | 820 | | if (rel != null) { |
791 | 821 | | if (rel.getLastSeen() > RosterElement.INITIAL_LAST_SEEN_VAL) { |
792 | | - | result = |
793 | | - | "Buddy last seen on: " + new Date(rel.getLastSeen()) + ", weight: " |
794 | | - | + rel.getWeight(); |
| 822 | + | result = "Buddy last seen on: " + new Date(rel.getLastSeen()) + ", weight: " + |
| 823 | + | rel.getWeight(); |
795 | 824 | | } else { |
796 | 825 | | result = "Never seen"; |
797 | 826 | | } |
798 | 827 | | } |
| 828 | + | |
799 | 829 | | return result; |
800 | 830 | | } |
| 831 | + | |
| 832 | + | //~--- methods -------------------------------------------------------------- |
801 | 833 | | |
802 | 834 | | /* |
803 | 835 | | * (non-Javadoc) |
804 | 836 | | * |
805 | 837 | | * @see tigase.xmpp.impl.roster.RosterAbstract#logout() |
806 | 838 | | */ |
| 839 | + | |
| 840 | + | /** |
| 841 | + | * Method description |
| 842 | + | * |
| 843 | + | * |
| 844 | + | * @param session |
| 845 | + | */ |
807 | 846 | | @Override |
808 | 847 | | public void logout(XMPPResourceConnection session) { |
809 | 848 | | try { |
| skipped 1 lines |
811 | 850 | | saveUserRoster(session); |
812 | 851 | | } |
813 | 852 | | } catch (NotAuthorizedException ex) { |
| 853 | + | |
814 | 854 | | // TODO Auto-generated catch block |
815 | 855 | | ex.printStackTrace(); |
816 | 856 | | } catch (TigaseDBException ex) { |
| 857 | + | |
817 | 858 | | // TODO Auto-generated catch block |
818 | 859 | | ex.printStackTrace(); |
819 | 860 | | } |
820 | 861 | | } |
821 | 862 | | |
| 863 | + | //~--- get methods ---------------------------------------------------------- |
| 864 | + | |
822 | 865 | | /** |
823 | 866 | | * @param session |
824 | 867 | | * @return |
| skipped 1 lines |
826 | 869 | | * @throws NotAuthorizedException |
827 | 870 | | */ |
828 | 871 | | public boolean isModified(XMPPResourceConnection session) |
829 | | - | throws NotAuthorizedException, TigaseDBException { |
| 872 | + | throws NotAuthorizedException, TigaseDBException { |
830 | 873 | | Map<BareJID, RosterElement> roster = getUserRoster(session); |
831 | | - | boolean result = false; |
| 874 | + | boolean result = false; |
| 875 | + | |
832 | 876 | | if (roster != null) { |
833 | 877 | | for (RosterElement rel : roster.values()) { |
834 | 878 | | result |= rel.isModified(); |
835 | 879 | | } |
836 | 880 | | } |
| 881 | + | |
837 | 882 | | return result; |
838 | 883 | | } |
839 | 884 | | |
840 | | - | } // RosterFlat |
| 885 | + | //~--- inner classes -------------------------------------------------------- |
| 886 | + | |
| 887 | + | // @Override |
| 888 | + | // public void setBuddyOnline(XMPPResourceConnection session, String buddy, |
| 889 | + | // boolean online) |
| 890 | + | // throws NotAuthorizedException, TigaseDBException { |
| 891 | + | // RosterElement relem = getRosterElement(session, buddy); |
| 892 | + | // if (relem != null) { |
| 893 | + | // relem.setOnline(online); |
| 894 | + | // } |
| 895 | + | // } |
| 896 | + | // |
| 897 | + | // @Override |
| 898 | + | // public boolean isBuddyOnline(XMPPResourceConnection session, String buddy) |
| 899 | + | // throws NotAuthorizedException, TigaseDBException { |
| 900 | + | // RosterElement relem = getRosterElement(session, buddy); |
| 901 | + | // if (relem != null) { |
| 902 | + | // return relem.isOnline(); |
| 903 | + | // } |
| 904 | + | // return false; |
| 905 | + | // } |
| 906 | + | private class RosterElemComparator |
| 907 | + | implements Comparator<JID> { |
| 908 | + | private Map<BareJID, RosterElement> roster = null; |
| 909 | + | |
| 910 | + | //~--- constructors ------------------------------------------------------- |
| 911 | + | |
| 912 | + | private RosterElemComparator(Map<BareJID, RosterElement> roster) { |
| 913 | + | this.roster = roster; |
| 914 | + | } |
| 915 | + | |
| 916 | + | //~--- methods ------------------------------------------------------------ |
| 917 | + | |
| 918 | + | /* |
| 919 | + | * (non-Javadoc) |
| 920 | + | * |
| 921 | + | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
| 922 | + | */ |
| 923 | + | |
| 924 | + | /** |
| 925 | + | * Method description |
| 926 | + | * |
| 927 | + | * |
| 928 | + | * @param arg0 |
| 929 | + | * @param arg1 |
| 930 | + | * |
| 931 | + | * @return |
| 932 | + | */ |
| 933 | + | @Override |
| 934 | + | public int compare(JID arg0, JID arg1) { |
| 935 | + | double w0 = roster.get(arg0.getBareJID()).getWeight(); |
| 936 | + | double w1 = roster.get(arg1.getBareJID()).getWeight(); |
| 937 | + | |
| 938 | + | return Double.compare(w0, w1); |
| 939 | + | } |
| 940 | + | } |
| 941 | + | } // RosterFlat |
| 942 | + | |
| 943 | + | |
841 | 944 | | |
842 | 945 | | // ~ Formatted in Sun Code Convention |
843 | 946 | | |
844 | 947 | | // ~ Formatted by Jindent --- http://www.jindent.com |
845 | 948 | | |
| 949 | + | |
| 950 | + | //~ Formatted in Tigase Code Convention on 13/02/28 |
| 951 | + | |