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.PropsUtil;
31
32 import java.util.Iterator;
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 Iterator itr = _messages.iterator();
64
65 while (itr.hasNext()) {
66 KeyValuePair kvp = (KeyValuePair)itr.next();
67
68 OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
69 }
70 }
71
72 private ICQConnector() {
73 _messages = new Vector();
74 }
75
76 private void _connect() {
77 _connecting = true;
78
79 String login = PropsUtil.get(PropsUtil.ICQ_LOGIN);
80 String password = PropsUtil.get(PropsUtil.ICQ_PASSWORD);
81
82 _icq = new OscarConnection("login.icq.com", 5190, login, password);
83
84
86 _icq.addObserver(this);
87 }
88
89 private void _disconnect() {
90 try {
91 _icq.close();
92 }
93 catch (Exception e) {
94 _log.warn(e);
95 }
96 }
97
98 private synchronized void _send(String to, String msg) {
99 if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
100 _connect();
101
102 _messages.add(new KeyValuePair(to, msg));
103 }
104 else {
105 OscarInterface.sendMessage(_icq, to, msg);
106 }
107 }
108
109 private static Log _log = LogFactory.getLog(ICQConnector.class);
110
111 private static ICQConnector _instance = new ICQConnector();
112
113 private OscarConnection _icq;
114 private List _messages;
115 private boolean _connecting;
116
117 }