001: package com.completex.objective.components.ocache.impl;
002:
003: import com.completex.objective.components.ocache.OdalCache;
004: import com.completex.objective.components.ocache.OdalCacheWrapper;
005: import com.completex.objective.components.ocache.OdalKeyFactory;
006: import com.completex.objective.components.ocache.OdalKeyedCache;
007: import com.completex.objective.components.ocache.OdalRuntimeCacheException;
008: import com.completex.objective.components.ocache.OdalTransactionSensitiveCache;
009: import com.completex.objective.components.persistency.transact.TransactionListener;
010:
011: /**
012: * Cache implementation that is combined with TransactionListener implementation.
013: * Entries get removed and invalidated on removal from the cache and redundantly removed on
014: * transaction commit or rollback prividing for distributed cache consistency.
015: * Cache has to be registered with current transaction.
016: *
017: * @see com.completex.objective.components.persistency.transact.Transaction#addListerner(Object, com.completex.objective.components.persistency.transact.TransactionListener)
018: * @author Gennady Krizhevsky
019: */
020: public class TransactionSensitiveCacheImpl extends
021: CacheTransactionListenerImpl implements
022: OdalTransactionSensitiveCache, OdalKeyedCache, OdalCacheWrapper {
023:
024: private OdalKeyFactory odalKeyFactory = OdalKeyFactory.NULL_ODAL_KEY_FACTORY;
025: private boolean markCacheCollectionElements;
026:
027: public TransactionSensitiveCacheImpl(OdalCache coreCache,
028: OdalKeyFactory odalKeyFactory,
029: boolean markCacheCollectionElements) {
030: super (coreCache);
031: setOdalKeyFactory(odalKeyFactory);
032: setMarkCacheCollectionElements(markCacheCollectionElements);
033: }
034:
035: /**
036: * @see OdalCache#get(Object)
037: */
038: public Object get(Object key) {
039: return getCache().get(key);
040: }
041:
042: /**
043: * @see OdalCache#put(Object, Object)
044: */
045: public Object put(Object key, Object value) {
046: return getCache().put(key, value);
047: }
048:
049: /**
050: * @see com.completex.objective.components.ocache.OdalLinkedCache#put(Object)
051: */
052: public void put(Object value) {
053: OdalKeyFactory odalKeyFactory = getKeyFactory();
054: if (odalKeyFactory == null) {
055: throw new OdalRuntimeCacheException(
056: "OdalKeyFactory is null for OdalKeyedCache");
057: }
058:
059: put(odalKeyFactory.key(value), value);
060: }
061:
062: /**
063: * @see OdalCache#remove(Object)
064: */
065: public Object remove(Object key) {
066: invalidateEntry(key);
067: // return getCache().remove(key);
068: return getCache().get(key);
069: }
070:
071: /**
072: * @see com.completex.objective.components.ocache.OdalCache#clear()
073: */
074: public void clear() {
075: clearInvalid();
076: getCache().clear();
077: }
078:
079: /**
080: * @see com.completex.objective.components.ocache.OdalKeyedCache#getKeyFactory()
081: */
082: public OdalKeyFactory getKeyFactory() {
083: return odalKeyFactory;
084: }
085:
086: public void setOdalKeyFactory(OdalKeyFactory odalKeyFactory) {
087: if (odalKeyFactory != null) {
088: this .odalKeyFactory = odalKeyFactory;
089: }
090: }
091:
092: public boolean isMarkCacheCollectionElements() {
093: return markCacheCollectionElements;
094: }
095:
096: public void setMarkCacheCollectionElements(
097: boolean markCacheCollectionElements) {
098: this .markCacheCollectionElements = markCacheCollectionElements;
099: }
100:
101: /**
102: * @see com.completex.objective.components.ocache.OdalCacheWrapper#getCoreCache()
103: */
104: public OdalCache getCoreCache() {
105: return getCache();
106: }
107:
108: /**
109: * Returns odalCache if it is instance of TransactionListener or othewise recurcively goes through OdalCacheWrapper instances (if applicable) and returns the delegate
110: * if it is instance of TransactionListener
111: *
112: * @param odalCache cache to probe
113: * @return odalCache if it is instance of TransactionListener or othewise recurcively goes through OdalCacheWrapper instances (if applicable) and returns the delegate
114: * if it is instance of TransactionListener
115: */
116: public static TransactionListener extractTransactionListener(
117: OdalCache odalCache) {
118: if (odalCache == null) {
119: return null;
120: }
121:
122: if (odalCache instanceof TransactionListener) {
123: return (TransactionListener) odalCache;
124: } else if (odalCache instanceof OdalCacheWrapper) {
125: OdalCacheWrapper wrapper = (OdalCacheWrapper) odalCache;
126: return extractTransactionListener(wrapper.getCoreCache());
127: } else {
128: return null;
129: }
130: }
131:
132: }
|