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 org.walluck.oscar.AIMConnection;
029: import org.walluck.oscar.AIMSession;
030: import org.walluck.oscar.client.Oscar;
031:
032: /**
033: * <a href="AIMConnector.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: * @author Brett Randall
037: * @author Bruno Farache
038: *
039: */
040: public class AIMConnector {
041:
042: public static void disconnect() {
043: if (_instance != null) {
044: _instance._disconnect();
045: }
046: }
047:
048: public static void send(String to, String msg) {
049: _instance._send(to, msg);
050: }
051:
052: private AIMConnector() {
053: }
054:
055: private void _connect() {
056: String login = PropsUtil.get(PropsUtil.AIM_LOGIN);
057: String password = PropsUtil.get(PropsUtil.AIM_PASSWORD);
058:
059: AIMSession ses = new AIMSession();
060:
061: ses.setSN(login);
062:
063: Oscar oscar = new Oscar();
064:
065: oscar.setSN(login);
066: oscar.setPassword(password);
067:
068: ses.init();
069: }
070:
071: private void _disconnect() {
072: if (_aim != null) {
073: AIMConnection.killAllInSess(_aim);
074: }
075: }
076:
077: private void _send(String to, String msg) {
078: try {
079: if (_aim == null) {
080: _connect();
081:
082: // Daim's listeners are buggy. Instead, just wait a second
083: // before sending the first message.
084:
085: Thread.sleep(1000);
086: }
087:
088: _oscar.sendIM(_aim, to, msg, Oscar.getICQCaps());
089: } catch (Exception e) {
090: if (_log.isWarnEnabled()) {
091: _log.warn("Could not send AIM message");
092: }
093: }
094: }
095:
096: private static AIMConnector _instance = new AIMConnector();
097:
098: private static Log _log = LogFactory.getLog(AIMConnector.class);
099:
100: private AIMSession _aim;
101: private Oscar _oscar;
102:
103: }
|