KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > invalidation > triggers > EntityBeanCacheBatchInvalidatorInterceptor


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.cache.invalidation.triggers;
23
24 import org.jboss.metadata.XmlLoadable;
25 import org.jboss.metadata.MetaData;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * The role of this interceptor is to detect that an entity has been modified
30  * after an invocation has been performed an use the InvalidationsTxGrouper
31  * static class so that invalidation messages can be groupped and
32  * sent at transaction-commit time in a single batch.
33  *
34  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
35  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
36  * @version $Revision: 37459 $
37  */

38 public class EntityBeanCacheBatchInvalidatorInterceptor
39    extends org.jboss.ejb.plugins.AbstractInterceptor
40    implements XmlLoadable
41 {
42    protected boolean doCacheInvalidations = true;
43    protected org.jboss.cache.invalidation.InvalidationManagerMBean invalMgr = null;
44    protected org.jboss.cache.invalidation.InvalidationGroup ig = null;
45    protected org.jboss.ejb.EntityContainer container = null;
46    public boolean invalidateRelated;
47
48    public void start() throws Exception JavaDoc
49    {
50       org.jboss.metadata.EntityMetaData emd = ((org.jboss.metadata.EntityMetaData)this.getContainer ().getBeanMetaData ());
51       doCacheInvalidations = emd.doDistributedCacheInvalidations ();
52       
53       if (doCacheInvalidations)
54       {
55          // we are interested in receiving cache invalidation callbacks
56
//
57
String JavaDoc groupName = emd.getDistributedCacheInvalidationConfig ().getInvalidationGroupName ();
58          String JavaDoc imName = emd.getDistributedCacheInvalidationConfig ().getInvalidationManagerName ();
59          
60          this.invalMgr = (org.jboss.cache.invalidation.InvalidationManagerMBean)org.jboss.system.Registry.lookup (imName);
61          this.ig = this.invalMgr.getInvalidationGroup (groupName);
62       }
63    }
64
65    public void stop()
66    {
67       this.invalMgr = null;
68       // ig can be null if cache-invalidation is false
69
if(ig != null)
70       {
71          this.ig.removeReference (); // decrease the usage counter
72
this.ig = null;
73       }
74    }
75  
76    // Interceptor implementation --------------------------------------
77

78    protected boolean changed (org.jboss.invocation.Invocation mi, org.jboss.ejb.EntityEnterpriseContext ctx) throws Exception JavaDoc
79    {
80       if (ctx.getId() == null) return true;
81
82       if(!container.isReadOnly())
83       {
84          java.lang.reflect.Method JavaDoc method = mi.getMethod();
85             if(method == null ||
86                !container.getBeanMetaData().isMethodReadOnly(method.getName()))
87             {
88                return invalidateRelated ?
89                   container.getPersistenceManager().isModified(ctx) :
90                   container.getPersistenceManager().isStoreRequired(ctx);
91             }
92       }
93       return false;
94    }
95
96    public Object JavaDoc invoke(org.jboss.invocation.Invocation mi) throws Exception JavaDoc
97    {
98       if (doCacheInvalidations)
99       {
100          // We are going to work with the context a lot
101
org.jboss.ejb.EntityEnterpriseContext ctx = (org.jboss.ejb.EntityEnterpriseContext)mi.getEnterpriseContext();
102          Object JavaDoc id = ctx.getId();
103
104          // The Tx coming as part of the Method Invocation
105
javax.transaction.Transaction JavaDoc tx = mi.getTransaction();
106
107          // Invocation with a running Transaction
108
if (tx != null && tx.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION)
109          {
110             //Invoke down the chain
111
Object JavaDoc retVal = getNext().invoke(mi);
112
113             if (changed(mi, ctx))
114             {
115                org.jboss.cache.invalidation.InvalidationsTxGrouper.registerInvalidationSynchronization (tx, this.ig, (java.io.Serializable JavaDoc)id);
116             }
117
118             // return the return value
119
return retVal;
120          }
121          //
122
else
123          { // No tx
124
Object JavaDoc result = getNext().invoke(mi);
125
126             if (changed(mi, ctx))
127             {
128                org.jboss.cache.invalidation.InvalidationsTxGrouper.registerInvalidationSynchronization (tx, this.ig, (java.io.Serializable JavaDoc)id);
129             }
130             return result;
131          }
132       }
133       else
134       {
135          return getNext().invoke(mi);
136       }
137    }
138
139    public void setContainer(org.jboss.ejb.Container container) { this.container = (org.jboss.ejb.EntityContainer)container; }
140
141    public org.jboss.ejb.Container getContainer() { return container; }
142
143    // XmlLoadable implementation --------------------------------------------------------
144

145    public void importXml(Element JavaDoc element) throws Exception JavaDoc
146    {
147       String JavaDoc str = MetaData.getElementAttribute(element, "invalidate-related");
148       invalidateRelated = (str == null ? true : Boolean.valueOf(str).booleanValue());
149       if(log.isTraceEnabled())
150       {
151          log.trace("invalidate-related: " + invalidateRelated);
152       }
153    }
154 }
155
Popular Tags