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