1
22
23 package com.liferay.portal.im;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.MethodCache;
28 import com.liferay.portal.util.PropsKeys;
29 import com.liferay.portal.util.PropsUtil;
30
31 import java.lang.reflect.Method;
32
33
40 public class YMConnector {
41
42 public static void disconnect() {
43 if (_instance != null) {
44 _instance._disconnect();
45 }
46 }
47
48 public static void send(String to, String msg)
49 throws IllegalStateException {
50
51 _instance._send(to, msg);
52 }
53
54 private YMConnector() {
55 }
56
57 private void _connect() {
58 try {
59 _ym = Class.forName(_SESSION).newInstance();
60 }
61 catch (Exception e) {
62 _jYMSGLibraryFound = false;
63
64 if (_log.isWarnEnabled()) {
65 _log.warn(
66 "jYMSG library could not be loaded: " + e.getMessage());
67 }
68 }
69
70 try {
71 if (_jYMSGLibraryFound) {
72 String login = PropsUtil.get(PropsKeys.YM_LOGIN);
73 String password = PropsUtil.get(PropsKeys.YM_PASSWORD);
74
75 Method method = MethodCache.get(
76 _SESSION, "login",
77 new Class[] {String.class, String.class});
78
79 method.invoke(_ym, new Object[] {login, password});
80 }
81 }
82 catch (Exception e) {
83 _log.error(e);
84 }
85 }
86
87 private void _disconnect() {
88 try {
89 if (_ym != null) {
90 Method method = MethodCache.get(_SESSION, "logout");
91
92 method.invoke(_ym, new Object[] {});
93 }
94 }
95 catch (Exception e) {
96 if (_log.isWarnEnabled()) {
97 _log.warn(e);
98 }
99 }
100 }
101
102 private void _send(String to, String msg) throws IllegalStateException {
103 try {
104 if (_ym == null) {
105 _connect();
106 }
107
108 if (_jYMSGLibraryFound) {
109 Method method = MethodCache.get(
110 _SESSION, "sendMessage",
111 new Class[] {String.class, String.class});
112
113 method.invoke(_ym, new Object[] {to, msg});
114 }
115 }
116 catch (Exception e) {
117 _log.error(e);
118 }
119 }
120
121 private static final String _SESSION = "ymsg.network.Session";
122
123 private static Log _log = LogFactoryUtil.getLog(YMConnector.class);
124
125 private static YMConnector _instance = new YMConnector();
126
127 private boolean _jYMSGLibraryFound = true;
128 private Object _ym;
129
130 }