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 com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.util.PropsKeys;
28  import com.liferay.portal.util.PropsUtil;
29  
30  import org.walluck.oscar.AIMConnection;
31  import org.walluck.oscar.AIMSession;
32  import org.walluck.oscar.client.Oscar;
33  
34  /**
35   * <a href="AIMConnector.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Brett Randall
39   * @author Bruno Farache
40   *
41   */
42  public class AIMConnector {
43  
44      public static void disconnect() {
45          if (_instance != null) {
46              _instance._disconnect();
47          }
48      }
49  
50      public static void send(String to, String msg) {
51          _instance._send(to, msg);
52      }
53  
54      private AIMConnector() {
55      }
56  
57      private void _connect() {
58          String login = PropsUtil.get(PropsKeys.AIM_LOGIN);
59          String password = PropsUtil.get(PropsKeys.AIM_PASSWORD);
60  
61          AIMSession ses = new AIMSession();
62  
63          ses.setSN(login);
64  
65          Oscar oscar = new Oscar();
66  
67          oscar.setSN(login);
68          oscar.setPassword(password);
69  
70          ses.init();
71      }
72  
73      private void _disconnect() {
74          if (_aim != null) {
75              AIMConnection.killAllInSess(_aim);
76          }
77      }
78  
79      private void _send(String to, String msg) {
80          try {
81              if (_aim == null) {
82                  _connect();
83  
84                  // Daim's listeners are buggy. Instead, just wait a second
85                  // before sending the first message.
86  
87                  Thread.sleep(1000);
88              }
89  
90              _oscar.sendIM(_aim, to, msg, Oscar.getICQCaps());
91          }
92          catch (Exception e) {
93              if (_log.isWarnEnabled()) {
94                  _log.warn("Could not send AIM message");
95              }
96          }
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(AIMConnector.class);
100 
101     private static AIMConnector _instance = new AIMConnector();
102 
103     private AIMSession _aim;
104     private Oscar _oscar;
105 
106 }