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