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 com.liferay.portal.cache.ehcache.ModifiableEhcacheWrapper;
018    import com.liferay.portal.kernel.cache.CacheRegistryItem;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    
021    import java.util.Map;
022    
023    import net.sf.ehcache.Ehcache;
024    import net.sf.ehcache.hibernate.regions.EhcacheDataRegion;
025    
026    import org.hibernate.cache.CacheException;
027    import org.hibernate.cache.Region;
028    
029    /**
030     * @author Edward Han
031     */
032    public abstract class BaseRegionWrapper implements CacheRegistryItem, Region {
033    
034            public BaseRegionWrapper(EhcacheDataRegion ehcacheDataRegion) {
035                    _ehcacheDataRegion = ehcacheDataRegion;
036    
037                    Ehcache ehcache = _ehcacheDataRegion.getEhcache();
038    
039                    if (ehcache instanceof ModifiableEhcacheWrapper) {
040                            ModifiableEhcacheWrapper modifiableEhcacheWrapper =
041                                    (ModifiableEhcacheWrapper)ehcache;
042    
043                            modifiableEhcacheWrapper.addReference();
044                    }
045    
046                    CacheRegistryUtil.register(this);
047            }
048    
049            public boolean contains(Object object) {
050                    return _ehcacheDataRegion.contains(object);
051            }
052    
053            public void destroy() throws CacheException {
054                    EhcacheDataRegion ehcacheDataRegion = getEhcacheDataRegion();
055    
056                    Ehcache ehcache = ehcacheDataRegion.getEhcache();
057    
058                    if (ehcache instanceof ModifiableEhcacheWrapper) {
059                            ModifiableEhcacheWrapper modifiableEhcacheWrapper =
060                                    (ModifiableEhcacheWrapper)ehcache;
061    
062                            modifiableEhcacheWrapper.removeReference();
063    
064                            if (modifiableEhcacheWrapper.getActiveReferenceCount() == 0) {
065                                    doDestroy();
066                            }
067                    }
068                    else {
069                            doDestroy();
070                    }
071            }
072    
073            public long getElementCountInMemory() {
074                    return _ehcacheDataRegion.getElementCountInMemory();
075            }
076    
077            public long getElementCountOnDisk() {
078                    return _ehcacheDataRegion.getElementCountOnDisk();
079            }
080    
081            public String getName() {
082                    return _ehcacheDataRegion.getName();
083            }
084    
085            public String getRegistryName() {
086                    return getName();
087            }
088    
089            public long getSizeInMemory() {
090                    return _ehcacheDataRegion.getSizeInMemory();
091            }
092    
093            public int getTimeout() {
094                    return _ehcacheDataRegion.getTimeout();
095            }
096    
097            public long nextTimestamp() {
098                    return _ehcacheDataRegion.nextTimestamp();
099            }
100    
101            @SuppressWarnings("rawtypes")
102            public Map toMap() {
103                    return _ehcacheDataRegion.toMap();
104            }
105    
106            @Override
107            public String toString() {
108                    return _ehcacheDataRegion.toString();
109            }
110    
111            protected void doDestroy() {
112                    _ehcacheDataRegion.destroy();
113    
114                    CacheRegistryUtil.unregister(getRegistryName());
115            }
116    
117            protected EhcacheDataRegion getEhcacheDataRegion() {
118                    return _ehcacheDataRegion;
119            }
120    
121            private EhcacheDataRegion _ehcacheDataRegion;
122    
123    }