1 package org.jboss.cache.interceptors; 2 3 import org.jboss.cache.CacheSPI; 4 import org.jboss.cache.Fqn; 5 import org.jboss.cache.NodeSPI; 6 import org.jboss.cache.loader.CacheLoader; 7 import org.jboss.cache.marshall.MethodCall; 8 import org.jboss.cache.marshall.MethodDeclarations; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 import java.util.concurrent.atomic.AtomicLong ; 13 14 21 public class PassivationInterceptor extends Interceptor implements PassivationInterceptorMBean 22 { 23 24 protected CacheLoader loader = null; 25 private AtomicLong m_passivations = new AtomicLong (0); 26 27 public synchronized void setCache(CacheSPI cache) 28 { 29 super.setCache(cache); 30 this.loader = cache.getCacheLoaderManager().getCacheLoader(); 31 } 32 33 41 public Object invoke(MethodCall m) throws Throwable 42 { 43 46 if (m.getMethodId() == MethodDeclarations.evictNodeMethodLocal_id) 48 { 49 Object [] args = m.getArgs(); 50 Fqn fqn = (Fqn) args[0]; 51 try 52 { 53 synchronized (this) 54 { 55 Map attributes = getNodeAttributes(fqn); 58 cache.getNotifier().notifyNodePassivated(fqn, true, true); 60 61 loader.put(fqn, attributes); 62 63 cache.getNotifier().notifyNodePassivated(fqn, false, true); 64 } 65 66 if (getStatisticsEnabled() && configuration.getExposeManagementStatistics()) 67 { 68 m_passivations.getAndIncrement(); 69 } 70 } 71 catch (NodeNotLoadedException e) 72 { 73 if (log.isTraceEnabled()) 74 { 75 log.trace("Node " + fqn + " not loaded in memory; passivation skipped"); 76 } 77 } 78 } 79 80 return super.invoke(m); 81 } 82 83 public long getPassivations() 84 { 85 return m_passivations.get(); 86 } 87 88 public void resetStatistics() 89 { 90 m_passivations.set(0); 91 } 92 93 public Map <String , Object > dumpStatistics() 94 { 95 Map <String , Object > retval = new HashMap <String , Object >(); 96 retval.put("Passivations", m_passivations.get()); 97 return retval; 98 } 99 100 103 private Map getNodeAttributes(Fqn fqn) throws NodeNotLoadedException 104 { 105 if (fqn == null) 106 { 107 throw new NodeNotLoadedException(); 108 } 109 NodeSPI n = cache.peek(fqn); 110 111 if (n != null) 112 { 113 return n.getDataDirect(); 114 } 115 else 116 { 117 throw new NodeNotLoadedException(); 118 } 119 } 120 121 private static class NodeNotLoadedException extends Exception 122 { 123 126 private static final long serialVersionUID = -4078972305344328905L; 127 } 128 } 129 | Popular Tags |