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;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPool;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.cache.PortalCacheManager;
020    import com.liferay.portal.kernel.cache.PortalCacheManagerNames;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.resiliency.spi.cache.SPIPortalCacheManagerConfigurator;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.registry.Filter;
026    import com.liferay.registry.Registry;
027    import com.liferay.registry.RegistryUtil;
028    import com.liferay.registry.ServiceTracker;
029    
030    import java.io.Serializable;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Michael Young
035     */
036    @DoPrivileged
037    public class MultiVMPoolImpl implements MultiVMPool {
038    
039            public MultiVMPoolImpl() {
040                    Registry registry = RegistryUtil.getRegistry();
041    
042                    StringBundler sb = new StringBundler(11);
043    
044                    sb.append("(&(objectClass=");
045                    sb.append(PortalCacheManager.class.getName());
046                    sb.append(")(");
047                    sb.append(PortalCacheManager.PORTAL_CACHE_MANAGER_NAME);
048                    sb.append("=");
049                    sb.append(PortalCacheManagerNames.MULTI_VM);
050                    sb.append(")(");
051                    sb.append(PortalCacheManager.PORTAL_CACHE_MANAGER_TYPE);
052                    sb.append("=");
053                    sb.append(PropsValues.PORTAL_CACHE_MANAGER_TYPE_MULTI_VM);
054                    sb.append("))");
055    
056                    Filter filter = registry.getFilter(sb.toString());
057    
058                    ServiceTracker<PortalCacheManager
059                            <? extends Serializable, ? extends Serializable>, PortalCacheManager
060                                    <? extends Serializable, ? extends Serializable>>
061                                            serviceTracker = registry.trackServices(filter);
062    
063                    serviceTracker.open();
064    
065                    try {
066                            _portalCacheManager =
067                                    SPIPortalCacheManagerConfigurator.createSPIPortalCacheManager(
068                                            serviceTracker.waitForService(0));
069                    }
070                    catch (Exception e) {
071                            throw new IllegalStateException(
072                                    "Unable to initialize multi VM pool", e);
073                    }
074            }
075    
076            @Override
077            public void clear() {
078                    _portalCacheManager.clearAll();
079            }
080    
081            @Override
082            public PortalCache<? extends Serializable, ? extends Serializable> getCache(
083                    String portalCacheName) {
084    
085                    return _portalCacheManager.getCache(portalCacheName);
086            }
087    
088            @Override
089            public PortalCache<? extends Serializable, ? extends Serializable> getCache(
090                    String portalCacheName, boolean blocking) {
091    
092                    return _portalCacheManager.getCache(portalCacheName, blocking);
093            }
094    
095            @Override
096            public PortalCacheManager<? extends Serializable, ? extends Serializable>
097                    getCacheManager() {
098    
099                    return _portalCacheManager;
100            }
101    
102            @Override
103            public void removeCache(String portalCacheName) {
104                    _portalCacheManager.removeCache(portalCacheName);
105            }
106    
107            private final
108                    PortalCacheManager<? extends Serializable, ? extends Serializable>
109                            _portalCacheManager;
110    
111    }