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.util.PropsKeys;
28  import com.liferay.portal.util.PropsUtil;
29  
30  import org.walluck.oscar.AIMConnection;
31  import org.walluck.oscar.AIMSession;
32  import org.walluck.oscar.client.Oscar;
33  
34  /**
35   * <a href="AIMConnector.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  public class AIMConnector {
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          _instance._send(to, msg);
51      }
52  
53      private AIMConnector() {
54      }
55  
56      private void _connect() {
57          String login = PropsUtil.get(PropsKeys.AIM_LOGIN);
58          String password = PropsUtil.get(PropsKeys.AIM_PASSWORD);
59  
60          AIMSession ses = new AIMSession();
61  
62          ses.setSN(login);
63  
64          Oscar oscar = new Oscar();
65  
66          oscar.setSN(login);
67          oscar.setPassword(password);
68  
69          ses.init();
70      }
71  
72      private void _disconnect() {
73          if (_aim != null) {
74              AIMConnection.killAllInSess(_aim);
75          }
76      }
77  
78      private void _send(String to, String msg) {
79          try {
80              if (_aim == null) {
81                  _connect();
82  
83                  // Daim's listeners are buggy. Instead, just wait a second
84                  // before sending the first message.
85  
86                  Thread.sleep(1000);
87              }
88  
89              _oscar.sendIM(_aim, to, msg, Oscar.getICQCaps());
90          }
91          catch (Exception e) {
92              if (_log.isWarnEnabled()) {
93                  _log.warn("Could not send AIM message");
94              }
95          }
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(AIMConnector.class);
99  
100     private static AIMConnector _instance = new AIMConnector();
101 
102     private AIMSession _aim;
103     private Oscar _oscar;
104 
105 }