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