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.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            @Override
050            public boolean contains(Object object) {
051                    return _ehcacheDataRegion.contains(object);
052            }
053    
054            @Override
055            public void destroy() throws CacheException {
056                    EhcacheDataRegion ehcacheDataRegion = getEhcacheDataRegion();
057    
058                    Ehcache ehcache = ehcacheDataRegion.getEhcache();
059    
060                    if (ehcache instanceof ModifiableEhcacheWrapper) {
061                            ModifiableEhcacheWrapper modifiableEhcacheWrapper =
062                                    (ModifiableEhcacheWrapper)ehcache;
063    
064                            modifiableEhcacheWrapper.removeReference();
065    
066                            if (modifiableEhcacheWrapper.getActiveReferenceCount() == 0) {
067                                    doDestroy();
068                            }
069                    }
070                    else {
071                            doDestroy();
072                    }
073            }
074    
075            @Override
076            public long getElementCountInMemory() {
077                    return _ehcacheDataRegion.getElementCountInMemory();
078            }
079    
080            @Override
081            public long getElementCountOnDisk() {
082                    return _ehcacheDataRegion.getElementCountOnDisk();
083            }
084    
085            @Override
086            public String getName() {
087                    return _ehcacheDataRegion.getName();
088            }
089    
090            @Override
091            public String getRegistryName() {
092                    return getName();
093            }
094    
095            @Override
096            public long getSizeInMemory() {
097                    return _ehcacheDataRegion.getSizeInMemory();
098            }
099    
100            @Override
101            public int getTimeout() {
102                    return _ehcacheDataRegion.getTimeout();
103            }
104    
105            @Override
106            public long nextTimestamp() {
107                    return _ehcacheDataRegion.nextTimestamp();
108            }
109    
110            @Override
111            @SuppressWarnings("rawtypes")
112            public Map toMap() {
113                    return _ehcacheDataRegion.toMap();
114            }
115    
116            @Override
117            public String toString() {
118                    return _ehcacheDataRegion.toString();
119            }
120    
121            protected void doDestroy() {
122                    _ehcacheDataRegion.destroy();
123    
124                    CacheRegistryUtil.unregister(getRegistryName());
125            }
126    
127            protected EhcacheDataRegion getEhcacheDataRegion() {
128                    return _ehcacheDataRegion;
129            }
130    
131            private EhcacheDataRegion _ehcacheDataRegion;
132    
133    }