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.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   */
32  public class ProxyMessageListener implements MessageListener {
33  
34      public void receive(Message message) {
35          ProxyResponse proxyResponse = new ProxyResponse();
36  
37          try {
38              Object payload = message.getPayload();
39  
40              if (payload == null) {
41                  throw new Exception("Payload is null");
42              }
43              else if (!ProxyRequest.class.isAssignableFrom(payload.getClass())) {
44                  throw new Exception(
45                      "Payload " + payload.getClass() + " is not of type " +
46                          ProxyRequest.class.getName());
47              }
48              else {
49                  ProxyRequest proxyRequest = (ProxyRequest)payload;
50  
51                  Object result = proxyRequest.execute(_manager);
52  
53                  proxyResponse.setResult(result);
54              }
55          }
56          catch (Exception e) {
57              if (_log.isDebugEnabled()) {
58                  _log.debug(e, e);
59              }
60  
61              proxyResponse.setException(e);
62          }
63          finally {
64              String responseDestinationName =
65                  message.getResponseDestinationName();
66  
67              if (Validator.isNotNull(responseDestinationName)) {
68                  Message responseMessage = MessageBusUtil.createResponseMessage(
69                      message);
70  
71                  responseMessage.setPayload(proxyResponse);
72  
73                  _messageBus.sendMessage(
74                      responseDestinationName, responseMessage);
75              }
76          }
77      }
78  
79      public void setManager(Object manager) {
80          _manager = manager;
81      }
82  
83      public void setMessageBus(MessageBus messageBus) {
84          _messageBus = messageBus;
85      }
86  
87      private static Log _log = LogFactoryUtil.getLog(ProxyMessageListener.class);
88  
89      private Object _manager;
90      private MessageBus _messageBus;
91  
92  }