001: /**
002: * $Revision$
003: * $Date$
004: *
005: * Copyright (C) 2006-2007 Jive Software. All rights reserved.
006: *
007: * This software is published under the terms of the GNU Public License (GPL),
008: * a copy of which is included in this distribution.
009: */package org.jivesoftware.openfire.gateway.muc;
010:
011: import org.jivesoftware.util.NotFoundException;
012: import org.jivesoftware.openfire.gateway.session.TransportSession;
013:
014: import java.util.Collection;
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.lang.ref.WeakReference;
018:
019: /**
020: * @author Daniel Henninger
021: */
022: public class MUCTransportSessionManager {
023:
024: /**
025: * Creates a MUC session manager for a transport session.
026: *
027: * @param session Transport session we are attached to.
028: */
029: public MUCTransportSessionManager(TransportSession session) {
030: this .transportSessionRef = new WeakReference<TransportSession>(
031: session);
032: }
033:
034: /* The transport session we are attached to */
035: public WeakReference<TransportSession> transportSessionRef;
036:
037: /**
038: * Retrieve the transport session the manager is associated with.
039: *
040: * @return transport session manager is associated with.
041: */
042: public TransportSession getTransportSession() {
043: return transportSessionRef.get();
044: }
045:
046: /**
047: * Container for all active sessions.
048: */
049: private Map<String, MUCTransportSession> activeSessions = new HashMap<String, MUCTransportSession>();
050:
051: /**
052: * Retrieve the session instance for a given JID.
053: *
054: * Ignores the resource part of the jid.
055: *
056: * @param roomname Room name of the instance to be retrieved.
057: * @throws NotFoundException if the given jid is not found.
058: * @return MUCTransportSession instance requested.
059: */
060: public MUCTransportSession getSession(String roomname)
061: throws NotFoundException {
062: MUCTransportSession session = activeSessions.get(roomname
063: .toLowerCase());
064: if (session == null) {
065: throw new NotFoundException(
066: "Could not find session requested.");
067: }
068: return session;
069: }
070:
071: /**
072: * Stores a new session instance with the legacy service.
073: *
074: * Expects to be given a JID and a pre-created session. Ignores the
075: * resource part of the JID.
076: *
077: * @param roomname Room name used to track the session.
078: * @param session MUCTransportSession associated with the jid.
079: */
080: public void storeSession(String roomname,
081: MUCTransportSession session) {
082: activeSessions.put(roomname.toLowerCase(), session);
083: }
084:
085: /**
086: * Removes a session instance with the legacy service.
087: *
088: * Expects to be given a JID which indicates which session we are
089: * removing.
090: *
091: * @param roomname Room name to be removed.
092: */
093: public void removeSession(String roomname) {
094: activeSessions.remove(roomname.toLowerCase());
095: }
096:
097: /**
098: * Retrieves a collection of all active sessions.
099: *
100: * @return List of active sessions.
101: */
102: public Collection<MUCTransportSession> getSessions() {
103: return activeSessions.values();
104: }
105:
106: }
|