001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.im;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.util.PropsUtil;
021    
022    import rath.msnm.MSNMessenger;
023    import rath.msnm.UserStatus;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Brett Randall
028     */
029    public class MSNConnector {
030    
031            public static void disconnect() {
032                    if (_instance != null) {
033                            _instance._disconnect();
034                    }
035            }
036    
037            public static void send(String to, String msg) {
038                    _instance._send(to, msg);
039            }
040    
041            private MSNConnector() {
042                    _login = PropsUtil.get(PropsKeys.MSN_LOGIN);
043                    _password = PropsUtil.get(PropsKeys.MSN_PASSWORD);
044    
045                    _msn = new MSNMessenger(_login, _password);
046                    _msn.setInitialStatus(UserStatus.ONLINE);
047            }
048    
049            private void _connect() {
050                    if (_msn.isLoggedIn()) {
051                            return;
052                    }
053    
054                    _msn.login();
055    
056                    // Spend 5 seconds to attempt to login
057    
058                    for (int i = 0; i < 50 && !_msn.isLoggedIn(); i++) {
059                            try {
060                                    Thread.sleep(100);
061                            }
062                            catch (InterruptedException ie) {
063                                    if (_log.isWarnEnabled()) {
064                                            _log.warn(ie);
065                                    }
066    
067                                    break;
068                            }
069                    }
070    
071                    if (!_msn.isLoggedIn()) {
072                            _log.error("Unable to connect as " + _login);
073                    }
074            }
075    
076            private void _disconnect() {
077                    try {
078                            if (_msn.isLoggedIn()) {
079                                    _msn.logout();
080                            }
081                    }
082                    catch (Exception e) {
083                            if (_log.isWarnEnabled()) {
084                                    if (_log.isWarnEnabled()) {
085                                            _log.warn(e);
086                                    }
087                            }
088                    }
089            }
090    
091            private void _send(String to, String msg) {
092                    _connect();
093    
094                    _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
095    
096                    try {
097                            Thread.sleep(1500);
098    
099                            _msn.doCallWait(to);
100                    }
101                    catch (Exception e) {
102                            if (_log.isWarnEnabled()) {
103                                    _log.warn(e);
104                            }
105                    }
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(MSNConnector.class);
109    
110            private static MSNConnector _instance = new MSNConnector();
111    
112            private String _login;
113            private MSNMessenger _msn;
114            private String _password;
115    
116    }