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