1   /**
2    * Copyright (c) 2000-2008 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.util.KeyValuePair;
30  import com.liferay.portal.util.PropsKeys;
31  import com.liferay.portal.util.PropsUtil;
32  
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  /**
42   * <a href="ICQConnector.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Brett Randall
46   *
47   */
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          for (KeyValuePair kvp : _messages) {
64              OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
65          }
66      }
67  
68      private ICQConnector() {
69          _messages = new Vector<KeyValuePair>();
70      }
71  
72      private void _connect() {
73          _connecting = true;
74  
75          String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
76          String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
77  
78          _icq = new OscarConnection("login.icq.com", 5190, login, password);
79  
80          //_icq.getPacketAnalyser().setDebug(true);
81  
82          _icq.addObserver(this);
83      }
84  
85      private void _disconnect() {
86          try {
87              _icq.close();
88          }
89          catch (Exception e) {
90              _log.warn(e);
91          }
92      }
93  
94      private synchronized void _send(String to, String msg) {
95          if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
96              _connect();
97  
98              _messages.add(new KeyValuePair(to, msg));
99          }
100         else {
101             OscarInterface.sendMessage(_icq, to, msg);
102         }
103     }
104 
105     private static Log _log = LogFactory.getLog(ICQConnector.class);
106 
107     private static ICQConnector _instance = new ICQConnector();
108 
109     private OscarConnection _icq;
110     private List<KeyValuePair> _messages;
111     private boolean _connecting;
112 
113 }