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;
16  
17  import com.liferay.portal.kernel.json.JSONArray;
18  import com.liferay.portal.kernel.messaging.Message;
19  import com.liferay.portal.kernel.messaging.MessageBusUtil;
20  import com.liferay.portal.kernel.messaging.MessageListener;
21  import com.liferay.portal.kernel.poller.PollerRequest;
22  import com.liferay.portal.kernel.poller.PollerResponse;
23  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * <a href="PollerRequestManager.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Michael C. Han
32   */
33  public class PollerRequestManager implements MessageListener {
34  
35      public PollerRequestManager(
36          JSONArray pollerResponseChunksJSON, String destinationName,
37          String responseDestinationName, long timeout) {
38  
39          _pollerResponseChunksJSON = pollerResponseChunksJSON;
40          _destinationName = destinationName;
41          _responseDestinationName = responseDestinationName;
42          _timeout = timeout;
43      }
44  
45      public void addPollerRequest(PollerRequest pollerRequest) {
46          if (pollerRequest == null) {
47              return;
48          }
49  
50          _pollerRequests.put(pollerRequest.getPortletId(), pollerRequest);
51      }
52  
53      public void clearRequests() {
54          _pollerRequests.clear();
55          _responseIds.clear();
56          _responseCount = 0;
57      }
58  
59      public JSONArray getPollerResponse() {
60          return _pollerResponseChunksJSON;
61      }
62  
63      public void processRequests() {
64          MessageBusUtil.registerMessageListener(_responseDestinationName, this);
65  
66          try {
67              for (PollerRequest pollerRequest : _pollerRequests.values()) {
68                  Message message = new Message();
69  
70                  message.setPayload(pollerRequest);
71                  message.setResponseDestinationName(_responseDestinationName);
72  
73                  String responseId = PortalUUIDUtil.generate();
74  
75                  message.setResponseId(responseId);
76  
77                  _responseIds.put(responseId, responseId);
78  
79                  MessageBusUtil.sendMessage(_destinationName, message);
80              }
81  
82              synchronized (this) {
83                  if (_responseCount != _pollerRequests.size()) {
84                      try {
85                          this.wait(_timeout);
86                      }
87                      catch (InterruptedException ie) {
88                      }
89                  }
90              }
91          }
92          finally {
93              MessageBusUtil.unregisterMessageListener(
94                  _responseDestinationName, this);
95          }
96      }
97  
98      public void receive(Message message) {
99          if (!_responseIds.containsKey(message.getResponseId())) {
100             return;
101         }
102 
103         if (_pollerResponseChunksJSON != null) {
104             PollerResponse pollerResponse =
105                 (PollerResponse)message.getPayload();
106 
107             if (pollerResponse != null) {
108                 _pollerResponseChunksJSON.put(pollerResponse.toJSONObject());
109             }
110         }
111 
112         synchronized (this) {
113             _responseCount++;
114 
115             if (_responseCount == _pollerRequests.size()) {
116                 notify();
117             }
118         }
119     }
120 
121     private String _destinationName;
122     private Map<String, PollerRequest> _pollerRequests =
123         new HashMap<String, PollerRequest>();
124     private JSONArray _pollerResponseChunksJSON;
125     private int _responseCount;
126     private String _responseDestinationName;
127     private Map<String, String> _responseIds = new HashMap<String, String>();
128     private long _timeout;
129 
130 }