001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.im;
022:
023: import com.liferay.portal.util.PropsUtil;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import rath.msnm.MSNMessenger;
029: import rath.msnm.UserStatus;
030:
031: /**
032: * <a href="MSNConnector.java.html"><b><i>View Source</i></b></a>
033: *
034: * @author Brian Wing Shun Chan
035: * @author Brett Randall
036: *
037: */
038: public class MSNConnector {
039:
040: public static void disconnect() {
041: if (_instance != null) {
042: _instance._disconnect();
043: }
044: }
045:
046: public static void send(String to, String msg) {
047: _instance._send(to, msg);
048: }
049:
050: private MSNConnector() {
051: _login = PropsUtil.get(PropsUtil.MSN_LOGIN);
052: _password = PropsUtil.get(PropsUtil.MSN_PASSWORD);
053:
054: _msn = new MSNMessenger(_login, _password);
055: _msn.setInitialStatus(UserStatus.ONLINE);
056: }
057:
058: private void _connect() {
059: if (!_msn.isLoggedIn()) {
060: _msn.login();
061:
062: // Spend 5 seconds to attempt to login
063:
064: for (int i = 0; i < 50 && !_msn.isLoggedIn(); i++) {
065: try {
066: Thread.sleep(100);
067: } catch (InterruptedException e) {
068: _log.warn(e);
069:
070: break;
071: }
072: }
073:
074: if (!_msn.isLoggedIn()) {
075: _log.error("Unable to connect as " + _login);
076: }
077: }
078: }
079:
080: private void _disconnect() {
081: try {
082: if (_msn.isLoggedIn()) {
083: _msn.logout();
084: }
085: } catch (Exception e) {
086: _log.warn(e);
087: }
088: }
089:
090: private void _send(String to, String msg) {
091: _connect();
092:
093: _msn.addMsnListener(new MSNMessageAdapter(_msn, to, msg));
094:
095: try {
096: Thread.sleep(1500);
097:
098: _msn.doCallWait(to);
099: } catch (Exception e) {
100: _log.warn(e);
101: }
102: }
103:
104: private static Log _log = LogFactory.getLog(MSNConnector.class);
105:
106: private static MSNConnector _instance = new MSNConnector();
107:
108: private String _login;
109: private String _password;
110: private MSNMessenger _msn;
111:
112: }
|