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 java.util.Properties;
018    import java.util.concurrent.atomic.AtomicInteger;
019    
020    import org.hibernate.cache.CacheDataDescription;
021    import org.hibernate.cache.CacheException;
022    import org.hibernate.cache.CollectionRegion;
023    import org.hibernate.cache.EntityRegion;
024    import org.hibernate.cache.QueryResultsRegion;
025    import org.hibernate.cache.RegionFactory;
026    import org.hibernate.cache.TimestampsRegion;
027    import org.hibernate.cache.access.AccessType;
028    import org.hibernate.cfg.Settings;
029    
030    /**
031     * @author Edward Han
032     */
033    public class SingletonLiferayEhcacheRegionFactory implements RegionFactory {
034    
035            public static LiferayEhcacheRegionFactory getInstance() {
036                    return _liferayEhcacheRegionFactory;
037            }
038    
039            public SingletonLiferayEhcacheRegionFactory(Properties properties) {
040                    synchronized (this) {
041                            if (_liferayEhcacheRegionFactory == null) {
042                                    _liferayEhcacheRegionFactory = new LiferayEhcacheRegionFactory(
043                                            properties);
044                            }
045                    }
046            }
047    
048            @Override
049            public CollectionRegion buildCollectionRegion(
050                            String regionName, Properties properties,
051                            CacheDataDescription cacheDataDescription)
052                    throws CacheException {
053    
054                    return _liferayEhcacheRegionFactory.buildCollectionRegion(
055                            regionName, properties, cacheDataDescription);
056            }
057    
058            @Override
059            public EntityRegion buildEntityRegion(
060                            String regionName, Properties properties,
061                            CacheDataDescription cacheDataDescription)
062                    throws CacheException {
063    
064                    return _liferayEhcacheRegionFactory.buildEntityRegion(
065                            regionName, properties, cacheDataDescription);
066            }
067    
068            @Override
069            public QueryResultsRegion buildQueryResultsRegion(
070                            String regionName, Properties properties)
071                    throws CacheException {
072    
073                    return _liferayEhcacheRegionFactory.buildQueryResultsRegion(
074                            regionName, properties);
075            }
076    
077            @Override
078            public TimestampsRegion buildTimestampsRegion(
079                            String regionName, Properties properties)
080                    throws CacheException {
081    
082                    return _liferayEhcacheRegionFactory.buildTimestampsRegion(
083                            regionName, properties);
084            }
085    
086            @Override
087            public AccessType getDefaultAccessType() {
088                    return _liferayEhcacheRegionFactory.getDefaultAccessType();
089            }
090    
091            @Override
092            public boolean isMinimalPutsEnabledByDefault() {
093                    return _liferayEhcacheRegionFactory.isMinimalPutsEnabledByDefault();
094            }
095    
096            @Override
097            public long nextTimestamp() {
098                    return _liferayEhcacheRegionFactory.nextTimestamp();
099            }
100    
101            @Override
102            public synchronized void start(Settings settings, Properties properties)
103                    throws CacheException {
104    
105                    if (_instanceCounter.getAndIncrement() == 0) {
106                            _liferayEhcacheRegionFactory.start(settings, properties);
107                    }
108            }
109    
110            @Override
111            public synchronized void stop() {
112                    if (_instanceCounter.decrementAndGet() == 0) {
113                            _liferayEhcacheRegionFactory.stop();
114                    }
115            }
116    
117            private static AtomicInteger _instanceCounter = new AtomicInteger(0);
118            private static LiferayEhcacheRegionFactory _liferayEhcacheRegionFactory;
119    
120    }