1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class MSNConnector {
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 MSNConnector() {
52          _login = PropsUtil.get(PropsKeys.MSN_LOGIN);
53          _password = PropsUtil.get(PropsKeys.MSN_PASSWORD);
54  
55          _msn = new MSNMessenger(_login, _password);
56          _msn.setInitialStatus(UserStatus.ONLINE);
57      }
58  
59      private void _connect() {
60          if (!_msn.isLoggedIn()) {
61              _msn.login();
62  
63              // Spend 5 seconds to attempt to login
64  
65              for (int i = 0; i < 50 && !_msn.isLoggedIn(); i++) {
66                  try {
67                      Thread.sleep(100);
68                  }
69                  catch (InterruptedException e) {
70                      _log.warn(e);
71  
72                      break;
73                  }
74              }
75  
76              if (!_msn.isLoggedIn()) {
77                  _log.error("Unable to connect as " + _login);
78              }
79          }
80      }
81  
82      private void _disconnect() {
83          try {
84              if (_msn.isLoggedIn()) {
85                  _msn.logout();
86              }
87          }
88          catch (Exception e) {
89              _log.warn(e);
90          }
91      }
92  
93      private void _send(String to, String msg) {
94          _connect();
95  
96          _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
97  
98          try {
99              Thread.sleep(1500);
100 
101             _msn.doCallWait(to);
102         }
103         catch (Exception e) {
104             _log.warn(e);
105         }
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(MSNConnector.class);
109 
110     private static MSNConnector _instance = new MSNConnector();
111 
112     private String _login;
113     private String _password;
114     private MSNMessenger _msn;
115 
116 }