finally { close(phoneManager); }
        public void run() {            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;                }                CallSession callSession = getCallSessionFactory().getCallSession(event.getUniqueId(), phoneUser.getUsername());                if (callSession.getCallerID() != null) {                    // We have already reported a Dial event to the user, no need to do it gain                    return;                }                callSession.setChannel(event.getChannel());                Message message = new Message();                message.setID(event.getUniqueId());                message.setFrom(asteriskPlugin.getComponentJID());                String appData = event.getAppData();                String callerID;                if (appData != null) {                    if (appData.contains("Zap/")) {                        String[] tokens = appData.split("/");                        callerID = tokens[tokens.length - 1];                    }                    else if (appData.contains("IAX/") || appData.contains("SIP/")) {                        // Hopefully they used useful names like SIP/exten                        String name = getDevice(appData);                        // string may be like this SIP/6131&SIP/232|20                        int index = name.indexOf("|");                        if (index > 0) {                            name = name.substring(0, index);                        }                        // string may be like SIP/6131&SIP/232                        index = name.indexOf("&");                        if (index > 0) {                            name = name.substring(0, index);                        }                        // string will be like SIP/6131                        index = name.indexOf("/");                        name = name.substring(index + 1);                        callerID = name;                    }                    else {                        // Whatever it is use it (hack)                        callerID = appData;                    }                }                else {                    //finally just put something in there (hack)                    callerID = event.getUniqueId();                }                callSession.setCallerID(callerID);                PhoneEvent phoneEvent =                        new PhoneEvent(event.getUniqueId(), Type.DIALED, device);                message.getElement().add(phoneEvent);                phoneEvent.addElement("callerID").setText(callerID);                // 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);                }            }            catch (Exception e) {                Log.error(e.getMessage(), e);            }            finally {                close(phoneManager);            }        }