1   /**
2    * Copyright (c) 2000-2007 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.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  /**
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          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          //_icq.getPacketAnalyser().setDebug(true);
85  
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 }