if (callSessionCount > 1) { for (CallSession session : callSessionFactory.getUserCallSessions(phoneUser.getUsername())) { Log.debug("Asterisk-IM HangupTask: Remaining CallSession " + session); } }
        public void run() {            //everything after the hyphen should be skipped            String device = getDevice(event.getChannel());            PhoneManager phoneManager = null;            try {                phoneManager = getPhoneManager();                PhoneUser phoneUser = phoneManager.getByDevice(device);                //If there is no jid for this device don't do anything else                if (phoneUser == null) {                    return;                }                Log.debug("Asterisk-IM HangupTask called for user " + phoneUser);                Message message = new Message();                message.setFrom(asteriskPlugin.getComponentJID());                message.setID(event.getUniqueId());                PhoneEvent phoneEvent =                        new PhoneEvent(event.getUniqueId(), Type.HANG_UP, device);                message.getElement().add(phoneEvent);                // Send the message to each of jids for this user                SessionManager sessionManager = XMPPServer.getInstance().getSessionManager();                Collection<ClientSession> sessions = sessionManager.getSessions(phoneUser.getUsername());                for (ClientSession session : sessions) {                    message.setTo(session.getAddress());                    asteriskPlugin.sendPacket(message);                }                CallSessionFactory callSessionFactory = getCallSessionFactory();                // If the user does not have any more call sessions, set back                // the presence to what it was before they received any calls                synchronized (phoneUser.getUsername().intern()) {                    int callSessionCount = callSessionFactory.getUserCallSessions(phoneUser.getUsername()).size();                    if (callSessionCount <= 1) {                        // Set the user's presence back to what it was before the phone call                        Collection<Presence> presences = UserPresenceUtil.removePresences(phoneUser.getUsername());                        if (presences != null) {                            for (Presence presence : presences) {                                Element presenceElement = presence.getElement();                                Element phoneStatusElement = presenceElement.element("phone-status");                                // If the phone-status attribute exists check to see if the status is avaialbable                                if (phoneStatusElement != null) {                                    Attribute statusAtt = phoneStatusElement.attribute("status");                                    if (!Status.AVAILABLE.name().equals(statusAtt.getText())) {                                        statusAtt.setText(Status.AVAILABLE.name());                                    }                                }                                // The attribute doesn't exist add new attribute                                else {                                    PhoneStatus status = new PhoneStatus(Status.AVAILABLE);                                    presence.getElement().add(status);                                }                                getInstance().getPresenceRouter().route(presence);                            }                        }                    }                    else {                        Log.debug("Asterisk-IM HangupTask: User " + phoneUser.getUsername() + " has "                                + callSessionCount + " call sessions,  not changing presence");                    }                    // finally destroy the session.                    callSessionFactory.destroyPhoneSession(event.getUniqueId());                    // just in case this was a fake session, kill the fake session.                    // This should be ok to do, since noone should be orginating a call and hanging up at the same time                    // for a device                    callSessionFactory.destroyPhoneSession(device);                }            }            catch (Exception e) {                Log.error(e.getMessage(), e);            }            finally {                close(phoneManager);            }        }