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