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