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