restoreUserPresence(phoneUser.getUsername());
if (!restoreUserPresence(phoneUser.getUsername())) {    SessionManager sessionManager = XMPPServer.getInstance().getSessionManager(); Collection<ClientSession> sessions = sessionManager.getSessions(phoneUser.getUsername()); for (ClientSession session : sessions) { Presence presence = session.getPresence(); Element phoneStatusElement = presence.getElement().element("phone-status");  if (phoneStatusElement != null && PhoneStatus.Status.ON_PHONE.name().equals(phoneStatusElement.attributeValue("status"))) { Log.debug("Asterisk-IM HangupTask: User " + phoneUser.getUsername() + " has no more call sessions, but his presence is " + "still ON_PHONE. Changing to AVAILABLE");  phoneStatusElement.addAttribute("status", PhoneStatus.Status.AVAILABLE.name()); getInstance().getPresenceRouter().route(presence); } } }
        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);                // Send hang up message to user                sendHangupMessage(event.getUniqueId(), device, phoneUser.getUsername());                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. The                        // presence will be broadcasted to corresponding users                        restoreUserPresence(phoneUser.getUsername());                    }                    else {                        if (Log.isDebugEnabled()) {                            Log.debug("Asterisk-IM HangupTask: User " + phoneUser.getUsername() +                                    " has " + callSessionCount +                                    " call sessions,  not restoring presence. Destroying CallSession{id=" +                                    event.getUniqueId() + ", channel=" + event.getChannel() + "}");                        }                    }                    // finally destroy the session.                    callSessionFactory.destroyPhoneSession(event.getUniqueId());                    if (callSessionCount > 1) {                        for (CallSession session : callSessionFactory.getUserCallSessions(phoneUser.getUsername())) {                            Log.debug("Asterisk-IM HangupTask: Remaining CallSession " + session);                        }                    }                    // 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);            }        }