Commit e928da83 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@583 b35dd754-fafc-0310-a699-88a17e54d16e
parent 09e024e4
...@@ -14,6 +14,7 @@ package org.jivesoftware.messenger.user; ...@@ -14,6 +14,7 @@ package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.Presence; import org.jivesoftware.messenger.Presence;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Cacheable; import org.jivesoftware.util.Cacheable;
import org.xmpp.packet.Presence;
/** /**
* <p>A Roster that is cached in memory and persisted to some backend storage system.</p> * <p>A Roster that is cached in memory and persisted to some backend storage system.</p>
......
...@@ -11,16 +11,33 @@ ...@@ -11,16 +11,33 @@
package org.jivesoftware.messenger.user.spi; package org.jivesoftware.messenger.user.spi;
import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.messenger.container.ServiceLookupFactory;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.*;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import org.jivesoftware.messenger.ChannelHandler;
import org.jivesoftware.messenger.PresenceManager;
import org.jivesoftware.messenger.RoutingTable;
import org.jivesoftware.messenger.SessionManager;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.ServiceLookupFactory;
import org.jivesoftware.messenger.user.BasicRoster;
import org.jivesoftware.messenger.user.BasicRosterItem;
import org.jivesoftware.messenger.user.CachedRoster;
import org.jivesoftware.messenger.user.CachedRosterItem;
import org.jivesoftware.messenger.user.IQRoster;
import org.jivesoftware.messenger.user.IQRosterItem;
import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.messenger.user.RosterItemProvider;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.UserProviderFactory;
import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
/** /**
* <p>A roster implemented against a JDBC database.</p> * <p>A roster implemented against a JDBC database.</p>
...@@ -36,6 +53,10 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -36,6 +53,10 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
private RosterItemProvider rosterItemProvider; private RosterItemProvider rosterItemProvider;
private String username; private String username;
private SessionManager sessionManager;
private XMPPServer server;
private RoutingTable routingTable;
/** /**
* <p>Create a roster for the given user, pulling the existing roster items * <p>Create a roster for the given user, pulling the existing roster items
...@@ -44,12 +65,14 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -44,12 +65,14 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
* @param username The username of the user that owns this roster * @param username The username of the user that owns this roster
*/ */
public CachedRosterImpl(String username) { public CachedRosterImpl(String username) {
sessionManager = SessionManager.getInstance();
this.username = username; this.username = username;
rosterItemProvider = UserProviderFactory.getRosterItemProvider(); rosterItemProvider = UserProviderFactory.getRosterItemProvider();
Iterator items = rosterItemProvider.getItems(username); Iterator items = rosterItemProvider.getItems(username);
while (items.hasNext()) { while (items.hasNext()) {
RosterItem item = (RosterItem)items.next(); RosterItem item = (RosterItem)items.next();
rosterItems.put(item.getJid().toBareStringPrep(), item); rosterItems.put(item.getJid().toBareJID(), item);
} }
} }
...@@ -88,12 +111,9 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -88,12 +111,9 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
RosterItem item = (RosterItem)items.next(); RosterItem item = (RosterItem)items.next();
if (item.getSubStatus() == RosterItem.SUB_BOTH if (item.getSubStatus() == RosterItem.SUB_BOTH
|| item.getSubStatus() == RosterItem.SUB_FROM) { || item.getSubStatus() == RosterItem.SUB_FROM) {
XMPPAddress searchNode = JID searchNode = new JID(item.getJid().getNode(), item.getJid().getDomain(), null);
new XMPPAddress(item.getJid().getName(),
item.getJid().getHost(),
null);
Iterator sessions = routingTable.getRoutes(searchNode); Iterator sessions = routingTable.getRoutes(searchNode);
packet.setRecipient(item.getJid()); packet.setTo(item.getJid());
while (sessions.hasNext()) { while (sessions.hasNext()) {
ChannelHandler session = (ChannelHandler)sessions.next(); ChannelHandler session = (ChannelHandler)sessions.next();
try { try {
...@@ -112,18 +132,16 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -112,18 +132,16 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
} }
} }
protected RosterItem provideRosterItem(XMPPAddress user, String nickname, List group) protected RosterItem provideRosterItem(JID user, String nickname, List group) throws UserAlreadyExistsException, UnauthorizedException {
throws UserAlreadyExistsException, UnauthorizedException {
return provideRosterItem(new BasicRosterItem(user, nickname, group)); return provideRosterItem(new BasicRosterItem(user, nickname, group));
} }
protected RosterItem provideRosterItem(RosterItem item) protected RosterItem provideRosterItem(RosterItem item) throws UserAlreadyExistsException, UnauthorizedException {
throws UserAlreadyExistsException, UnauthorizedException {
item = rosterItemProvider.createItem(username, new BasicRosterItem(item)); item = rosterItemProvider.createItem(username, new BasicRosterItem(item));
// Broadcast the roster push to the user // Broadcast the roster push to the user
IQRoster roster = new IQRoster(); IQRoster roster = new IQRoster();
roster.setType(IQ.SET); roster.setType(IQ.Type.set);
roster.createRosterItem(item); roster.createRosterItem(item);
broadcast(roster); broadcast(roster);
...@@ -156,7 +174,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -156,7 +174,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
try { try {
IQRoster roster = new IQRoster(); IQRoster roster = new IQRoster();
roster.setType(IQ.SET); roster.setType(IQ.Type.set);
roster.createRosterItem(cachedItem); roster.createRosterItem(cachedItem);
broadcast(roster); broadcast(roster);
} }
...@@ -174,7 +192,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -174,7 +192,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
} }
} }
public synchronized RosterItem deleteRosterItem(XMPPAddress user) throws UnauthorizedException { public synchronized RosterItem deleteRosterItem(JID user) throws UnauthorizedException {
// Note that the super cache will always only hold cached roster items // Note that the super cache will always only hold cached roster items
CachedRosterItem item = (CachedRosterItem)super.deleteRosterItem(user); CachedRosterItem item = (CachedRosterItem)super.deleteRosterItem(user);
...@@ -185,7 +203,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -185,7 +203,7 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
try { try {
// broadcast the update to the user // broadcast the update to the user
IQRoster roster = new IQRoster(); IQRoster roster = new IQRoster();
roster.setType(IQ.SET); roster.setType(IQ.Type.set);
IQRosterItem iqItem = (IQRosterItem)roster.createRosterItem(user); IQRosterItem iqItem = (IQRosterItem)roster.createRosterItem(user);
iqItem.setSubStatus(RosterItem.SUB_REMOVE); iqItem.setSubStatus(RosterItem.SUB_REMOVE);
broadcast(roster); broadcast(roster);
...@@ -197,20 +215,17 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster { ...@@ -197,20 +215,17 @@ public class CachedRosterImpl extends BasicRoster implements CachedRoster {
return item; return item;
} }
private SessionManager sessionManager;
private XMPPServer server;
private RoutingTable routingTable;
private void broadcast(IQRoster roster) throws UnauthorizedException { private void broadcast(IQRoster roster) throws UnauthorizedException {
try { try {
if (server == null) { if (server == null) {
server = (XMPPServer)ServiceLookupFactory.getLookup().lookup(XMPPServer.class); server = (XMPPServer)ServiceLookupFactory.getLookup().lookup(XMPPServer.class);
} }
XMPPAddress recipient = server.createJID(username, null); JID recipient = server.createJID(username, null);
roster.setRecipient(recipient); roster.setTo(recipient);
roster.setOriginatingSession(server.getSession());
if (sessionManager == null) { if (sessionManager == null) {
sessionManager = (SessionManager)ServiceLookupFactory.getLookup().lookup(SessionManager.class); sessionManager = SessionManager.getInstance();
} }
sessionManager.userBroadcast(username, roster); sessionManager.userBroadcast(username, roster);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment