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