1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
41   * <a href="ICQConnector.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Brett Randall
45   *
46   */
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          //_icq.getPacketAnalyser().setDebug(true);
80  
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 }