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