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 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            public void update(Observable obs, Object obj) {
049                    _connecting = false;
050    
051                    for (KeyValuePair kvp : _messages) {
052                            OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
053                    }
054            }
055    
056            private ICQConnector() {
057                    _messages = new Vector<KeyValuePair>();
058            }
059    
060            private void _connect() {
061                    _connecting = true;
062    
063                    String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
064                    String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
065    
066                    _icq = new OscarConnection("login.icq.com", 5190, login, password);
067    
068                    //_icq.getPacketAnalyser().setDebug(true);
069    
070                    _icq.addObserver(this);
071            }
072    
073            private void _disconnect() {
074                    try {
075                            _icq.close();
076                    }
077                    catch (Exception e) {
078                            _log.warn(e);
079                    }
080            }
081    
082            private synchronized void _send(String to, String msg) {
083                    if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
084                            _connect();
085    
086                            _messages.add(new KeyValuePair(to, msg));
087                    }
088                    else {
089                            OscarInterface.sendMessage(_icq, to, msg);
090                    }
091            }
092    
093            private static Log _log = LogFactoryUtil.getLog(ICQConnector.class);
094    
095            private static ICQConnector _instance = new ICQConnector();
096    
097            private OscarConnection _icq;
098            private List<KeyValuePair> _messages;
099            private boolean _connecting;
100    
101    }