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