1   /**
2    * Copyright (c) 2000-2008 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.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  /**
35   * <a href="YMConnector.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Brett Randall
39   * @author Bruno Farache
40   *
41   */
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 }