001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.ocache.impl;
010:
011: import com.completex.objective.components.ocache.OdalCache;
012: import com.completex.objective.components.persistency.transact.TransactionListener;
013:
014: import java.util.HashSet;
015: import java.util.Iterator;
016:
017: /**
018: * Implementation of TransactionListener interface manipulating with cache.
019: * One can invalidate cache etries with invalidateEntry method.
020: * All the invalid values will be removed from the cache after commit or rollback.
021: * This is important for distributed caches were the same values can be inserted
022: * independently into different servers.
023: *
024: * @author Gennady Krizhevsky
025: */
026: public class CacheTransactionListenerImpl implements
027: TransactionListener {
028:
029: private OdalCache cache;
030: private final HashSet invalidKeys = new HashSet();
031:
032: /**
033: *
034: * @param cache underlying cache
035: */
036: public CacheTransactionListenerImpl(OdalCache cache) {
037: this .cache = cache;
038: }
039:
040: /**
041: * Returns underlying cache
042: *
043: * @return underlying cache
044: */
045: protected OdalCache getCache() {
046: return cache;
047: }
048:
049: /**
050: * Schedules the value for
051: * removal after commit or rollback. The last one is important for distributed ccahces
052: * to trigger the remote removal
053: *
054: * @param key key
055: */
056: public void invalidateEntry(Object key) {
057: invalidKeys.add(key);
058: }
059:
060: /**
061: * @see com.completex.objective.components.persistency.transact.TransactionListener#afterCommit()
062: */
063: public void afterCommit() {
064: removeInvalid();
065: clearInvalid();
066: }
067:
068: /**
069: * @see com.completex.objective.components.persistency.transact.TransactionListener#afterRollback()
070: */
071: public void afterRollback() {
072: clearInvalid();
073: }
074:
075: /**
076: * Clears all the values scheduled with #invalidateEntry(Object) method.
077: */
078: protected void clearInvalid() {
079: invalidKeys.clear();
080: }
081:
082: /**
083: * Removes all the values scheduled with #invalidateEntry(Object) method from the cache.
084: */
085: private void removeInvalid() {
086: for (Iterator iterator = invalidKeys.iterator(); iterator
087: .hasNext();) {
088: Object key = iterator.next();
089: cache.remove(key);
090: }
091: }
092:
093: /**
094: * Returns underlying cache name
095: *
096: * @return underlying cache name
097: */
098: public String getName() {
099: return cache.getName();
100: }
101:
102: }
|