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