1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.poller;
24  
25  import com.liferay.portal.kernel.json.JSONArray;
26  import com.liferay.portal.kernel.messaging.Message;
27  import com.liferay.portal.kernel.messaging.MessageBusUtil;
28  import com.liferay.portal.kernel.messaging.MessageListener;
29  import com.liferay.portal.kernel.poller.PollerRequest;
30  import com.liferay.portal.kernel.poller.PollerResponse;
31  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * <a href="PollerRequestManager.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Michael C. Han
40   */
41  public class PollerRequestManager implements MessageListener {
42  
43      public PollerRequestManager(
44          JSONArray pollerResponseChunksJSON, String destinationName,
45          String responseDestinationName, long timeout) {
46  
47          _pollerResponseChunksJSON = pollerResponseChunksJSON;
48          _destinationName = destinationName;
49          _responseDestinationName = responseDestinationName;
50          _timeout = timeout;
51      }
52  
53      public void addPollerRequest(PollerRequest pollerRequest) {
54          if (pollerRequest == null) {
55              return;
56          }
57  
58          _pollerRequests.put(pollerRequest.getPortletId(), pollerRequest);
59      }
60  
61      public void clearRequests() {
62          _pollerRequests.clear();
63          _responseIds.clear();
64          _responseCount = 0;
65      }
66  
67      public JSONArray getPollerResponse() {
68          return _pollerResponseChunksJSON;
69      }
70  
71      public void processRequests() {
72          MessageBusUtil.registerMessageListener(_responseDestinationName, this);
73  
74          try {
75              for (PollerRequest pollerRequest : _pollerRequests.values()) {
76                  Message message = new Message();
77  
78                  message.setPayload(pollerRequest);
79                  message.setResponseDestinationName(_responseDestinationName);
80  
81                  String responseId = PortalUUIDUtil.generate();
82  
83                  message.setResponseId(responseId);
84  
85                  _responseIds.put(responseId, responseId);
86  
87                  MessageBusUtil.sendMessage(_destinationName, message);
88              }
89  
90              synchronized (this) {
91                  if (_responseCount != _pollerRequests.size()) {
92                      try {
93                          this.wait(_timeout);
94                      }
95                      catch (InterruptedException ie) {
96                      }
97                  }
98              }
99          }
100         finally {
101             MessageBusUtil.unregisterMessageListener(
102                 _responseDestinationName, this);
103         }
104     }
105 
106     public void receive(Message message) {
107         if (!_responseIds.containsKey(message.getResponseId())) {
108             return;
109         }
110 
111         if (_pollerResponseChunksJSON != null) {
112             PollerResponse pollerResponse =
113                 (PollerResponse)message.getPayload();
114 
115             if (pollerResponse != null) {
116                 _pollerResponseChunksJSON.put(pollerResponse.toJSONObject());
117             }
118         }
119 
120         synchronized (this) {
121             _responseCount++;
122 
123             if (_responseCount == _pollerRequests.size()) {
124                 notify();
125             }
126         }
127     }
128 
129     private String _destinationName;
130     private Map<String, PollerRequest> _pollerRequests =
131         new HashMap<String, PollerRequest>();
132     private JSONArray _pollerResponseChunksJSON;
133     private int _responseCount;
134     private String _responseDestinationName;
135     private Map<String, String> _responseIds = new HashMap<String, String>();
136     private long _timeout;
137 
138 }