Commit 28cc412b authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Modified to take advantage of Generics.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@762 b35dd754-fafc-0310-a699-88a17e54d16e
parent 3ff3a501
...@@ -125,14 +125,14 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -125,14 +125,14 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
* have no associated user, and don't follow the normal routing rules for * have no associated user, and don't follow the normal routing rules for
* priority based fall over. * priority based fall over.
*/ */
private Map anonymousSessions = new HashMap(); private Map<String, Session> anonymousSessions = new HashMap<String, Session>();
/** /**
* Simple data structure to track sessions for a single user (tracked by resource * Simple data structure to track sessions for a single user (tracked by resource
* and priority). * and priority).
*/ */
private class SessionMap { private class SessionMap {
private Map resources = new HashMap(); private Map<String,Session> resources = new HashMap<String,Session>();
private LinkedList priorityList = new LinkedList(); private LinkedList priorityList = new LinkedList();
/** /**
...@@ -159,7 +159,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -159,7 +159,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
if (priorityList.size() > 0) { if (priorityList.size() > 0) {
Iterator iter = priorityList.iterator(); Iterator iter = priorityList.iterator();
for (int i = 0; iter.hasNext(); i++) { for (int i = 0; iter.hasNext(); i++) {
Session sess = (Session)resources.get(iter.next()); Session sess = resources.get(iter.next());
if (sess.getPresence().getPriority() <= priority) { if (sess.getPresence().getPriority() <= priority) {
priorityList.add(i, resource); priorityList.add(i, resource);
break; break;
...@@ -203,7 +203,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -203,7 +203,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
* @return The session for that resource or null if none found (use getDefaultSession() to obtain default) * @return The session for that resource or null if none found (use getDefaultSession() to obtain default)
*/ */
Session getSession(String resource) { Session getSession(String resource) {
return (Session)resources.get(resource); return resources.get(resource);
} }
/** /**
...@@ -228,7 +228,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -228,7 +228,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
} }
// return (Session) resources.get(priorityList.getFirst()); // return (Session) resources.get(priorityList.getFirst());
Session s = (Session)resources.get(priorityList.getFirst()); Session s = resources.get(priorityList.getFirst());
return s; return s;
} }
...@@ -247,9 +247,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -247,9 +247,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
* @param packet * @param packet
*/ */
private void broadcast(Packet packet) throws UnauthorizedException, PacketException { private void broadcast(Packet packet) throws UnauthorizedException, PacketException {
Iterator entries = resources.values().iterator(); for (Session session : resources.values()) {
while (entries.hasNext()) {
Session session = (Session)entries.next();
packet.setTo(session.getAddress()); packet.setTo(session.getAddress());
session.getConnection().deliver(packet); session.getConnection().deliver(packet);
} }
...@@ -263,10 +261,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -263,10 +261,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
* @return An iterator of all sessions * @return An iterator of all sessions
*/ */
public Iterator getSessions() { public Iterator getSessions() {
LinkedList list = new LinkedList(); LinkedList<Session> list = new LinkedList<Session>();
Iterator entries = resources.values().iterator(); for (Session session : resources.values()) {
while (entries.hasNext()) { list.add(session);
list.add(entries.next());
} }
return list.iterator(); return list.iterator();
} }
...@@ -303,7 +300,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -303,7 +300,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
String username = session.getAddress().getNode().toLowerCase(); String username = session.getAddress().getNode().toLowerCase();
SessionMap resources = null; SessionMap resources = null;
try { try {
resources = (SessionMap)sessions.get(username); resources = sessions.get(username);
if (resources == null) { if (resources == null) {
resources = new SessionMap(); resources = new SessionMap();
sessions.put(username, resources); sessions.put(username, resources);
...@@ -343,7 +340,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -343,7 +340,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
String username = sender.getNode().toLowerCase(); String username = sender.getNode().toLowerCase();
sessionLock.writeLock().lock(); sessionLock.writeLock().lock();
try { try {
SessionMap resources = (SessionMap)sessions.get(username); SessionMap resources = sessions.get(username);
if (resources == null) { if (resources == null) {
return; return;
} }
...@@ -380,7 +377,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -380,7 +377,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
if (resource != null) { if (resource != null) {
anonymousSessionLock.readLock().lock(); anonymousSessionLock.readLock().lock();
try { try {
session = (Session)anonymousSessions.get(resource); session = anonymousSessions.get(resource);
if(session == null){ if(session == null){
session = getSession(recipient); session = getSession(recipient);
} }
...@@ -394,7 +391,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -394,7 +391,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
username = username.toLowerCase(); username = username.toLowerCase();
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
if (resource == null) { if (resource == null) {
session = sessionMap.getDefaultSession(); session = sessionMap.getDefaultSession();
...@@ -435,7 +432,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -435,7 +432,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
Session session = null; Session session = null;
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
if (resource == null) { if (resource == null) {
hasRoute = !sessionMap.isEmpty(); hasRoute = !sessionMap.isEmpty();
...@@ -483,7 +480,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -483,7 +480,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
if (username == null || "".equals(username)) { if (username == null || "".equals(username)) {
anonymousSessionLock.readLock().lock(); anonymousSessionLock.readLock().lock();
try { try {
session = (Session)anonymousSessions.get(resource); session = anonymousSessions.get(resource);
} }
finally { finally {
anonymousSessionLock.readLock().unlock(); anonymousSessionLock.readLock().unlock();
...@@ -493,7 +490,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -493,7 +490,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
username = username.toLowerCase(); username = username.toLowerCase();
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
session = sessionMap.getSession(resource); session = sessionMap.getSession(resource);
} }
...@@ -654,9 +651,8 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -654,9 +651,8 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
// Add anonymous sessions // Add anonymous sessions
anonymousSessionLock.readLock().lock(); anonymousSessionLock.readLock().lock();
try { try {
Iterator sessionItr = anonymousSessions.values().iterator(); for (Session session : anonymousSessions.values()) {
while (sessionItr.hasNext()) { sessions.add(session);
sessions.add(sessionItr.next());
} }
} }
finally { finally {
...@@ -685,7 +681,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -685,7 +681,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
// Get a copy of the sessions from all users // Get a copy of the sessions from all users
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
Iterator sessionItr = sessionMap.getSessions(); Iterator sessionItr = sessionMap.getSessions();
while (sessionItr.hasNext()) { while (sessionItr.hasNext()) {
...@@ -732,7 +728,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -732,7 +728,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
int sessionCount = 0; int sessionCount = 0;
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
sessionCount = sessionMap.resources.size(); sessionCount = sessionMap.resources.size();
} }
...@@ -766,9 +762,8 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -766,9 +762,8 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
} }
anonymousSessionLock.readLock().lock(); anonymousSessionLock.readLock().lock();
try { try {
Iterator values = anonymousSessions.values().iterator(); for (Session session : anonymousSessions.values()) {
while (values.hasNext()) { session.getConnection().deliver(packet);
((Session)values.next()).getConnection().deliver(packet);
} }
} }
finally { finally {
...@@ -786,7 +781,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -786,7 +781,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
public void userBroadcast(String username, Packet packet) throws UnauthorizedException, PacketException { public void userBroadcast(String username, Packet packet) throws UnauthorizedException, PacketException {
sessionLock.readLock().lock(); sessionLock.readLock().lock();
try { try {
SessionMap sessionMap = (SessionMap)sessions.get(username); SessionMap sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
sessionMap.broadcast(packet); sessionMap.broadcast(packet);
} }
...@@ -822,7 +817,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -822,7 +817,7 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
String username = session.getAddress().getNode().toLowerCase(); String username = session.getAddress().getNode().toLowerCase();
sessionLock.writeLock().lock(); sessionLock.writeLock().lock();
try { try {
sessionMap = (SessionMap)sessions.get(username); sessionMap = sessions.get(username);
if (sessionMap != null) { if (sessionMap != null) {
sessionMap.removeSession(session); sessionMap.removeSession(session);
sessionCount--; sessionCount--;
......
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