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                                    _log.warn(ie);
064    
065                                    break;
066                            }
067                    }
068    
069                    if (!_msn.isLoggedIn()) {
070                            _log.error("Unable to connect as " + _login);
071                    }
072            }
073    
074            private void _disconnect() {
075                    try {
076                            if (_msn.isLoggedIn()) {
077                                    _msn.logout();
078                            }
079                    }
080                    catch (Exception e) {
081                            _log.warn(e);
082                    }
083            }
084    
085            private void _send(String to, String msg) {
086                    _connect();
087    
088                    _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
089    
090                    try {
091                            Thread.sleep(1500);
092    
093                            _msn.doCallWait(to);
094                    }
095                    catch (Exception e) {
096                            _log.warn(e);
097                    }
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(MSNConnector.class);
101    
102            private static MSNConnector _instance = new MSNConnector();
103    
104            private String _login;
105            private MSNMessenger _msn;
106            private String _password;
107    
108    }