001
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.PropsKeys;
020 import com.liferay.portal.util.PropsUtil;
021
022 import java.lang.reflect.Method;
023
024
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 Class<?> clazz = Class.forName(_SESSION);
050
051 _ym = clazz.newInstance();
052
053 _loginMethod = clazz.getMethod("login", String.class, String.class);
054 _logoutMethod = clazz.getMethod("logout");
055 _sendMessageMethod = clazz.getMethod(
056 "sendMessage", String.class, String.class);
057 }
058 catch (Exception e) {
059 _jYMSGLibraryFound = false;
060
061 if (_log.isWarnEnabled()) {
062 _log.warn(
063 "jYMSG library could not be loaded: " + e.getMessage());
064 }
065 }
066
067 try {
068 if (_jYMSGLibraryFound) {
069 String login = PropsUtil.get(PropsKeys.YM_LOGIN);
070 String password = PropsUtil.get(PropsKeys.YM_PASSWORD);
071
072 _loginMethod.invoke(_ym, login, password);
073 }
074 }
075 catch (Exception e) {
076 _log.error(e);
077 }
078 }
079
080 private void _disconnect() {
081 try {
082 if (_ym != null) {
083 _logoutMethod.invoke(_ym);
084 }
085 }
086 catch (Exception e) {
087 if (_log.isWarnEnabled()) {
088 _log.warn(e);
089 }
090 }
091 }
092
093 private void _send(String to, String msg) throws IllegalStateException {
094 try {
095 if (_ym == null) {
096 _connect();
097 }
098
099 if (_jYMSGLibraryFound) {
100 _sendMessageMethod.invoke(_ym, to, msg);
101 }
102 }
103 catch (Exception e) {
104 _log.error(e);
105 }
106 }
107
108 private static final String _SESSION = "ymsg.network.Session";
109
110 private static Log _log = LogFactoryUtil.getLog(YMConnector.class);
111
112 private static YMConnector _instance = new YMConnector();
113
114 private boolean _jYMSGLibraryFound = true;
115 private Method _loginMethod;
116 private Method _logoutMethod;
117 private Method _sendMessageMethod;
118 private Object _ym;
119
120 }