001
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
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
069
070 _icq.addObserver(this);
071 }
072
073 private void _disconnect() {
074 try {
075 if (_icq != null) {
076 _icq.close();
077 }
078 }
079 catch (Exception e) {
080 _log.warn(e);
081 }
082 }
083
084 private synchronized void _send(String to, String msg) {
085 if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
086 _connect();
087
088 _messages.add(new KeyValuePair(to, msg));
089 }
090 else {
091 OscarInterface.sendMessage(_icq, to, msg);
092 }
093 }
094
095 private static Log _log = LogFactoryUtil.getLog(ICQConnector.class);
096
097 private static ICQConnector _instance = new ICQConnector();
098
099 private boolean _connecting;
100 private OscarConnection _icq;
101 private List<KeyValuePair> _messages;
102
103 }