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 JOscarLib.Core.OscarConnection;
018    
019    import JOscarLib.Tool.OscarInterface;
020    
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.KeyValuePair;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.util.PropsUtil;
026    
027    import java.util.List;
028    import java.util.Observable;
029    import java.util.Observer;
030    import java.util.Vector;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Brett Randall
035     */
036    public class ICQConnector implements Observer {
037    
038            public static void disconnect() {
039                    if (_instance != null) {
040                            _instance._disconnect();
041                    }
042            }
043    
044            public static void send(String to, String msg) {
045                    _instance._send(to, msg);
046            }
047    
048            @Override
049            public void update(Observable obs, Object obj) {
050                    _connecting = false;
051    
052                    for (KeyValuePair kvp : _messages) {
053                            OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
054                    }
055            }
056    
057            private ICQConnector() {
058                    _messages = new Vector<KeyValuePair>();
059            }
060    
061            private void _connect() {
062                    _connecting = true;
063    
064                    String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
065                    String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
066    
067                    _icq = new OscarConnection("login.icq.com", 5190, login, password);
068    
069                    //_icq.getPacketAnalyser().setDebug(true);
070    
071                    _icq.addObserver(this);
072            }
073    
074            private void _disconnect() {
075                    try {
076                            if (_icq != null) {
077                                    _icq.close();
078                            }
079                    }
080                    catch (Exception e) {
081                            _log.warn(e);
082                    }
083            }
084    
085            private synchronized void _send(String to, String msg) {
086                    if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
087                            _connect();
088    
089                            _messages.add(new KeyValuePair(to, msg));
090                    }
091                    else {
092                            OscarInterface.sendMessage(_icq, to, msg);
093                    }
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(ICQConnector.class);
097    
098            private static ICQConnector _instance = new ICQConnector();
099    
100            private boolean _connecting;
101            private OscarConnection _icq;
102            private List<KeyValuePair> _messages;
103    
104    }