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