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