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.kernel.util.MethodCache;
024: import com.liferay.portal.util.PropsUtil;
025:
026: import java.io.IOException;
027:
028: import java.lang.reflect.Method;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * <a href="YMConnector.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: * @author Brett Randall
038: * @author Bruno Farache
039: *
040: */
041: public class YMConnector {
042:
043: public static void disconnect() {
044: if (_instance != null) {
045: _instance._disconnect();
046: }
047: }
048:
049: public static void send(String to, String msg)
050: throws IllegalStateException, IOException {
051:
052: _instance._send(to, msg);
053: }
054:
055: private YMConnector() {
056: }
057:
058: private void _connect() {
059: try {
060: _ym = (Object) Class.forName(_SESSION).newInstance();
061: } catch (Exception e) {
062: _jYMSGLibraryFound = false;
063:
064: if (_log.isWarnEnabled()) {
065: _log.warn("jYMSG library could not be loaded: "
066: + e.getMessage());
067: }
068: }
069:
070: try {
071: if (_jYMSGLibraryFound) {
072: String login = PropsUtil.get(PropsUtil.YM_LOGIN);
073: String password = PropsUtil.get(PropsUtil.YM_PASSWORD);
074:
075: Method method = MethodCache.get(_SESSION, "login",
076: new Class[] { String.class, String.class });
077:
078: method.invoke(_ym, new Object[] { login, password });
079: }
080: } catch (Exception e) {
081: _log.error(e);
082: }
083: }
084:
085: private void _disconnect() {
086: try {
087: if (_ym != null) {
088: Method method = MethodCache.get(_SESSION, "logout");
089:
090: method.invoke(_ym, new Object[] {});
091: }
092: } catch (Exception e) {
093: if (_log.isWarnEnabled()) {
094: _log.warn(e);
095: }
096: }
097: }
098:
099: private void _send(String to, String msg)
100: throws IllegalStateException, IOException {
101:
102: try {
103: if (_ym == null) {
104: _connect();
105: }
106:
107: if (_jYMSGLibraryFound) {
108: Method method = MethodCache.get(_SESSION,
109: "sendMessage", new Class[] { String.class,
110: String.class });
111:
112: method.invoke(_ym, new Object[] { to, msg });
113: }
114: } catch (Exception e) {
115: _log.error(e);
116: }
117: }
118:
119: private static final String _SESSION = "ymsg.network.Session";
120:
121: private static Log _log = LogFactory.getLog(YMConnector.class);
122:
123: private static YMConnector _instance = new YMConnector();
124:
125: private boolean _jYMSGLibraryFound = true;
126: private Object _ym;
127:
128: }
|