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