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