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.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            @Override
086            public long calculateInMemorySize()
087                    throws CacheException, IllegalStateException {
088    
089                    return _ehcache.calculateInMemorySize();
090            }
091    
092            /**
093             * @deprecated As of 6.2.0
094             */
095            @Override
096            public long calculateOffHeapSize()
097                    throws CacheException, IllegalStateException {
098    
099                    return _ehcache.calculateOffHeapSize();
100            }
101    
102            /**
103             * @deprecated As of 6.2.0
104             */
105            @Override
106            public long calculateOnDiskSize()
107                    throws CacheException, IllegalStateException {
108    
109                    return _ehcache.calculateOnDiskSize();
110            }
111    
112            @Override
113            public Object clone() throws CloneNotSupportedException {
114                    return _ehcache.clone();
115            }
116    
117            @Override
118            public Query createQuery() {
119                    return _ehcache.createQuery();
120            }
121    
122            @Override
123            public void disableDynamicFeatures() {
124                    _ehcache.disableDynamicFeatures();
125            }
126    
127            @Override
128            public void dispose() throws IllegalStateException {
129                    _ehcache.dispose();
130            }
131    
132            @Override
133            public boolean equals(Object object) {
134                    return _ehcache.equals(object);
135            }
136    
137            @Override
138            public void evictExpiredElements() {
139                    _ehcache.evictExpiredElements();
140            }
141    
142            @Override
143            public void flush() throws CacheException, IllegalStateException {
144                    _ehcache.flush();
145            }
146    
147            @Override
148            public Element get(Object key)
149                    throws CacheException, IllegalStateException {
150    
151                    return _ehcache.get(key);
152            }
153    
154            @Override
155            public Element get(Serializable key)
156                    throws CacheException, IllegalStateException {
157    
158                    return _ehcache.get(key);
159            }
160    
161            public int getActiveReferenceCount() {
162                    return _referenceCounter.get();
163            }
164    
165            @Override
166            public Map<Object, Element> getAll(Collection<?> keys)
167                    throws CacheException, IllegalStateException, NullPointerException {
168    
169                    return _ehcache.getAll(keys);
170            }
171    
172            @Override
173            @SuppressWarnings("rawtypes")
174            public Map getAllWithLoader(Collection keys, Object argument)
175                    throws CacheException {
176    
177                    return _ehcache.getAllWithLoader(keys, argument);
178            }
179    
180            @Override
181            public BootstrapCacheLoader getBootstrapCacheLoader() {
182                    return _ehcache.getBootstrapCacheLoader();
183            }
184    
185            @Override
186            public CacheConfiguration getCacheConfiguration() {
187                    return _ehcache.getCacheConfiguration();
188            }
189    
190            @Override
191            public RegisteredEventListeners getCacheEventNotificationService() {
192                    return _ehcache.getCacheEventNotificationService();
193            }
194    
195            @Override
196            public CacheExceptionHandler getCacheExceptionHandler() {
197                    return _ehcache.getCacheExceptionHandler();
198            }
199    
200            @Override
201            public CacheManager getCacheManager() {
202                    return _ehcache.getCacheManager();
203            }
204    
205            /**
206             * @deprecated As of 6.2.0
207             */
208            @Override
209            public int getDiskStoreSize() throws IllegalStateException {
210                    return _ehcache.getDiskStoreSize();
211            }
212    
213            @Override
214            public String getGuid() {
215                    return _ehcache.getGuid();
216            }
217    
218            @Override
219            public Object getInternalContext() {
220                    return _ehcache.getInternalContext();
221            }
222    
223            @Override
224            @SuppressWarnings("rawtypes")
225            public List getKeys() throws CacheException, IllegalStateException {
226                    return _ehcache.getKeys();
227            }
228    
229            /**
230             * @deprecated As of 6.2.0
231             */
232            @Override
233            @SuppressWarnings("rawtypes")
234            public List getKeysNoDuplicateCheck() throws IllegalStateException {
235                    return _ehcache.getKeysNoDuplicateCheck();
236            }
237    
238            @Override
239            @SuppressWarnings("rawtypes")
240            public List getKeysWithExpiryCheck()
241                    throws CacheException, IllegalStateException {
242    
243                    return _ehcache.getKeysWithExpiryCheck();
244            }
245    
246            /**
247             * @deprecated As of 6.2.0
248             */
249            @Override
250            public long getMemoryStoreSize() throws IllegalStateException {
251                    return _ehcache.getMemoryStoreSize();
252            }
253    
254            @Override
255            public String getName() {
256                    return _ehcache.getName();
257            }
258    
259            /**
260             * @deprecated As of 6.2.0
261             */
262            @Override
263            public long getOffHeapStoreSize() throws IllegalStateException {
264                    return _ehcache.getOffHeapStoreSize();
265            }
266    
267            @Override
268            public Element getQuiet(Object key)
269                    throws CacheException, IllegalStateException {
270    
271                    return _ehcache.getQuiet(key);
272            }
273    
274            @Override
275            public Element getQuiet(Serializable key)
276                    throws CacheException, IllegalStateException {
277    
278                    return _ehcache.getQuiet(key);
279            }
280    
281            @Override
282            public List<CacheExtension> getRegisteredCacheExtensions() {
283                    return _ehcache.getRegisteredCacheExtensions();
284            }
285    
286            @Override
287            public List<CacheLoader> getRegisteredCacheLoaders() {
288                    return _ehcache.getRegisteredCacheLoaders();
289            }
290    
291            @Override
292            public CacheWriter getRegisteredCacheWriter() {
293                    return _ehcache.getRegisteredCacheWriter();
294            }
295    
296            @Override
297            public <T> Attribute<T> getSearchAttribute(String attributeName)
298                    throws CacheException {
299    
300                    return _ehcache.getSearchAttribute(attributeName);
301            }
302    
303            @Override
304            public Set<Attribute> getSearchAttributes() throws CacheException {
305                    return _ehcache.getSearchAttributes();
306            }
307    
308            @Override
309            public int getSize() throws CacheException, IllegalStateException {
310                    return _ehcache.getSize();
311            }
312    
313            @Override
314            public StatisticsGateway getStatistics() throws IllegalStateException {
315                    return _ehcache.getStatistics();
316            }
317    
318            @Override
319            public Status getStatus() {
320                    return _ehcache.getStatus();
321            }
322    
323            @Override
324            public Element getWithLoader(
325                            Object key, CacheLoader cacheLoader, Object argument)
326                    throws CacheException {
327    
328                    return _ehcache.getWithLoader(key, cacheLoader, argument);
329            }
330    
331            public Ehcache getWrappedCache() {
332                    return _ehcache;
333            }
334    
335            @Override
336            public CacheWriterManager getWriterManager() {
337                    return _ehcache.getWriterManager();
338            }
339    
340            @Override
341            public boolean hasAbortedSizeOf() {
342                    return _ehcache.hasAbortedSizeOf();
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 As of 6.1.0, replaced by {@link #isClusterBulkLoadEnabled}
364             */
365            @Override
366            public boolean isClusterCoherent() throws TerracottaNotRunningException {
367                    return _ehcache.isClusterBulkLoadEnabled();
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 As of 6.1.0, replaced by {@link #isNodeBulkLoadEnabled}
416             */
417            @Override
418            public boolean isNodeCoherent() throws TerracottaNotRunningException {
419                    return _ehcache.isNodeBulkLoadEnabled();
420            }
421    
422            @Override
423            public boolean isReadLockedByCurrentThread(Object key)
424                    throws UnsupportedOperationException {
425    
426                    return _ehcache.isReadLockedByCurrentThread(key);
427            }
428    
429            @Override
430            public boolean isSearchable() {
431                    return _ehcache.isSearchable();
432            }
433    
434            @Override
435            public boolean isValueInCache(Object value) {
436                    return _ehcache.isValueInCache(value);
437            }
438    
439            @Override
440            public boolean isWriteLockedByCurrentThread(Object key)
441                    throws UnsupportedOperationException {
442    
443                    return _ehcache.isWriteLockedByCurrentThread(key);
444            }
445    
446            @Override
447            public void load(Object key) throws CacheException {
448                    _ehcache.load(key);
449            }
450    
451            @Override
452            @SuppressWarnings("rawtypes")
453            public void loadAll(Collection keys, Object argument)
454                    throws CacheException {
455    
456                    _ehcache.loadAll(keys, argument);
457            }
458    
459            @Override
460            public void put(Element element)
461                    throws CacheException, IllegalArgumentException, IllegalStateException {
462    
463                    _ehcache.put(element);
464            }
465    
466            @Override
467            public void put(Element element, boolean doNotNotifyCacheReplicators)
468                    throws CacheException, IllegalArgumentException, IllegalStateException {
469    
470                    _ehcache.put(element, doNotNotifyCacheReplicators);
471            }
472    
473            @Override
474            public void putAll(Collection<Element> elements)
475                    throws CacheException, IllegalArgumentException, IllegalStateException {
476    
477                    _ehcache.putAll(elements);
478            }
479    
480            @Override
481            public Element putIfAbsent(Element element) throws NullPointerException {
482                    return _ehcache.putIfAbsent(element);
483            }
484    
485            @Override
486            public Element putIfAbsent(
487                            Element element, boolean doNotNotifyCacheReplicators)
488                    throws NullPointerException {
489    
490                    return _ehcache.putIfAbsent(element, doNotNotifyCacheReplicators);
491            }
492    
493            @Override
494            public void putQuiet(Element element)
495                    throws CacheException, IllegalArgumentException, IllegalStateException {
496    
497                    _ehcache.putQuiet(element);
498            }
499    
500            @Override
501            public void putWithWriter(Element element)
502                    throws CacheException, IllegalArgumentException, IllegalStateException {
503    
504                    _ehcache.putWithWriter(element);
505            }
506    
507            @Override
508            public void registerCacheExtension(CacheExtension cacheExtension) {
509    
510                    _ehcache.registerCacheExtension(cacheExtension);
511            }
512    
513            @Override
514            public void registerCacheLoader(CacheLoader cacheLoader) {
515                    _ehcache.registerCacheLoader(cacheLoader);
516            }
517    
518            @Override
519            public void registerCacheWriter(CacheWriter cacheWriter) {
520    
521                    _ehcache.registerCacheWriter(cacheWriter);
522            }
523    
524            @Override
525            public void registerDynamicAttributesExtractor(
526                    DynamicAttributesExtractor dynamicAttributesExtractor) {
527    
528                    _ehcache.registerDynamicAttributesExtractor(dynamicAttributesExtractor);
529            }
530    
531            @Override
532            public void releaseReadLockOnKey(Object key) {
533                    _ehcache.releaseReadLockOnKey(key);
534            }
535    
536            @Override
537            public void releaseWriteLockOnKey(Object key) {
538                    _ehcache.releaseWriteLockOnKey(key);
539            }
540    
541            @Override
542            public boolean remove(Object key) throws IllegalStateException {
543                    return _ehcache.remove(key);
544            }
545    
546            @Override
547            public boolean remove(Object key, boolean doNotNotifyCacheReplicators)
548                    throws IllegalStateException {
549    
550                    return _ehcache.remove(key, doNotNotifyCacheReplicators);
551            }
552    
553            @Override
554            public boolean remove(Serializable key) throws IllegalStateException {
555                    return _ehcache.remove(key);
556            }
557    
558            @Override
559            public boolean remove(Serializable key, boolean doNotNotifyCacheReplicators)
560                    throws IllegalStateException {
561    
562                    return _ehcache.remove(key, doNotNotifyCacheReplicators);
563            }
564    
565            @Override
566            public void removeAll() throws CacheException, IllegalStateException {
567                    if (!isStatusAlive()) {
568                            return;
569                    }
570    
571                    _ehcache.removeAll();
572            }
573    
574            @Override
575            public void removeAll(boolean doNotNotifyCacheReplicators)
576                    throws CacheException, IllegalStateException {
577    
578                    if (!isStatusAlive()) {
579                            return;
580                    }
581    
582                    _ehcache.removeAll(doNotNotifyCacheReplicators);
583            }
584    
585            @Override
586            public void removeAll(Collection<?> keys)
587                    throws IllegalStateException, NullPointerException {
588    
589                    _ehcache.removeAll(keys);
590            }
591    
592            @Override
593            public void removeAll(
594                            Collection<?> keys, boolean doNotNotifyCacheReplicators)
595                    throws IllegalStateException, NullPointerException {
596    
597                    _ehcache.removeAll(keys, doNotNotifyCacheReplicators);
598            }
599    
600            @Override
601            public boolean removeElement(Element element) throws NullPointerException {
602                    return _ehcache.removeElement(element);
603            }
604    
605            @Override
606            public void removePropertyChangeListener(
607                    PropertyChangeListener propertyChangeListener) {
608    
609                    _ehcache.removePropertyChangeListener(propertyChangeListener);
610            }
611    
612            @Override
613            public boolean removeQuiet(Object key) throws IllegalStateException {
614                    if (!isStatusAlive()) {
615                            return true;
616                    }
617    
618                    return _ehcache.removeQuiet(key);
619            }
620    
621            @Override
622            public boolean removeQuiet(Serializable key) throws IllegalStateException {
623                    if (!isStatusAlive()) {
624                            return true;
625                    }
626    
627                    return _ehcache.removeQuiet(key);
628            }
629    
630            public void removeReference() {
631                    _referenceCounter.decrementAndGet();
632            }
633    
634            @Override
635            public boolean removeWithWriter(Object key)
636                    throws CacheException, IllegalStateException {
637    
638                    if (!isStatusAlive()) {
639                            return true;
640                    }
641    
642                    return _ehcache.removeWithWriter(key);
643            }
644    
645            @Override
646            public Element replace(Element element) throws NullPointerException {
647                    return _ehcache.replace(element);
648            }
649    
650            @Override
651            public boolean replace(Element old, Element element)
652                    throws IllegalArgumentException, NullPointerException {
653    
654                    return _ehcache.replace(old, element);
655            }
656    
657            @Override
658            public void setBootstrapCacheLoader(
659                            BootstrapCacheLoader bootstrapCacheLoader)
660                    throws CacheException {
661    
662                    _ehcache.setBootstrapCacheLoader(bootstrapCacheLoader);
663            }
664    
665            @Override
666            public void setCacheExceptionHandler(
667                    CacheExceptionHandler cacheExceptionHandler) {
668    
669                    _ehcache.setCacheExceptionHandler(cacheExceptionHandler);
670            }
671    
672            @Override
673            public void setCacheManager(CacheManager cacheManager) {
674                    _ehcache.setCacheManager(cacheManager);
675            }
676    
677            @Override
678            public void setDisabled(boolean disabled) {
679                    _ehcache.setDisabled(disabled);
680            }
681    
682            @Override
683            public void setName(String name) {
684                    _ehcache.setName(name);
685            }
686    
687            @Override
688            public void setNodeBulkLoadEnabled(boolean enabledBulkLoad)
689                    throws TerracottaNotRunningException, UnsupportedOperationException {
690    
691                    _ehcache.setNodeBulkLoadEnabled(enabledBulkLoad);
692            }
693    
694            /**
695             * @deprecated As of 6.1.0, replaced by {@link
696             *             #setNodeBulkLoadEnabled(boolean)}
697             */
698            @Override
699            public void setNodeCoherent(boolean nodeCoherent)
700                    throws TerracottaNotRunningException, UnsupportedOperationException {
701    
702                    _ehcache.setNodeBulkLoadEnabled(nodeCoherent);
703            }
704    
705            @Override
706            public void setTransactionManagerLookup(
707                    TransactionManagerLookup transactionManagerLookup) {
708    
709                    _ehcache.setTransactionManagerLookup(transactionManagerLookup);
710            }
711    
712            public void setWrappedCache(Ehcache ehcache) {
713                    _ehcache = ehcache;
714            }
715    
716            @Override
717            public String toString() {
718                    return _ehcache.toString();
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 As of 6.1.0, replaced by {@link
759             *             #waitUntilClusterBulkLoadComplete}
760             */
761            @Override
762            public void waitUntilClusterCoherent()
763                    throws TerracottaNotRunningException, UnsupportedOperationException {
764    
765                    _ehcache.waitUntilClusterBulkLoadComplete();
766            }
767    
768            protected boolean isStatusAlive() {
769                    Status status = _ehcache.getStatus();
770    
771                    if (status.equals(Status.STATUS_ALIVE)) {
772                            return true;
773                    }
774                    else {
775                            return false;
776                    }
777            }
778    
779            private Ehcache _ehcache;
780            private AtomicInteger _referenceCounter = new AtomicInteger(0);
781    
782    }