KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > schema > CacheInvalidator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc2.schema;
23
24 import org.jboss.cache.invalidation.Invalidatable;
25 import org.jboss.cache.invalidation.InvalidationGroup;
26 import org.jboss.logging.Logger;
27
28 import javax.transaction.TransactionManager JavaDoc;
29 import javax.transaction.Transaction JavaDoc;
30 import javax.transaction.SystemException JavaDoc;
31 import java.io.Serializable JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
35  * @version <tt>$Revision: 37459 $</tt>
36  */

37 public class CacheInvalidator
38    implements Invalidatable
39 {
40    private static final Logger log = Logger.getLogger(CacheInvalidator.class);
41
42    private final Cache cache;
43    private final TransactionManager JavaDoc tm;
44    private final InvalidationGroup group;
45
46    public CacheInvalidator(Cache cache, TransactionManager JavaDoc tm, InvalidationGroup group)
47    {
48       this.cache = cache;
49       this.tm = tm;
50       this.group = group;
51       group.register(this);
52       log.debug("registered to group " + group.getGroupName());
53    }
54
55    public void unregister()
56    {
57       group.unregister(this);
58       log.debug("unregistered from group " + group.getGroupName());
59    }
60
61    public void isInvalid(Serializable JavaDoc key)
62    {
63       Transaction JavaDoc tx = null;
64       try
65       {
66          tx = tm.getTransaction();
67       }
68       catch(SystemException JavaDoc e)
69       {
70          log.error("Failed to obtain the current transaction", e);
71          throw new IllegalStateException JavaDoc("Failed to obtain the current transaction: " + e.getMessage());
72       }
73
74       if(log.isTraceEnabled())
75       {
76          log.trace("invalidating key=" + key);
77       }
78
79       cache.lock(key);
80       try
81       {
82          cache.remove(tx, key);
83       }
84       catch(Cache.RemoveException e)
85       {
86          if(log.isTraceEnabled())
87          {
88             log.trace(e.getMessage());
89          }
90       }
91       finally
92       {
93          cache.unlock(key);
94       }
95    }
96
97    public void areInvalid(Serializable JavaDoc[] keys)
98    {
99       Transaction JavaDoc tx = null;
100       try
101       {
102          tx = tm.getTransaction();
103       }
104       catch(SystemException JavaDoc e)
105       {
106          log.error("Failed to obtain the current transaction", e);
107          throw new IllegalStateException JavaDoc("Failed to obtain the current transaction: " + e.getMessage());
108       }
109
110       boolean trace = log.isTraceEnabled();
111       for(int i = 0; i < keys.length; ++i)
112       {
113          if(trace)
114          {
115             log.trace("invalidating key[" + i + "]=" + keys[i]);
116          }
117
118          cache.lock();
119          try
120          {
121             cache.remove(tx, keys[i]);
122          }
123          catch(Cache.RemoveException e)
124          {
125             if(trace)
126             {
127                log.trace(e.getMessage());
128             }
129          }
130          finally
131          {
132             cache.unlock();
133          }
134       }
135    }
136
137    public void invalidateAll()
138    {
139       cache.lock();
140       try
141       {
142          cache.flush();
143       }
144       finally
145       {
146          cache.unlock();
147       }
148    }
149 }
150
Popular Tags