1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.messaging.proxy;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.Message;
20  import com.liferay.portal.kernel.messaging.MessageBus;
21  import com.liferay.portal.kernel.messaging.MessageBusUtil;
22  import com.liferay.portal.kernel.messaging.MessageListener;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  /**
26   * <a href="ProxyMessageListener.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Micha Kiener
29   * @author Michael C. Han
30   * @author Brian Wing Shun Chan
31   * @author Igor Spasic
32   */
33  public class ProxyMessageListener implements MessageListener {
34  
35      public void receive(Message message) {
36          ProxyResponse proxyResponse = new ProxyResponse();
37  
38          try {
39              Object payload = message.getPayload();
40  
41              if (payload == null) {
42                  throw new Exception("Payload is null");
43              }
44              else if (!ProxyRequest.class.isAssignableFrom(payload.getClass())) {
45                  throw new Exception(
46                      "Payload " + payload.getClass() + " is not of type " +
47                          ProxyRequest.class.getName());
48              }
49              else {
50                  ProxyRequest proxyRequest = (ProxyRequest)payload;
51  
52                  Object result = proxyRequest.execute(_manager);
53  
54                  proxyResponse.setResult(result);
55              }
56          }
57          catch (Exception e) {
58              proxyResponse.setException(e);
59          }
60          finally {
61              String responseDestinationName =
62                  message.getResponseDestinationName();
63  
64              Exception proxyResponseException = proxyResponse.getException();
65  
66              if (Validator.isNotNull(responseDestinationName)) {
67                  Message responseMessage = MessageBusUtil.createResponseMessage(
68                      message);
69  
70                  responseMessage.setPayload(proxyResponse);
71  
72                  if (_log.isDebugEnabled() && (proxyResponseException != null)) {
73                      _log.debug(proxyResponseException, proxyResponseException);
74                  }
75  
76                  _messageBus.sendMessage(
77                      responseDestinationName, responseMessage);
78              }
79              else {
80                  if (proxyResponseException != null) {
81                      _log.error(proxyResponseException, proxyResponseException);
82                  }
83              }
84          }
85      }
86  
87      public void setManager(Object manager) {
88          _manager = manager;
89      }
90  
91      public void setMessageBus(MessageBus messageBus) {
92          _messageBus = messageBus;
93      }
94  
95      private static Log _log = LogFactoryUtil.getLog(ProxyMessageListener.class);
96  
97      private Object _manager;
98      private MessageBus _messageBus;
99  
100 }