KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > PassivationInterceptor


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 JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.concurrent.atomic.AtomicLong JavaDoc;
13
14 /**
15  * Writes evicted nodes back to the store on the way in through the
16  * CacheLoader, either before each method call (no TXs), or at TX commit.
17  *
18  * @author <a HREF="mailto:{hmesha@novell.com}">{Hany Mesha}</a>
19  * @version $Id: PassivationInterceptor.java,v 1.39 2007/01/04 05:35:37 msurtani Exp $
20  */

21 public class PassivationInterceptor extends Interceptor implements PassivationInterceptorMBean
22 {
23
24    protected CacheLoader loader = null;
25    private AtomicLong JavaDoc m_passivations = new AtomicLong JavaDoc(0);
26
27    public synchronized void setCache(CacheSPI cache)
28    {
29       super.setCache(cache);
30       this.loader = cache.getCacheLoaderManager().getCacheLoader();
31    }
32
33    /**
34     * Notifies the cache instance listeners that the evicted node is about to
35     * be passivated and stores the evicted node and its attributes back to the
36     * store using the CacheLoader.
37     *
38     * @return
39     * @throws Throwable
40     */

41    public Object JavaDoc invoke(MethodCall m) throws Throwable JavaDoc
42    {
43       // hmesha- We don't need to handle transaction during passivation since
44
// passivation happens local to a node and never replicated
45

46       // evict() method need to be applied to the CacheLoader before passing the call on
47
if (m.getMethodId() == MethodDeclarations.evictNodeMethodLocal_id)
48       {
49          Object JavaDoc[] args = m.getArgs();
50          Fqn fqn = (Fqn) args[0];
51          try
52          {
53             synchronized (this)
54             {
55                // evict method local doesn't hold attributes therefore we have
56
// to get them manually
57
Map JavaDoc attributes = getNodeAttributes(fqn);
58                // notify listeners that this node is about to be passivated
59
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 JavaDoc<String JavaDoc, Object JavaDoc> dumpStatistics()
94    {
95       Map JavaDoc<String JavaDoc, Object JavaDoc> retval = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
96       retval.put("Passivations", m_passivations.get());
97       return retval;
98    }
99
100    /**
101     * Returns attributes for a node.
102     */

103    private Map JavaDoc 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 JavaDoc
122    {
123       /**
124        * The serialVersionUID
125        */

126       private static final long serialVersionUID = -4078972305344328905L;
127    }
128 }
129
Popular Tags