1 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 ; 27 28 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 49 { 50 org.jboss.metadata.EntityMetaData emd = ((org.jboss.metadata.EntityMetaData)this.getContainer ().getBeanMetaData ()); 51 doCacheInvalidations = emd.doDistributedCacheInvalidations (); 52 53 if (doCacheInvalidations) 54 { 55 String groupName = emd.getDistributedCacheInvalidationConfig ().getInvalidationGroupName (); 58 String 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 if(ig != null) 70 { 71 this.ig.removeReference (); this.ig = null; 73 } 74 } 75 76 78 protected boolean changed (org.jboss.invocation.Invocation mi, org.jboss.ejb.EntityEnterpriseContext ctx) throws Exception 79 { 80 if (ctx.getId() == null) return true; 81 82 if(!container.isReadOnly()) 83 { 84 java.lang.reflect.Method 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 invoke(org.jboss.invocation.Invocation mi) throws Exception 97 { 98 if (doCacheInvalidations) 99 { 100 org.jboss.ejb.EntityEnterpriseContext ctx = (org.jboss.ejb.EntityEnterpriseContext)mi.getEnterpriseContext(); 102 Object id = ctx.getId(); 103 104 javax.transaction.Transaction tx = mi.getTransaction(); 106 107 if (tx != null && tx.getStatus() != javax.transaction.Status.STATUS_NO_TRANSACTION) 109 { 110 Object retVal = getNext().invoke(mi); 112 113 if (changed(mi, ctx)) 114 { 115 org.jboss.cache.invalidation.InvalidationsTxGrouper.registerInvalidationSynchronization (tx, this.ig, (java.io.Serializable )id); 116 } 117 118 return retVal; 120 } 121 else 123 { Object result = getNext().invoke(mi); 125 126 if (changed(mi, ctx)) 127 { 128 org.jboss.cache.invalidation.InvalidationsTxGrouper.registerInvalidationSynchronization (tx, this.ig, (java.io.Serializable )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 145 public void importXml(Element element) throws Exception 146 { 147 String 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 |