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.nio.intraband.proxy;
016    
017    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
018    import com.liferay.portal.kernel.resiliency.spi.SPI;
019    import com.liferay.portal.kernel.resiliency.spi.SPIRegistryUtil;
020    import com.liferay.portal.nio.intraband.proxy.StubHolder.StubCreator;
021    
022    import java.rmi.RemoteException;
023    
024    import java.util.concurrent.ConcurrentHashMap;
025    import java.util.concurrent.ConcurrentMap;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class StubMapImpl<T>
031            extends ConcurrentHashMap<String, T> implements StubMap<T> {
032    
033            public StubMapImpl(StubCreator<T> stubCreator) {
034                    _stubCreator = stubCreator;
035            }
036    
037            @Override
038            public T get(Object key) {
039                    String portletId = String.valueOf(key);
040    
041                    StubHolder<T> stubHolder = _stubHolders.get(portletId);
042    
043                    if (stubHolder != null) {
044                            return stubHolder.getStub();
045                    }
046    
047                    T originalValue = super.get(key);
048    
049                    if (originalValue == null) {
050                            return null;
051                    }
052    
053                    RegistrationReference registrationReference = _getRegistrationReference(
054                            portletId);
055    
056                    if (registrationReference == null) {
057                            return originalValue;
058                    }
059    
060                    stubHolder = new StubHolder<>(
061                            originalValue, portletId, registrationReference, _stubCreator);
062    
063                    StubHolder<T> previousStubHolder = _stubHolders.putIfAbsent(
064                            portletId, stubHolder);
065    
066                    if (previousStubHolder != null) {
067                            stubHolder = previousStubHolder;
068                    }
069    
070                    return stubHolder.getStub();
071            }
072    
073            @Override
074            public boolean removeStubHolder(String portletId, T stub) {
075                    return _stubHolders.remove(portletId, stub);
076            }
077    
078            private RegistrationReference _getRegistrationReference(String portletId) {
079                    SPI spi = SPIRegistryUtil.getPortletSPI(portletId);
080    
081                    if (spi == null) {
082                            return null;
083                    }
084    
085                    try {
086                            return spi.getRegistrationReference();
087                    }
088                    catch (RemoteException re) {
089                            throw new RuntimeException(re);
090                    }
091            }
092    
093            private final StubCreator<T> _stubCreator;
094            private final ConcurrentMap<String, StubHolder<T>> _stubHolders =
095                    new ConcurrentHashMap<>();
096    
097    }