001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.im;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.MethodCache;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.util.PropsUtil;
022    
023    import java.lang.reflect.Method;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Brett Randall
028     * @author Bruno Farache
029     */
030    public class YMConnector {
031    
032            public static void disconnect() {
033                    if (_instance != null) {
034                            _instance._disconnect();
035                    }
036            }
037    
038            public static void send(String to, String msg)
039                    throws IllegalStateException {
040    
041                    _instance._send(to, msg);
042            }
043    
044            private YMConnector() {
045            }
046    
047            private void _connect() {
048                    try {
049                            _ym = Class.forName(_SESSION).newInstance();
050                    }
051                    catch (Exception e) {
052                            _jYMSGLibraryFound = false;
053    
054                            if (_log.isWarnEnabled()) {
055                                    _log.warn(
056                                            "jYMSG library could not be loaded: " + e.getMessage());
057                            }
058                    }
059    
060                    try {
061                            if (_jYMSGLibraryFound) {
062                                    String login = PropsUtil.get(PropsKeys.YM_LOGIN);
063                                    String password = PropsUtil.get(PropsKeys.YM_PASSWORD);
064    
065                                    Method method = MethodCache.get(
066                                            _SESSION, "login",
067                                            new Class[] {String.class, String.class});
068    
069                                    method.invoke(_ym, new Object[] {login, password});
070                            }
071                    }
072                    catch (Exception e) {
073                            _log.error(e);
074                    }
075            }
076    
077            private void _disconnect() {
078                    try {
079                            if (_ym != null) {
080                                    Method method = MethodCache.get(_SESSION, "logout");
081    
082                                    method.invoke(_ym, new Object[] {});
083                            }
084                    }
085                    catch (Exception e) {
086                            if (_log.isWarnEnabled()) {
087                                    _log.warn(e);
088                            }
089                    }
090            }
091    
092            private void _send(String to, String msg) throws IllegalStateException {
093                    try {
094                            if (_ym == null) {
095                                    _connect();
096                            }
097    
098                            if (_jYMSGLibraryFound) {
099                                    Method method = MethodCache.get(
100                                            _SESSION, "sendMessage",
101                                            new Class[] {String.class, String.class});
102    
103                                    method.invoke(_ym, new Object[] {to, msg});
104                            }
105                    }
106                    catch (Exception e) {
107                            _log.error(e);
108                    }
109            }
110    
111            private static final String _SESSION = "ymsg.network.Session";
112    
113            private static Log _log = LogFactoryUtil.getLog(YMConnector.class);
114    
115            private static YMConnector _instance = new YMConnector();
116    
117            private boolean _jYMSGLibraryFound = true;
118            private Object _ym;
119    
120    }