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.poller.messaging;
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.MessageBusUtil;
21  import com.liferay.portal.kernel.messaging.MessageListener;
22  import com.liferay.portal.kernel.poller.PollerException;
23  import com.liferay.portal.kernel.poller.PollerProcessor;
24  import com.liferay.portal.kernel.poller.PollerRequest;
25  import com.liferay.portal.kernel.poller.PollerResponse;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.poller.PollerProcessorUtil;
28  
29  /**
30   * <a href="PollerMessageListener.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Michael C. Han
33   */
34  public class PollerMessageListener implements MessageListener {
35  
36      public void receive(Message message) {
37          PollerResponse pollerResponse = null;
38  
39          try {
40              PollerRequest pollerRequest = (PollerRequest)message.getPayload();
41  
42              String portletId = pollerRequest.getPortletId();
43  
44              PollerProcessor pollerProcessor =
45                  PollerProcessorUtil.getPollerProcessor(portletId);
46  
47              if (pollerRequest.isReceiveRequest()) {
48                  pollerResponse = new PollerResponse(
49                      portletId, pollerRequest.getChunkId());
50  
51                  try {
52                      pollerProcessor.receive(pollerRequest, pollerResponse);
53                  }
54                  catch (PollerException pe) {
55                      _log.error(
56                          "Unable to receive poller request " + pollerRequest,
57                          pe);
58  
59                      pollerResponse.setParameter(
60                          "pollerException", pe.getMessage());
61                  }
62              }
63              else {
64                  try {
65                      pollerProcessor.send(pollerRequest);
66                  }
67                  catch (PollerException pe) {
68                      _log.error(
69                          "Unable to send poller request " + pollerRequest,
70                          pe);
71                  }
72              }
73          }
74          finally {
75              String responseDestinationName =
76                  message.getResponseDestinationName();
77  
78              if (Validator.isNotNull(responseDestinationName)) {
79                  Message responseMessage = MessageBusUtil.createResponseMessage(
80                      message);
81  
82                  responseMessage.setPayload(pollerResponse);
83  
84                  MessageBusUtil.sendMessage(
85                      responseDestinationName, responseMessage);
86              }
87          }
88      }
89  
90      private static Log _log = LogFactoryUtil.getLog(
91          PollerMessageListener.class);
92  
93  }