001    /**
002     * Copyright (c) 2000-present 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                    PollerRequest pollerRequest, String responseId) {
033    
034                    String portletId = pollerRequest.getPortletId();
035    
036                    // Do not process a new request if there is a request already pending.
037                    // This prevents flooding the server in the event of slow receive
038                    // requests.
039    
040                    if (_pendingResponseIds.containsKey(portletId)) {
041                            return false;
042                    }
043    
044                    _pendingResponseIds.put(portletId, responseId);
045    
046                    _pollerRequests.put(portletId, pollerRequest);
047    
048                    return true;
049            }
050    
051            public synchronized boolean completePortletProcessing(
052                    String portletId, String responseId) {
053    
054                    String pendingResponseId = _pendingResponseIds.get(portletId);
055    
056                    if (responseId.equals(pendingResponseId)) {
057                            _pendingResponseIds.remove(portletId);
058    
059                            _pollerRequests.remove(portletId);
060                    }
061    
062                    return _pendingResponseIds.isEmpty();
063            }
064    
065            public String getPollerSessionId() {
066                    return _pollerSessionId;
067            }
068    
069            private final Map<String, String> _pendingResponseIds = new HashMap<>();
070            private final Map<String, PollerRequest> _pollerRequests = new HashMap<>();
071            private final String _pollerSessionId;
072    
073    }