1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
34   * <a href="YMConnector.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Brett Randall
38   * @author Bruno Farache
39   *
40   */
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 }