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