001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.poller;
016    
017    import com.liferay.portal.kernel.poller.PollerRequest;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    /**
023     * @author Edward Han
024     */
025    public class PollerSession {
026    
027            public PollerSession(String pollerSessionId) {
028                    _pollerSessionId = pollerSessionId;
029            }
030    
031            public synchronized boolean beginPortletProcessing(
032                    PollerRequestResponsePair pollerRequestResponsePair,
033                    String responseId) {
034    
035                    PollerRequest pollerRequest =
036                            pollerRequestResponsePair.getPollerRequest();
037    
038                    String portletId = pollerRequest.getPortletId();
039    
040                    // Do not process a new request if there is a request already pending.
041                    // This prevents flooding the server in the event of slow receive
042                    // requests.
043    
044                    if (_pendingResponseIds.containsKey(portletId)) {
045                            return false;
046                    }
047    
048                    _pendingResponseIds.put(portletId, responseId);
049    
050                    _pollerRequestResponsePairs.put(portletId, pollerRequestResponsePair);
051    
052                    return true;
053            }
054    
055            public synchronized boolean completePortletProcessing(
056                    String portletId, String responseId) {
057    
058                    String pendingResponseId = _pendingResponseIds.get(portletId);
059    
060                    if (responseId.equals(pendingResponseId)) {
061                            _pendingResponseIds.remove(portletId);
062    
063                            _pollerRequestResponsePairs.remove(portletId);
064                    }
065    
066                    return _pendingResponseIds.isEmpty();
067            }
068    
069            public String getPollerSessionId() {
070                    return _pollerSessionId;
071            }
072    
073            private Map<String, String> _pendingResponseIds =
074                    new HashMap<String, String>();
075            private Map<String, PollerRequestResponsePair> _pollerRequestResponsePairs =
076                    new HashMap<String, PollerRequestResponsePair>();
077            private String _pollerSessionId;
078    
079    }