1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.im;
21  
22  import JOscarLib.Core.OscarConnection;
23  
24  import JOscarLib.Tool.OscarInterface;
25  
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.KeyValuePair;
29  import com.liferay.portal.util.PropsKeys;
30  import com.liferay.portal.util.PropsUtil;
31  
32  import java.util.List;
33  import java.util.Observable;
34  import java.util.Observer;
35  import java.util.Vector;
36  
37  /**
38   * <a href="ICQConnector.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Brett Randall
42   *
43   */
44  public class ICQConnector implements Observer {
45  
46      public static void disconnect() {
47          if (_instance != null) {
48              _instance._disconnect();
49          }
50      }
51  
52      public static void send(String to, String msg) {
53          _instance._send(to, msg);
54      }
55  
56      public void update(Observable obs, Object obj) {
57          _connecting = false;
58  
59          for (KeyValuePair kvp : _messages) {
60              OscarInterface.sendMessage(_icq, kvp.getKey(), kvp.getValue());
61          }
62      }
63  
64      private ICQConnector() {
65          _messages = new Vector<KeyValuePair>();
66      }
67  
68      private void _connect() {
69          _connecting = true;
70  
71          String login = PropsUtil.get(PropsKeys.ICQ_LOGIN);
72          String password = PropsUtil.get(PropsKeys.ICQ_PASSWORD);
73  
74          _icq = new OscarConnection("login.icq.com", 5190, login, password);
75  
76          //_icq.getPacketAnalyser().setDebug(true);
77  
78          _icq.addObserver(this);
79      }
80  
81      private void _disconnect() {
82          try {
83              _icq.close();
84          }
85          catch (Exception e) {
86              _log.warn(e);
87          }
88      }
89  
90      private synchronized void _send(String to, String msg) {
91          if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
92              _connect();
93  
94              _messages.add(new KeyValuePair(to, msg));
95          }
96          else {
97              OscarInterface.sendMessage(_icq, to, msg);
98          }
99      }
100 
101     private static Log _log = LogFactoryUtil.getLog(ICQConnector.class);
102 
103     private static ICQConnector _instance = new ICQConnector();
104 
105     private OscarConnection _icq;
106     private List<KeyValuePair> _messages;
107     private boolean _connecting;
108 
109 }