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