001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }