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.cache.mvcc;
016    
017    import com.liferay.portal.kernel.cache.LowLevelCache;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.cache.PortalCacheWrapper;
020    import com.liferay.portal.model.MVCCModel;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class MVCCPortalCacheFactory {
028    
029            public static <K extends Serializable>
030                    PortalCache<K, ?> createMVCCEhcachePortalCache(
031                            PortalCache<K, ?> portalCache) {
032    
033                    if (portalCache instanceof LowLevelCache) {
034                            return new MVCCPortalCache<>(
035                                    (LowLevelCache<K, MVCCModel>)portalCache);
036                    }
037    
038                    PortalCache<K, ?> currentPortalCache = portalCache;
039    
040                    while (currentPortalCache instanceof PortalCacheWrapper) {
041                            PortalCacheWrapper<K, MVCCModel> portalCacheWrapper =
042                                    (PortalCacheWrapper<K, MVCCModel>)currentPortalCache;
043    
044                            PortalCache<K, MVCCModel> nextPortalCache =
045                                    portalCacheWrapper.getWrappedPortalCache();
046    
047                            if (nextPortalCache instanceof LowLevelCache) {
048                                    nextPortalCache = new MVCCPortalCache<>(
049                                            (LowLevelCache<K, MVCCModel>)nextPortalCache);
050    
051                                    portalCacheWrapper.setPortalCache(nextPortalCache);
052    
053                                    break;
054                            }
055                            else {
056                                    currentPortalCache = nextPortalCache;
057                            }
058                    }
059    
060                    return portalCache;
061            }
062    
063    }