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