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