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.cache.ehcache;
016    
017    import java.beans.PropertyChangeListener;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collection;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.concurrent.atomic.AtomicInteger;
025    
026    import net.sf.ehcache.CacheException;
027    import net.sf.ehcache.CacheManager;
028    import net.sf.ehcache.Ehcache;
029    import net.sf.ehcache.Element;
030    import net.sf.ehcache.Statistics;
031    import net.sf.ehcache.Status;
032    import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
033    import net.sf.ehcache.config.CacheConfiguration;
034    import net.sf.ehcache.event.RegisteredEventListeners;
035    import net.sf.ehcache.exceptionhandler.CacheExceptionHandler;
036    import net.sf.ehcache.extension.CacheExtension;
037    import net.sf.ehcache.loader.CacheLoader;
038    import net.sf.ehcache.search.Attribute;
039    import net.sf.ehcache.search.Query;
040    import net.sf.ehcache.statistics.CacheUsageListener;
041    import net.sf.ehcache.statistics.LiveCacheStatistics;
042    import net.sf.ehcache.statistics.sampled.SampledCacheStatistics;
043    import net.sf.ehcache.terracotta.TerracottaNotRunningException;
044    import net.sf.ehcache.transaction.manager.TransactionManagerLookup;
045    import net.sf.ehcache.writer.CacheWriter;
046    import net.sf.ehcache.writer.CacheWriterManager;
047    
048    /**
049     * @author Edward Han
050     */
051    public class ModifiableEhcacheWrapper implements Ehcache {
052    
053            public ModifiableEhcacheWrapper(Ehcache ehcache) {
054                    _ehcache = ehcache;
055            }
056    
057            @Override
058            public void acquireReadLockOnKey(Object key) {
059                    _ehcache.acquireReadLockOnKey(key);
060            }
061    
062            @Override
063            public void acquireWriteLockOnKey(Object key) {
064                    _ehcache.acquireWriteLockOnKey(key);
065            }
066    
067            @Override
068            public void addPropertyChangeListener(
069                    PropertyChangeListener propertyChangeListener) {
070    
071                    _ehcache.addPropertyChangeListener(propertyChangeListener);
072            }
073    
074            public void addReference() {
075                    _referenceCounter.incrementAndGet();
076            }
077    
078            @Override
079            public void bootstrap() {
080                    _ehcache.bootstrap();
081            }
082    
083            @Override
084            public long calculateInMemorySize()
085                    throws CacheException, IllegalStateException {
086    
087                    return _ehcache.calculateInMemorySize();
088            }
089    
090            @Override
091            public long calculateOffHeapSize()
092                    throws CacheException, IllegalStateException {
093    
094                    return _ehcache.calculateOffHeapSize();
095            }
096    
097            @Override
098            public void clearStatistics() {
099                    _ehcache.clearStatistics();
100            }
101    
102            @Override
103            public Object clone() throws CloneNotSupportedException {
104                    return _ehcache.clone();
105            }
106    
107            @Override
108            public Query createQuery() {
109                    return _ehcache.createQuery();
110            }
111    
112            @Override
113            public void disableDynamicFeatures() {
114                    _ehcache.disableDynamicFeatures();
115            }
116    
117            @Override
118            public void dispose() throws IllegalStateException {
119                    _ehcache.dispose();
120            }
121    
122            @Override
123            public boolean equals(Object object) {
124                    return _ehcache.equals(object);
125            }
126    
127            @Override
128            public void evictExpiredElements() {
129                    _ehcache.evictExpiredElements();
130            }
131    
132            @Override
133            public void flush() throws CacheException, IllegalStateException {
134                    _ehcache.flush();
135            }
136    
137            @Override
138            public Element get(Object key)
139                    throws CacheException, IllegalStateException {
140    
141                    return _ehcache.get(key);
142            }
143    
144            @Override
145            public Element get(Serializable key)
146                    throws CacheException, IllegalStateException {
147    
148                    return _ehcache.get(key);
149            }
150    
151            public int getActiveReferenceCount() {
152                    return _referenceCounter.get();
153            }
154    
155            @Override
156            @SuppressWarnings("rawtypes")
157            public Map getAllWithLoader(Collection keys, Object argument)
158                    throws CacheException {
159    
160                    return _ehcache.getAllWithLoader(keys, argument);
161            }
162    
163            @Override
164            public float getAverageGetTime() {
165                    return _ehcache.getAverageGetTime();
166            }
167    
168            @Override
169            public long getAverageSearchTime() {
170                    return _ehcache.getAverageSearchTime();
171            }
172    
173            @Override
174            public BootstrapCacheLoader getBootstrapCacheLoader() {
175                    return _ehcache.getBootstrapCacheLoader();
176            }
177    
178            @Override
179            public CacheConfiguration getCacheConfiguration() {
180                    return _ehcache.getCacheConfiguration();
181            }
182    
183            @Override
184            public RegisteredEventListeners getCacheEventNotificationService() {
185                    return _ehcache.getCacheEventNotificationService();
186            }
187    
188            @Override
189            public CacheExceptionHandler getCacheExceptionHandler() {
190                    return _ehcache.getCacheExceptionHandler();
191            }
192    
193            @Override
194            public CacheManager getCacheManager() {
195                    return _ehcache.getCacheManager();
196            }
197    
198            @Override
199            public int getDiskStoreSize() throws IllegalStateException {
200                    return _ehcache.getDiskStoreSize();
201            }
202    
203            @Override
204            public String getGuid() {
205                    return _ehcache.getGuid();
206            }
207    
208            @Override
209            public Object getInternalContext() {
210                    return _ehcache.getInternalContext();
211            }
212    
213            @Override
214            @SuppressWarnings("rawtypes")
215            public List getKeys() throws CacheException, IllegalStateException {
216                    return _ehcache.getKeys();
217            }
218    
219            @Override
220            @SuppressWarnings("rawtypes")
221            public List getKeysNoDuplicateCheck() throws IllegalStateException {
222                    return _ehcache.getKeysNoDuplicateCheck();
223            }
224    
225            @Override
226            @SuppressWarnings("rawtypes")
227            public List getKeysWithExpiryCheck()
228                    throws CacheException, IllegalStateException {
229    
230                    return _ehcache.getKeysWithExpiryCheck();
231            }
232    
233            @Override
234            public LiveCacheStatistics getLiveCacheStatistics()
235                    throws IllegalStateException {
236    
237                    return _ehcache.getLiveCacheStatistics();
238            }
239    
240            @Override
241            public long getMemoryStoreSize() throws IllegalStateException {
242                    return _ehcache.getMemoryStoreSize();
243            }
244    
245            @Override
246            public String getName() {
247                    return _ehcache.getName();
248            }
249    
250            @Override
251            public long getOffHeapStoreSize() throws IllegalStateException {
252                    return _ehcache.getOffHeapStoreSize();
253            }
254    
255            @Override
256            public Element getQuiet(Object key)
257                    throws CacheException, IllegalStateException {
258    
259                    return _ehcache.getQuiet(key);
260            }
261    
262            @Override
263            public Element getQuiet(Serializable key)
264                    throws CacheException, IllegalStateException {
265    
266                    return _ehcache.getQuiet(key);
267            }
268    
269            @Override
270            public List<CacheExtension> getRegisteredCacheExtensions() {
271                    return _ehcache.getRegisteredCacheExtensions();
272            }
273    
274            @Override
275            public List<CacheLoader> getRegisteredCacheLoaders() {
276                    return _ehcache.getRegisteredCacheLoaders();
277            }
278    
279            @Override
280            public CacheWriter getRegisteredCacheWriter() {
281                    return _ehcache.getRegisteredCacheWriter();
282            }
283    
284            @Override
285            public SampledCacheStatistics getSampledCacheStatistics() {
286                    return _ehcache.getSampledCacheStatistics();
287            }
288    
289            @Override
290            public <T> Attribute<T> getSearchAttribute(String attributeName)
291                    throws CacheException {
292    
293                    return _ehcache.getSearchAttribute(attributeName);
294            }
295    
296            @Override
297            public long getSearchesPerSecond() {
298                    return _ehcache.getSearchesPerSecond();
299            }
300    
301            @Override
302            public int getSize() throws CacheException, IllegalStateException {
303                    return _ehcache.getSize();
304            }
305    
306            @Override
307            public int getSizeBasedOnAccuracy(int statisticsAccuracy)
308                    throws CacheException, IllegalArgumentException, IllegalStateException {
309    
310                    return _ehcache.getSizeBasedOnAccuracy(statisticsAccuracy);
311            }
312    
313            @Override
314            public Statistics getStatistics() throws IllegalStateException {
315                    return _ehcache.getStatistics();
316            }
317    
318            @Override
319            public int getStatisticsAccuracy() {
320                    return _ehcache.getStatisticsAccuracy();
321            }
322    
323            @Override
324            public Status getStatus() {
325                    return _ehcache.getStatus();
326            }
327    
328            @Override
329            public Element getWithLoader(
330                            Object key, CacheLoader cacheLoader, Object argument)
331                    throws CacheException {
332    
333                    return _ehcache.getWithLoader(key, cacheLoader, argument);
334            }
335    
336            public Ehcache getWrappedCache() {
337                    return _ehcache;
338            }
339    
340            @Override
341            public CacheWriterManager getWriterManager() {
342                    return _ehcache.getWriterManager();
343            }
344    
345            @Override
346            public int hashCode() {
347                    return _ehcache.hashCode();
348            }
349    
350            @Override
351            public void initialise() {
352                    _ehcache.initialise();
353            }
354    
355            @Override
356            public boolean isClusterBulkLoadEnabled()
357                    throws TerracottaNotRunningException, UnsupportedOperationException {
358    
359                    return _ehcache.isClusterBulkLoadEnabled();
360            }
361    
362            /**
363             * @deprecated
364             */
365            @Override
366            public boolean isClusterCoherent() {
367                    return _ehcache.isClusterCoherent();
368            }
369    
370            @Override
371            public boolean isDisabled() {
372                    return _ehcache.isDisabled();
373            }
374    
375            @Override
376            public boolean isElementInMemory(Object key) {
377                    return _ehcache.isElementInMemory(key);
378            }
379    
380            @Override
381            public boolean isElementInMemory(Serializable key) {
382                    return _ehcache.isElementInMemory(key);
383            }
384    
385            @Override
386            public boolean isElementOnDisk(Object key) {
387                    return _ehcache.isElementOnDisk(key);
388            }
389    
390            @Override
391            public boolean isElementOnDisk(Serializable key) {
392                    return _ehcache.isElementOnDisk(key);
393            }
394    
395            @Override
396            public boolean isExpired(Element element)
397                    throws IllegalStateException, NullPointerException {
398    
399                    return _ehcache.isExpired(element);
400            }
401    
402            @Override
403            public boolean isKeyInCache(Object key) {
404                    return _ehcache.isKeyInCache(key);
405            }
406    
407            @Override
408            public boolean isNodeBulkLoadEnabled()
409                    throws TerracottaNotRunningException, UnsupportedOperationException {
410    
411                    return _ehcache.isNodeBulkLoadEnabled();
412            }
413    
414            /**
415             * @deprecated
416             */
417            @Override
418            public boolean isNodeCoherent() {
419                    return _ehcache.isNodeCoherent();
420            }
421    
422            @Override
423            public boolean isReadLockedByCurrentThread(Object key) {
424                    return _ehcache.isReadLockedByCurrentThread(key);
425            }
426    
427            @Override
428            public boolean isSampledStatisticsEnabled() {
429                    return _ehcache.isSampledStatisticsEnabled();
430            }
431    
432            @Override
433            public boolean isSearchable() {
434                    return _ehcache.isSearchable();
435            }
436    
437            @Override
438            public boolean isStatisticsEnabled() {
439                    return _ehcache.isStatisticsEnabled();
440            }
441    
442            @Override
443            public boolean isValueInCache(Object value) {
444                    return _ehcache.isValueInCache(value);
445            }
446    
447            @Override
448            public boolean isWriteLockedByCurrentThread(Object key) {
449                    return _ehcache.isWriteLockedByCurrentThread(key);
450            }
451    
452            @Override
453            public void load(Object key) throws CacheException {
454                    _ehcache.load(key);
455            }
456    
457            @Override
458            @SuppressWarnings("rawtypes")
459            public void loadAll(Collection keys, Object argument)
460                    throws CacheException {
461    
462                    _ehcache.loadAll(keys, argument);
463            }
464    
465            @Override
466            public void put(Element element)
467                    throws CacheException, IllegalArgumentException, IllegalStateException {
468    
469                    _ehcache.put(element);
470            }
471    
472            @Override
473            public void put(Element element, boolean doNotNotifyCacheReplicators)
474                    throws CacheException, IllegalArgumentException, IllegalStateException {
475    
476                    _ehcache.put(element, doNotNotifyCacheReplicators);
477            }
478    
479            @Override
480            public Element putIfAbsent(Element element) throws NullPointerException {
481                    return _ehcache.putIfAbsent(element);
482            }
483    
484            @Override
485            public void putQuiet(Element element)
486                    throws CacheException, IllegalArgumentException, IllegalStateException {
487    
488                    _ehcache.putQuiet(element);
489            }
490    
491            @Override
492            public void putWithWriter(Element element)
493                    throws CacheException, IllegalArgumentException, IllegalStateException {
494    
495                    _ehcache.putWithWriter(element);
496            }
497    
498            @Override
499            public void registerCacheExtension(CacheExtension cacheExtension) {
500                    _ehcache.registerCacheExtension(cacheExtension);
501            }
502    
503            @Override
504            public void registerCacheLoader(CacheLoader cacheLoader) {
505                    _ehcache.registerCacheLoader(cacheLoader);
506            }
507    
508            @Override
509            public void registerCacheUsageListener(
510                            CacheUsageListener cacheUsageListener)
511                    throws IllegalStateException {
512    
513                    _ehcache.registerCacheUsageListener(cacheUsageListener);
514            }
515    
516            @Override
517            public void registerCacheWriter(CacheWriter cacheWriter) {
518                    _ehcache.registerCacheWriter(cacheWriter);
519            }
520    
521            @Override
522            public void releaseReadLockOnKey(Object key) {
523                    _ehcache.releaseReadLockOnKey(key);
524            }
525    
526            @Override
527            public void releaseWriteLockOnKey(Object key) {
528                    _ehcache.releaseWriteLockOnKey(key);
529            }
530    
531            @Override
532            public boolean remove(Object key) throws IllegalStateException {
533                    return _ehcache.remove(key);
534            }
535    
536            @Override
537            public boolean remove(Object key, boolean doNotNotifyCacheReplicators)
538                    throws IllegalStateException {
539    
540                    return _ehcache.remove(key, doNotNotifyCacheReplicators);
541            }
542    
543            @Override
544            public boolean remove(Serializable key) throws IllegalStateException {
545                    return _ehcache.remove(key);
546            }
547    
548            @Override
549            public boolean remove(Serializable key, boolean doNotNotifyCacheReplicators)
550                    throws IllegalStateException {
551    
552                    return _ehcache.remove(key, doNotNotifyCacheReplicators);
553            }
554    
555            @Override
556            public void removeAll() throws CacheException, IllegalStateException {
557                    if (!isStatusAlive()) {
558                            return;
559                    }
560    
561                    _ehcache.removeAll();
562            }
563    
564            @Override
565            public void removeAll(boolean doNotNotifyCacheReplicators)
566                    throws CacheException, IllegalStateException {
567    
568                    if (!isStatusAlive()) {
569                            return;
570                    }
571    
572                    _ehcache.removeAll(doNotNotifyCacheReplicators);
573            }
574    
575            @Override
576            public void removeCacheUsageListener(CacheUsageListener cacheUsageListener)
577                    throws IllegalStateException {
578    
579                    _ehcache.removeCacheUsageListener(cacheUsageListener);
580            }
581    
582            @Override
583            public boolean removeElement(Element element) throws NullPointerException {
584                    if (!isStatusAlive()) {
585                            return true;
586                    }
587    
588                    return _ehcache.removeElement(element);
589            }
590    
591            @Override
592            public void removePropertyChangeListener(
593                    PropertyChangeListener propertyChangeListener) {
594    
595                    _ehcache.removePropertyChangeListener(propertyChangeListener);
596            }
597    
598            @Override
599            public boolean removeQuiet(Object key) throws IllegalStateException {
600                    if (!isStatusAlive()) {
601                            return true;
602                    }
603    
604                    return _ehcache.removeQuiet(key);
605            }
606    
607            @Override
608            public boolean removeQuiet(Serializable key) throws IllegalStateException {
609                    if (!isStatusAlive()) {
610                            return true;
611                    }
612    
613                    return _ehcache.removeQuiet(key);
614            }
615    
616            public void removeReference() {
617                    _referenceCounter.decrementAndGet();
618            }
619    
620            @Override
621            public boolean removeWithWriter(Object key)
622                    throws CacheException, IllegalStateException {
623    
624                    if (!isStatusAlive()) {
625                            return true;
626                    }
627    
628                    return _ehcache.removeWithWriter(key);
629            }
630    
631            @Override
632            public Element replace(Element element) throws NullPointerException {
633                    return _ehcache.replace(element);
634            }
635    
636            @Override
637            public boolean replace(Element oldElement, Element newElement)
638                    throws IllegalArgumentException, NullPointerException {
639    
640                    return _ehcache.replace(oldElement, newElement);
641            }
642    
643            @Override
644            public void setBootstrapCacheLoader(
645                            BootstrapCacheLoader bootstrapCacheLoader)
646                    throws CacheException {
647    
648                    _ehcache.setBootstrapCacheLoader(bootstrapCacheLoader);
649            }
650    
651            @Override
652            public void setCacheExceptionHandler(
653                    CacheExceptionHandler cacheExceptionHandler) {
654    
655                    _ehcache.setCacheExceptionHandler(cacheExceptionHandler);
656            }
657    
658            @Override
659            public void setCacheManager(CacheManager cacheManager) {
660                    _ehcache.setCacheManager(cacheManager);
661            }
662    
663            @Override
664            public void setDisabled(boolean disabled) {
665                    _ehcache.setDisabled(disabled);
666            }
667    
668            @Override
669            public void setDiskStorePath(String diskStorePath) throws CacheException {
670                    _ehcache.setDiskStorePath(diskStorePath);
671            }
672    
673            @Override
674            public void setName(String name) {
675                    _ehcache.setName(name);
676            }
677    
678            @Override
679            public void setNodeBulkLoadEnabled(boolean enabledBulkLoad)
680                    throws TerracottaNotRunningException, UnsupportedOperationException {
681    
682                    _ehcache.setNodeBulkLoadEnabled(enabledBulkLoad);
683            }
684    
685            /**
686             * @deprecated
687             */
688            @Override
689            public void setNodeCoherent(boolean nodeCoherent)
690                    throws UnsupportedOperationException {
691    
692                    _ehcache.setNodeCoherent(nodeCoherent);
693            }
694    
695            @Override
696            public void setSampledStatisticsEnabled(boolean sampleStatisticsEnabled) {
697                    _ehcache.setSampledStatisticsEnabled(sampleStatisticsEnabled);
698            }
699    
700            @Override
701            public void setStatisticsAccuracy(int statisticsAccuracy) {
702                    _ehcache.setStatisticsAccuracy(statisticsAccuracy);
703            }
704    
705            @Override
706            public void setStatisticsEnabled(boolean statisticsEnabled) {
707                    _ehcache.setStatisticsEnabled(statisticsEnabled);
708            }
709    
710            @Override
711            public void setTransactionManagerLookup(
712                    TransactionManagerLookup transactionManagerLookup) {
713    
714                    _ehcache.setTransactionManagerLookup(transactionManagerLookup);
715            }
716    
717            public void setWrappedCache(Ehcache ehcache) {
718                    _ehcache = ehcache;
719            }
720    
721            @Override
722            public boolean tryReadLockOnKey(Object key, long timeout)
723                    throws InterruptedException {
724    
725                    return _ehcache.tryReadLockOnKey(key, timeout);
726            }
727    
728            @Override
729            public boolean tryWriteLockOnKey(Object key, long timeout)
730                    throws InterruptedException {
731    
732                    return _ehcache.tryWriteLockOnKey(key, timeout);
733            }
734    
735            @Override
736            public void unregisterCacheExtension(CacheExtension cacheExtension) {
737                    _ehcache.unregisterCacheExtension(cacheExtension);
738            }
739    
740            @Override
741            public void unregisterCacheLoader(CacheLoader cacheLoader) {
742                    _ehcache.unregisterCacheLoader(cacheLoader);
743            }
744    
745            @Override
746            public void unregisterCacheWriter() {
747                    _ehcache.unregisterCacheWriter();
748            }
749    
750            @Override
751            public void waitUntilClusterBulkLoadComplete()
752                    throws TerracottaNotRunningException, UnsupportedOperationException {
753    
754                    _ehcache.waitUntilClusterBulkLoadComplete();
755            }
756    
757            /**
758             * @deprecated
759             */
760            @Override
761            public void waitUntilClusterCoherent()
762                    throws UnsupportedOperationException {
763    
764                    _ehcache.waitUntilClusterCoherent();
765            }
766    
767            protected boolean isStatusAlive() {
768                    Status status = _ehcache.getStatus();
769    
770                    if (status.equals(Status.STATUS_ALIVE)) {
771                            return true;
772                    }
773                    else {
774                            return false;
775                    }
776            }
777    
778            private Ehcache _ehcache;
779            private AtomicInteger _referenceCounter = new AtomicInteger(0);
780    
781    }