1   /**
2    * Copyright (c) 2000-2009 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.comm;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PropsKeys;
31  import com.liferay.portal.util.PropsUtil;
32  
33  import java.io.Serializable;
34  
35  import java.util.LinkedList;
36  import java.util.List;
37  
38  import org.jgroups.Channel;
39  import org.jgroups.JChannel;
40  import org.jgroups.Message;
41  import org.jgroups.MessageListener;
42  import org.jgroups.blocks.PullPushAdapter;
43  import org.jgroups.util.Util;
44  
45  /**
46   * <a href="CommLink.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class CommLink implements MessageListener {
52  
53      public static CommLink getInstance() {
54          return _instance;
55      }
56  
57      public byte[] getState() {
58          try {
59              return Util.objectToByteBuffer(_history);
60          }
61          catch (Exception e) {
62              return null;
63          }
64      }
65  
66      public void setState(byte[] state) {
67          try {
68              _history = (List<Object>)Util.objectFromByteBuffer(state);
69          }
70          catch (Exception e) {
71              _log.error("Error setting state", e);
72          }
73      }
74  
75      public void receive(Message msg) {
76          try {
77              Object obj = msg.getObject();
78  
79              if (_log.isDebugEnabled()) {
80                  _log.debug("Receiving message " + obj);
81              }
82  
83              if (obj instanceof MethodWrapper) {
84                  MethodWrapper methodWrapper = (MethodWrapper)obj;
85  
86                  MethodInvoker.invoke(methodWrapper);
87              }
88  
89              _history.add(obj);
90          }
91          catch (Exception e) {
92              _log.error("Error receiving message", e);
93          }
94      }
95  
96      public void send(Serializable obj) {
97          try {
98              if (_channel == null) {
99                  return;
100             }
101 
102             Message msg = new Message(null, null, obj);
103 
104             _channel.send(msg);
105         }
106         catch (Exception e) {
107             _log.error("Error sending message", e);
108         }
109     }
110 
111     private CommLink() {
112         try {
113             String properties = PropsUtil.get(PropsKeys.COMM_LINK_PROPERTIES);
114 
115             if (Validator.isNotNull(properties)) {
116                 _channel = new JChannel(properties);
117 
118                 _channel.connect("PortalMessageListener");
119 
120                 new PullPushAdapter(_channel, this);
121             }
122         }
123         catch (Exception e) {
124             _log.error("Error initializing", e);
125         }
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(CommLink.class);
129 
130     private static CommLink _instance = new CommLink();
131 
132     private Channel _channel;
133     private List<Object> _history = new LinkedList<Object>();
134 
135 }