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 rath.msnm.MSNMessenger;
23  import rath.msnm.UserStatus;
24  
25  /**
26   * <a href="MSNConnector.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Brett Randall
30   */
31  public class MSNConnector {
32  
33      public static void disconnect() {
34          if (_instance != null) {
35              _instance._disconnect();
36          }
37      }
38  
39      public static void send(String to, String msg) {
40          _instance._send(to, msg);
41      }
42  
43      private MSNConnector() {
44          _login = PropsUtil.get(PropsKeys.MSN_LOGIN);
45          _password = PropsUtil.get(PropsKeys.MSN_PASSWORD);
46  
47          _msn = new MSNMessenger(_login, _password);
48          _msn.setInitialStatus(UserStatus.ONLINE);
49      }
50  
51      private void _connect() {
52          if (!_msn.isLoggedIn()) {
53              _msn.login();
54  
55              // Spend 5 seconds to attempt to login
56  
57              for (int i = 0; i < 50 && !_msn.isLoggedIn(); i++) {
58                  try {
59                      Thread.sleep(100);
60                  }
61                  catch (InterruptedException e) {
62                      _log.warn(e);
63  
64                      break;
65                  }
66              }
67  
68              if (!_msn.isLoggedIn()) {
69                  _log.error("Unable to connect as " + _login);
70              }
71          }
72      }
73  
74      private void _disconnect() {
75          try {
76              if (_msn.isLoggedIn()) {
77                  _msn.logout();
78              }
79          }
80          catch (Exception e) {
81              _log.warn(e);
82          }
83      }
84  
85      private void _send(String to, String msg) {
86          _connect();
87  
88          _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
89  
90          try {
91              Thread.sleep(1500);
92  
93              _msn.doCallWait(to);
94          }
95          catch (Exception e) {
96              _log.warn(e);
97          }
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(MSNConnector.class);
101 
102     private static MSNConnector _instance = new MSNConnector();
103 
104     private String _login;
105     private String _password;
106     private MSNMessenger _msn;
107 
108 }