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 JOscarLib.Core.OscarConnection;
024:
025: import JOscarLib.Tool.OscarInterface;
026:
027: import com.liferay.portal.kernel.util.KeyValuePair;
028: import com.liferay.portal.util.PropsUtil;
029:
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Observable;
033: import java.util.Observer;
034: import java.util.Vector;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <a href="ICQConnector.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: * @author Brett Randall
044: *
045: */
046: public class ICQConnector implements Observer {
047:
048: public static void disconnect() {
049: if (_instance != null) {
050: _instance._disconnect();
051: }
052: }
053:
054: public static void send(String to, String msg) {
055: _instance._send(to, msg);
056: }
057:
058: public void update(Observable obs, Object obj) {
059: _connecting = false;
060:
061: Iterator itr = _messages.iterator();
062:
063: while (itr.hasNext()) {
064: KeyValuePair kvp = (KeyValuePair) itr.next();
065:
066: OscarInterface.sendMessage(_icq, kvp.getKey(), kvp
067: .getValue());
068: }
069: }
070:
071: private ICQConnector() {
072: _messages = new Vector();
073: }
074:
075: private void _connect() {
076: _connecting = true;
077:
078: String login = PropsUtil.get(PropsUtil.ICQ_LOGIN);
079: String password = PropsUtil.get(PropsUtil.ICQ_PASSWORD);
080:
081: _icq = new OscarConnection("login.icq.com", 5190, login,
082: password);
083:
084: //_icq.getPacketAnalyser().setDebug(true);
085:
086: _icq.addObserver(this );
087: }
088:
089: private void _disconnect() {
090: try {
091: _icq.close();
092: } catch (Exception e) {
093: _log.warn(e);
094: }
095: }
096:
097: private synchronized void _send(String to, String msg) {
098: if (((_icq == null) || !_icq.isLogged()) && !_connecting) {
099: _connect();
100:
101: _messages.add(new KeyValuePair(to, msg));
102: } else {
103: OscarInterface.sendMessage(_icq, to, msg);
104: }
105: }
106:
107: private static Log _log = LogFactory.getLog(ICQConnector.class);
108:
109: private static ICQConnector _instance = new ICQConnector();
110:
111: private OscarConnection _icq;
112: private List _messages;
113: private boolean _connecting;
114:
115: }
|