1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class YMConnector {
41  
42      public static void disconnect() {
43          if (_instance != null) {
44              _instance._disconnect();
45          }
46      }
47  
48      public static void send(String to, String msg)
49          throws IllegalStateException {
50  
51          _instance._send(to, msg);
52      }
53  
54      private YMConnector() {
55      }
56  
57      private void _connect() {
58          try {
59              _ym = Class.forName(_SESSION).newInstance();
60          }
61          catch (Exception e) {
62              _jYMSGLibraryFound = false;
63  
64              if (_log.isWarnEnabled()) {
65                  _log.warn(
66                      "jYMSG library could not be loaded: " + e.getMessage());
67              }
68          }
69  
70          try {
71              if (_jYMSGLibraryFound) {
72                  String login = PropsUtil.get(PropsKeys.YM_LOGIN);
73                  String password = PropsUtil.get(PropsKeys.YM_PASSWORD);
74  
75                  Method method = MethodCache.get(
76                      _SESSION, "login",
77                      new Class[] {String.class, String.class});
78  
79                  method.invoke(_ym, new Object[] {login, password});
80              }
81          }
82          catch (Exception e) {
83              _log.error(e);
84          }
85      }
86  
87      private void _disconnect() {
88          try {
89              if (_ym != null) {
90                  Method method = MethodCache.get(_SESSION, "logout");
91  
92                  method.invoke(_ym, new Object[] {});
93              }
94          }
95          catch (Exception e) {
96              if (_log.isWarnEnabled()) {
97                  _log.warn(e);
98              }
99          }
100     }
101 
102     private void _send(String to, String msg) throws IllegalStateException {
103         try {
104             if (_ym == null) {
105                 _connect();
106             }
107 
108             if (_jYMSGLibraryFound) {
109                 Method method = MethodCache.get(
110                     _SESSION, "sendMessage",
111                     new Class[] {String.class, String.class});
112 
113                 method.invoke(_ym, new Object[] {to, msg});
114             }
115         }
116         catch (Exception e) {
117             _log.error(e);
118         }
119     }
120 
121     private static final String _SESSION = "ymsg.network.Session";
122 
123     private static Log _log = LogFactoryUtil.getLog(YMConnector.class);
124 
125     private static YMConnector _instance = new YMConnector();
126 
127     private boolean _jYMSGLibraryFound = true;
128     private Object _ym;
129 
130 }