1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ICQConnector implements Observer {
47  
48      public static void disconnect() {
49          if (_instance != null) {
50              _instance._disconnect();
51          }
52      }
53  
54      public static void send(String to, String msg) {
55          _instance._send(to, msg);
56      }
57  
58      public void update(Observable obs, Object obj) {
59          _connecting = false;
60  
61          for (KeyValuePair kvp : _messages) {
62              OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
63          }
64      }
65  
66      private ICQConnector() {
67          _messages = new Vector<KeyValuePair>();
68      }
69  
70      private void _connect() {
71          _connecting = true;
72  
73          String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
74          String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
75  
76          _icq = new OscarConnection("login.icq.com", 5190, login, password);
77  
78          //_icq.getPacketAnalyser().setDebug(true);
79  
80          _icq.addObserver(this);
81      }
82  
83      private void _disconnect() {
84          try {
85              _icq.close();
86          }
87          catch (Exception e) {
88              _log.warn(e);
89          }
90      }
91  
92      private synchronized void _send(String to, String msg) {
93          if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
94              _connect();
95  
96              _messages.add(new KeyValuePair(to, msg));
97          }
98          else {
99              OscarInterface.sendMessage(_icq, to, msg);
100         }
101     }
102 
103     private static Log _log = LogFactoryUtil.getLog(ICQConnector.class);
104 
105     private static ICQConnector _instance = new ICQConnector();
106 
107     private OscarConnection _icq;
108     private List<KeyValuePair> _messages;
109     private boolean _connecting;
110 
111 }