1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }