1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.im;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.MethodCache;
25  import com.liferay.portal.util.PropsKeys;
26  import com.liferay.portal.util.PropsUtil;
27  
28  import java.lang.reflect.Method;
29  
30  /**
31   * <a href="YMConnector.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Brett Randall
35   * @author Bruno Farache
36   *
37   */
38  public class YMConnector {
39  
40      public static void disconnect() {
41          if (_instance != null) {
42              _instance._disconnect();
43          }
44      }
45  
46      public static void send(String to, String msg)
47          throws IllegalStateException {
48  
49          _instance._send(to, msg);
50      }
51  
52      private YMConnector() {
53      }
54  
55      private void _connect() {
56          try {
57              _ym = Class.forName(_SESSION).newInstance();
58          }
59          catch (Exception e) {
60              _jYMSGLibraryFound = false;
61  
62              if (_log.isWarnEnabled()) {
63                  _log.warn(
64                      "jYMSG library could not be loaded: " + e.getMessage());
65              }
66          }
67  
68          try {
69              if (_jYMSGLibraryFound) {
70                  String login = PropsUtil.get(PropsKeys.YM_LOGIN);
71                  String password = PropsUtil.get(PropsKeys.YM_PASSWORD);
72  
73                  Method method = MethodCache.get(
74                      _SESSION, "login",
75                      new Class[] {String.class, String.class});
76  
77                  method.invoke(_ym, new Object[] {login, password});
78              }
79          }
80          catch (Exception e) {
81              _log.error(e);
82          }
83      }
84  
85      private void _disconnect() {
86          try {
87              if (_ym != null) {
88                  Method method = MethodCache.get(_SESSION, "logout");
89  
90                  method.invoke(_ym, new Object[] {});
91              }
92          }
93          catch (Exception e) {
94              if (_log.isWarnEnabled()) {
95                  _log.warn(e);
96              }
97          }
98      }
99  
100     private void _send(String to, String msg) throws IllegalStateException {
101         try {
102             if (_ym == null) {
103                 _connect();
104             }
105 
106             if (_jYMSGLibraryFound) {
107                 Method method = MethodCache.get(
108                     _SESSION, "sendMessage",
109                     new Class[] {String.class, String.class});
110 
111                 method.invoke(_ym, new Object[] {to, msg});
112             }
113         }
114         catch (Exception e) {
115             _log.error(e);
116         }
117     }
118 
119     private static final String _SESSION = "ymsg.network.Session";
120 
121     private static Log _log = LogFactoryUtil.getLog(YMConnector.class);
122 
123     private static YMConnector _instance = new YMConnector();
124 
125     private boolean _jYMSGLibraryFound = true;
126     private Object _ym;
127 
128 }