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