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 org.walluck.oscar.AIMConnection;
023    import org.walluck.oscar.AIMSession;
024    import org.walluck.oscar.client.Oscar;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Brett Randall
029     * @author Bruno Farache
030     */
031    public class AIMConnector {
032    
033            public static void disconnect() {
034                    if (_instance != null) {
035                            _instance._disconnect();
036                    }
037            }
038    
039            public static void send(String to, String msg) {
040                    _instance._send(to, msg);
041            }
042    
043            private AIMConnector() {
044            }
045    
046            private void _connect() {
047                    String login = PropsUtil.get(PropsKeys.AIM_LOGIN);
048                    String password = PropsUtil.get(PropsKeys.AIM_PASSWORD);
049    
050                    AIMSession ses = new AIMSession();
051    
052                    ses.setSN(login);
053    
054                    Oscar oscar = new Oscar();
055    
056                    oscar.setSN(login);
057                    oscar.setPassword(password);
058    
059                    ses.init();
060            }
061    
062            private void _disconnect() {
063                    if (_aim != null) {
064                            AIMConnection.killAllInSess(_aim);
065                    }
066            }
067    
068            private void _send(String to, String msg) {
069                    try {
070                            if (_aim == null) {
071                                    _connect();
072    
073                                    // Daim's listeners are buggy. Instead, just wait a second
074                                    // before sending the first message.
075    
076                                    Thread.sleep(1000);
077                            }
078    
079                            _oscar.sendIM(_aim, to, msg, Oscar.getICQCaps());
080                    }
081                    catch (Exception e) {
082                            if (_log.isWarnEnabled()) {
083                                    _log.warn("Could not send AIM message");
084                            }
085                    }
086            }
087    
088            private static Log _log = LogFactoryUtil.getLog(AIMConnector.class);
089    
090            private static AIMConnector _instance = new AIMConnector();
091    
092            private AIMSession _aim;
093            private Oscar _oscar;
094    
095    }