001    /**
002     * Copyright (c) 2000-2012 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            public CollectionRegion buildCollectionRegion(
049                            String regionName, Properties properties,
050                            CacheDataDescription cacheDataDescription)
051                    throws CacheException {
052    
053                    return _liferayEhcacheRegionFactory.buildCollectionRegion(
054                            regionName, properties, cacheDataDescription);
055            }
056    
057            public EntityRegion buildEntityRegion(
058                            String regionName, Properties properties,
059                            CacheDataDescription cacheDataDescription)
060                    throws CacheException {
061    
062                    return _liferayEhcacheRegionFactory.buildEntityRegion(
063                            regionName, properties, cacheDataDescription);
064            }
065    
066            public QueryResultsRegion buildQueryResultsRegion(
067                            String regionName, Properties properties)
068                    throws CacheException {
069    
070                    return _liferayEhcacheRegionFactory.buildQueryResultsRegion(
071                            regionName, properties);
072            }
073    
074            public TimestampsRegion buildTimestampsRegion(
075                            String regionName, Properties properties)
076                    throws CacheException {
077    
078                    return _liferayEhcacheRegionFactory.buildTimestampsRegion(
079                            regionName, properties);
080            }
081    
082            public AccessType getDefaultAccessType() {
083                    return _liferayEhcacheRegionFactory.getDefaultAccessType();
084            }
085    
086            public boolean isMinimalPutsEnabledByDefault() {
087                    return _liferayEhcacheRegionFactory.isMinimalPutsEnabledByDefault();
088            }
089    
090            public long nextTimestamp() {
091                    return _liferayEhcacheRegionFactory.nextTimestamp();
092            }
093    
094            public synchronized void start(Settings settings, Properties properties)
095                    throws CacheException {
096    
097                    if (_instanceCounter.getAndIncrement() == 0) {
098                            _liferayEhcacheRegionFactory.start(settings, properties);
099                    }
100            }
101    
102            public synchronized void stop() {
103                    if (_instanceCounter.decrementAndGet() == 0) {
104                            _liferayEhcacheRegionFactory.stop();
105                    }
106            }
107    
108            private static AtomicInteger _instanceCounter = new AtomicInteger(0);
109            private static LiferayEhcacheRegionFactory _liferayEhcacheRegionFactory;
110    
111    }