1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cluster;
16  
17  import com.liferay.portal.kernel.cluster.ClusterException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.MethodInvoker;
21  import com.liferay.portal.kernel.util.MethodWrapper;
22  
23  import java.io.Serializable;
24  
25  import org.jgroups.Address;
26  import org.jgroups.ChannelException;
27  import org.jgroups.JChannel;
28  import org.jgroups.Message;
29  import org.jgroups.ReceiverAdapter;
30  import org.jgroups.View;
31  
32  /**
33   * <a href="ClusterInvokeReceiver.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Shuyang Zhou
36   */
37  public class ClusterInvokeReceiver extends ReceiverAdapter {
38  
39      public ClusterInvokeReceiver(JChannel channel) {
40          _channel = channel;
41      }
42  
43      public void receive(Message message) {
44          Address sourceAddress = message.getSrc();
45          Address localAddress = _channel.getLocalAddress();
46  
47          if ((!localAddress.equals(sourceAddress)) ||
48              (message.getDest() != null)) {
49  
50              Message responseMessage = new Message();
51  
52              responseMessage.setDest(sourceAddress);
53              responseMessage.setSrc(localAddress);
54  
55              Object payload = message.getObject();
56  
57              if (payload instanceof MethodWrapper) {
58                  MethodWrapper methodWrapper = (MethodWrapper)payload;
59  
60                  try {
61                      Object returnValue = MethodInvoker.invoke(methodWrapper);
62  
63                      if (returnValue instanceof Serializable) {
64                          responseMessage.setObject((Serializable)returnValue);
65                      }
66                      else {
67                          responseMessage.setObject(
68                              new ClusterException(
69                                  "Return value is not Serializable"));
70                      }
71                  }
72                  catch (Exception e) {
73                      responseMessage.setObject(e);
74                  }
75              }
76              else {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("Payload is not a MethodWrapper");
79                  }
80              }
81  
82              try {
83                  _channel.send(responseMessage);
84              }
85              catch (ChannelException ce) {
86                  _log.error(
87                      "Unable to send response message " + responseMessage, ce);
88              }
89          }
90          else {
91              if (_log.isDebugEnabled()) {
92                  _log.debug("Block received message " + message);
93              }
94          }
95      }
96  
97      public void viewAccepted(View view) {
98          if (_log.isDebugEnabled()) {
99              _log.debug("Accepted view " + view);
100         }
101     }
102 
103     private static Log _log = LogFactoryUtil.getLog(
104         ClusterInvokeReceiver.class);
105 
106     private JChannel _channel;
107 
108 }