001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2.schema;
023:
024: import org.jboss.cache.invalidation.Invalidatable;
025: import org.jboss.cache.invalidation.InvalidationGroup;
026: import org.jboss.logging.Logger;
027:
028: import javax.transaction.TransactionManager;
029: import javax.transaction.Transaction;
030: import javax.transaction.SystemException;
031: import java.io.Serializable;
032:
033: /**
034: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
035: * @version <tt>$Revision: 57209 $</tt>
036: */
037: public class CacheInvalidator implements Invalidatable {
038: private static final Logger log = Logger
039: .getLogger(CacheInvalidator.class);
040:
041: private final Cache cache;
042: private final TransactionManager tm;
043: private final InvalidationGroup group;
044:
045: public CacheInvalidator(Cache cache, TransactionManager tm,
046: InvalidationGroup group) {
047: this .cache = cache;
048: this .tm = tm;
049: this .group = group;
050: group.register(this );
051: log.debug("registered to group " + group.getGroupName());
052: }
053:
054: public void unregister() {
055: group.unregister(this );
056: log.debug("unregistered from group " + group.getGroupName());
057: }
058:
059: public void isInvalid(Serializable key) {
060: Transaction tx = null;
061: try {
062: tx = tm.getTransaction();
063: } catch (SystemException e) {
064: log.error("Failed to obtain the current transaction", e);
065: throw new IllegalStateException(
066: "Failed to obtain the current transaction: "
067: + e.getMessage());
068: }
069:
070: if (log.isTraceEnabled()) {
071: log.trace("invalidating key=" + key);
072: }
073:
074: cache.lock(key);
075: try {
076: cache.remove(tx, key);
077: } catch (Cache.RemoveException e) {
078: if (log.isTraceEnabled()) {
079: log.trace(e.getMessage());
080: }
081: } finally {
082: cache.unlock(key);
083: }
084: }
085:
086: public void areInvalid(Serializable[] keys) {
087: Transaction tx = null;
088: try {
089: tx = tm.getTransaction();
090: } catch (SystemException e) {
091: log.error("Failed to obtain the current transaction", e);
092: throw new IllegalStateException(
093: "Failed to obtain the current transaction: "
094: + e.getMessage());
095: }
096:
097: boolean trace = log.isTraceEnabled();
098: for (int i = 0; i < keys.length; ++i) {
099: if (trace) {
100: log.trace("invalidating key[" + i + "]=" + keys[i]);
101: }
102:
103: cache.lock();
104: try {
105: cache.remove(tx, keys[i]);
106: } catch (Cache.RemoveException e) {
107: if (trace) {
108: log.trace(e.getMessage());
109: }
110: } finally {
111: cache.unlock();
112: }
113: }
114: }
115:
116: public void invalidateAll() {
117: cache.lock();
118: try {
119: cache.flush();
120: } finally {
121: cache.unlock();
122: }
123: }
124: }
|