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