001    /**
002     * Copyright (c) 2000-2011 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                            _msn.login();
052    
053                            // Spend 5 seconds to attempt to login
054    
055                            for (int i = 0; i < 50 && !_msn.isLoggedIn(); i++) {
056                                    try {
057                                            Thread.sleep(100);
058                                    }
059                                    catch (InterruptedException e) {
060                                            _log.warn(e);
061    
062                                            break;
063                                    }
064                            }
065    
066                            if (!_msn.isLoggedIn()) {
067                                    _log.error("Unable to connect as " + _login);
068                            }
069                    }
070            }
071    
072            private void _disconnect() {
073                    try {
074                            if (_msn.isLoggedIn()) {
075                                    _msn.logout();
076                            }
077                    }
078                    catch (Exception e) {
079                            _log.warn(e);
080                    }
081            }
082    
083            private void _send(String to, String msg) {
084                    _connect();
085    
086                    _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
087    
088                    try {
089                            Thread.sleep(1500);
090    
091                            _msn.doCallWait(to);
092                    }
093                    catch (Exception e) {
094                            _log.warn(e);
095                    }
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(MSNConnector.class);
099    
100            private static MSNConnector _instance = new MSNConnector();
101    
102            private String _login;
103            private String _password;
104            private MSNMessenger _msn;
105    
106    }