Commit 5db5e134 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added session events (JM-347) from Conor Hayes.

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2806 b35dd754-fafc-0310-a699-88a17e54d16e
parent 614868e4
...@@ -22,6 +22,7 @@ import org.jivesoftware.messenger.user.UserManager; ...@@ -22,6 +22,7 @@ import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException; import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.component.ComponentSession; import org.jivesoftware.messenger.component.ComponentSession;
import org.jivesoftware.messenger.component.InternalComponentManager; import org.jivesoftware.messenger.component.InternalComponentManager;
import org.jivesoftware.messenger.event.SessionEventDispatcher;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
...@@ -501,6 +502,11 @@ public class SessionManager extends BasicModule { ...@@ -501,6 +502,11 @@ public class SessionManager extends BasicModule {
// Remove the pre-Authenticated session but remember to use the temporary JID as the key // Remove the pre-Authenticated session but remember to use the temporary JID as the key
preAuthenticatedSessions.remove(new JID(null, session.getAddress().getDomain(), preAuthenticatedSessions.remove(new JID(null, session.getAddress().getDomain(),
session.getStreamID().toString()).toString()); session.getStreamID().toString()).toString());
// Fire session created event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.session_created);
success = true; success = true;
} }
return success; return success;
...@@ -841,6 +847,7 @@ public class SessionManager extends BasicModule { ...@@ -841,6 +847,7 @@ public class SessionManager extends BasicModule {
results); results);
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
// Ignore.
} }
} }
...@@ -1109,9 +1116,8 @@ public class SessionManager extends BasicModule { ...@@ -1109,9 +1116,8 @@ public class SessionManager extends BasicModule {
* @param packet The packet to be broadcast * @param packet The packet to be broadcast
*/ */
public void broadcast(Packet packet) throws UnauthorizedException { public void broadcast(Packet packet) throws UnauthorizedException {
Iterator values = sessions.values().iterator(); for (SessionMap sessionMap : sessions.values()) {
while (values.hasNext()) { ((SessionMap) sessionMap).broadcast(packet);
((SessionMap)values.next()).broadcast(packet);
} }
for (Session session : anonymousSessions.values()) { for (Session session : anonymousSessions.values()) {
...@@ -1150,6 +1156,10 @@ public class SessionManager extends BasicModule { ...@@ -1150,6 +1156,10 @@ public class SessionManager extends BasicModule {
if (anonymousSessions.containsValue(session)) { if (anonymousSessions.containsValue(session)) {
anonymousSessions.remove(session.getAddress().getResource()); anonymousSessions.remove(session.getAddress().getResource());
sessionCount--; sessionCount--;
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.anonymous_session_destroyed);
} }
else { else {
// If this is a non-anonymous session then remove the session from the SessionMap // If this is a non-anonymous session then remove the session from the SessionMap
...@@ -1163,6 +1173,10 @@ public class SessionManager extends BasicModule { ...@@ -1163,6 +1173,10 @@ public class SessionManager extends BasicModule {
if (sessionMap.isEmpty()) { if (sessionMap.isEmpty()) {
sessions.remove(username); sessions.remove(username);
} }
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.session_destroyed);
} }
} }
} }
...@@ -1186,6 +1200,10 @@ public class SessionManager extends BasicModule { ...@@ -1186,6 +1200,10 @@ public class SessionManager extends BasicModule {
anonymousSessions.put(session.getAddress().getResource(), session); anonymousSessions.put(session.getAddress().getResource(), session);
// Remove the session from the pre-Authenticated sessions list // Remove the session from the pre-Authenticated sessions list
preAuthenticatedSessions.remove(session.getAddress().toString()); preAuthenticatedSessions.remove(session.getAddress().toString());
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.anonymous_session_created);
} }
public int getConflictKickLimit() { public int getConflictKickLimit() {
...@@ -1372,6 +1390,7 @@ public class SessionManager extends BasicModule { ...@@ -1372,6 +1390,7 @@ public class SessionManager extends BasicModule {
} }
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
// Ignore.
} }
} }
...@@ -1398,10 +1417,12 @@ public class SessionManager extends BasicModule { ...@@ -1398,10 +1417,12 @@ public class SessionManager extends BasicModule {
session.getConnection().close(); session.getConnection().close();
} }
catch (Throwable t) { catch (Throwable t) {
// Ignore.
} }
} }
} }
catch (Exception e) { catch (Exception e) {
// Ignore.
} }
} }
......
/**
* $RCSfile: UserEventDispatcher.java,v $
* $Revision: 1.1 $
* $Date: 2005/04/10 18:21:58 $
*
* Copyright 2004 Jive Software.
*
* All rights reserved. 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 org.jivesoftware.messenger.event;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jivesoftware.messenger.Session;
import org.jivesoftware.util.Log;
/**
* Dispatches session events. Each event has a {@link EventType type}
*
* @author Matt Tucker
*/
public class SessionEventDispatcher {
private static List<SessionEventListener> listeners =
new CopyOnWriteArrayList<SessionEventListener>();
private SessionEventDispatcher() {
// Not instantiable.
}
/**
* Registers a listener to receive events.
*
* @param listener the listener.
*/
public static void addListener(SessionEventListener listener) {
if (listener == null) {
throw new NullPointerException();
}
listeners.add(listener);
}
/**
* Unregisters a listener to receive events.
*
* @param listener the listener.
*/
public static void removeListener(SessionEventListener listener) {
listeners.remove(listener);
}
/**
* Dispatches an event to all listeners.
*
* @param session the session.
* @param eventType the event type.
*/
public static void dispatchEvent(Session session, EventType eventType) {
for (SessionEventListener listener : listeners) {
try {
switch (eventType) {
case session_created: {
listener.sessionCreated(session);
break;
}
case session_destroyed: {
listener.sessionDestroyed(session);
break;
}
case anonymous_session_created: {
listener.anonymousSessionCreated(session);
break;
}
case anonymous_session_destroyed: {
listener.anonymousSessionDestroyed(session);
break;
}
default:
break;
}
}
catch (Exception e) {
Log.error(e);
}
}
}
/**
* Represents valid event types.
*/
public enum EventType {
/**
* A session was created.
*/
session_created,
/**
* A session was destroyed
*/
session_destroyed,
/**
* An anonymous session was created.
*/
anonymous_session_created,
/**
* A anonymous session was destroyed
*/
anonymous_session_destroyed,
}
}
\ No newline at end of file
/**
* Copyright (C) 2004-2005 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.messenger.event;
import org.jivesoftware.messenger.Session;
/**
* Interface to listen for session events. Use the
* {@link SessionEventDispatcher#addListener(SessionEventListener)}
* method to register for events.
*
* @author Matt Tucker
*/
public interface SessionEventListener {
/**
* A session was created.
*
* @param session the session.
*/
public void sessionCreated(Session session);
/**
* A session was destroyed
*
* @param session the session.
*/
public void sessionDestroyed(Session session);
/**
* An anonymous session was created.
*
* @param session the session.
*/
public void anonymousSessionCreated(Session session);
/**
* An anonymous session was created.
*
* @param session the session.
*/
public void anonymousSessionDestroyed(Session session);
}
\ No newline at end of file
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