1
22
23 package com.liferay.portal.im;
24
25 import JOscarLib.Core.OscarConnection;
26
27 import JOscarLib.Tool.OscarInterface;
28
29 import com.liferay.portal.kernel.util.KeyValuePair;
30 import com.liferay.portal.util.PropsKeys;
31 import com.liferay.portal.util.PropsUtil;
32
33 import java.util.List;
34 import java.util.Observable;
35 import java.util.Observer;
36 import java.util.Vector;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41
48 public class ICQConnector implements Observer {
49
50 public static void disconnect() {
51 if (_instance != null) {
52 _instance._disconnect();
53 }
54 }
55
56 public static void send(String to, String msg) {
57 _instance._send(to, msg);
58 }
59
60 public void update(Observable obs, Object obj) {
61 _connecting = false;
62
63 for (KeyValuePair kvp : _messages) {
64 OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
65 }
66 }
67
68 private ICQConnector() {
69 _messages = new Vector<KeyValuePair>();
70 }
71
72 private void _connect() {
73 _connecting = true;
74
75 String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
76 String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
77
78 _icq = new OscarConnection("login.icq.com", 5190, login, password);
79
80
82 _icq.addObserver(this);
83 }
84
85 private void _disconnect() {
86 try {
87 _icq.close();
88 }
89 catch (Exception e) {
90 _log.warn(e);
91 }
92 }
93
94 private synchronized void _send(String to, String msg) {
95 if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
96 _connect();
97
98 _messages.add(new KeyValuePair(to, msg));
99 }
100 else {
101 OscarInterface.sendMessage(_icq, to, msg);
102 }
103 }
104
105 private static Log _log = LogFactory.getLog(ICQConnector.class);
106
107 private static ICQConnector _instance = new ICQConnector();
108
109 private OscarConnection _icq;
110 private List<KeyValuePair> _messages;
111 private boolean _connecting;
112
113 }