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