1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
33   * <a href="ICQConnector.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Brett Randall
37   */
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          //_icq.getPacketAnalyser().setDebug(true);
71  
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 }