001    /**
002     * Copyright (c) 2000-2013 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.dao.orm.hibernate.region;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    
020    import java.util.Properties;
021    
022    import org.hibernate.cache.CacheDataDescription;
023    import org.hibernate.cache.CacheException;
024    import org.hibernate.cache.CollectionRegion;
025    import org.hibernate.cache.EntityRegion;
026    import org.hibernate.cache.QueryResultsRegion;
027    import org.hibernate.cache.RegionFactory;
028    import org.hibernate.cache.TimestampsRegion;
029    import org.hibernate.cache.access.AccessType;
030    import org.hibernate.cfg.Settings;
031    
032    /**
033     * @author Edward Han
034     * @author Shuyang Zhou
035     */
036    public class SingletonLiferayEhcacheRegionFactory implements RegionFactory {
037    
038            public static LiferayEhcacheRegionFactory getInstance() {
039                    return _liferayEhcacheRegionFactory;
040            }
041    
042            public SingletonLiferayEhcacheRegionFactory(Properties properties) {
043                    synchronized (this) {
044                            boolean useQueryCache = GetterUtil.getBoolean(
045                                    properties.get(PropsKeys.HIBERNATE_CACHE_USE_QUERY_CACHE));
046                            boolean useSecondLevelCache = GetterUtil.getBoolean(
047                                    properties.get(
048                                            PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
049    
050                            if (useQueryCache || useSecondLevelCache) {
051                                    _enabled = true;
052                            }
053    
054                            if (_liferayEhcacheRegionFactory == null) {
055                                    _liferayEhcacheRegionFactory = new LiferayEhcacheRegionFactory(
056                                            properties);
057                            }
058                    }
059            }
060    
061            @Override
062            public CollectionRegion buildCollectionRegion(
063                            String regionName, Properties properties,
064                            CacheDataDescription cacheDataDescription)
065                    throws CacheException {
066    
067                    return _liferayEhcacheRegionFactory.buildCollectionRegion(
068                            regionName, properties, cacheDataDescription);
069            }
070    
071            @Override
072            public EntityRegion buildEntityRegion(
073                            String regionName, Properties properties,
074                            CacheDataDescription cacheDataDescription)
075                    throws CacheException {
076    
077                    return _liferayEhcacheRegionFactory.buildEntityRegion(
078                            regionName, properties, cacheDataDescription);
079            }
080    
081            @Override
082            public QueryResultsRegion buildQueryResultsRegion(
083                            String regionName, Properties properties)
084                    throws CacheException {
085    
086                    return _liferayEhcacheRegionFactory.buildQueryResultsRegion(
087                            regionName, properties);
088            }
089    
090            @Override
091            public TimestampsRegion buildTimestampsRegion(
092                            String regionName, Properties properties)
093                    throws CacheException {
094    
095                    return _liferayEhcacheRegionFactory.buildTimestampsRegion(
096                            regionName, properties);
097            }
098    
099            @Override
100            public AccessType getDefaultAccessType() {
101                    return _liferayEhcacheRegionFactory.getDefaultAccessType();
102            }
103    
104            @Override
105            public boolean isMinimalPutsEnabledByDefault() {
106                    return _liferayEhcacheRegionFactory.isMinimalPutsEnabledByDefault();
107            }
108    
109            @Override
110            public long nextTimestamp() {
111                    return _liferayEhcacheRegionFactory.nextTimestamp();
112            }
113    
114            @Override
115            public synchronized void start(Settings settings, Properties properties)
116                    throws CacheException {
117    
118                    if (_enabled && (_instanceCounter++ == 0)) {
119                            _liferayEhcacheRegionFactory.start(settings, properties);
120                    }
121            }
122    
123            @Override
124            public synchronized void stop() {
125                    if (_enabled && (--_instanceCounter == 0)) {
126                            _liferayEhcacheRegionFactory.stop();
127                    }
128            }
129    
130            private static boolean _enabled;
131            private static int _instanceCounter;
132            private static LiferayEhcacheRegionFactory _liferayEhcacheRegionFactory;
133    
134    }