KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > AopLRUPolicy


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  * Created on March 25 2003
7  */

8 package org.jboss.cache.eviction;
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.aop.AOPInstance;
11 import org.jboss.cache.aop.TreeCacheAop;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15
16
17 /** Provider to provide eviction policy. This one is based on LRU algorithm that a user
18  * can specify either maximum number of nodes or the idle time of a node to be evicted.
19  *
20  * @author Ben Wang 02-2004
21  */

22 public class AopLRUPolicy extends LRUPolicy implements AopEvictionPolicy
23 {
24    public AopLRUPolicy() {
25       super();
26    }
27
28    /**
29     * Override for aop-specific. In this case, if a node is visited and it is an aop-node, i.e.,
30     * it has AopInstance, then all the children nodes are considered visited as well.
31     * @param fqn
32     */

33    public void nodeVisited(Fqn fqn)
34    {
35       if(isInternalNode(fqn)) return;
36
37       Region region = regionManager_.getRegion(fqn.toString());
38       // TODO May need to optimize to check if this fqn is already on the queue. No need to process it twice.
39
region.setVisitedNode(fqn);
40
41       if(!isAopNode(fqn))
42          return;
43
44
45       visitChildrenRecursively(region, fqn);
46    }
47
48    protected void visitChildrenRecursively(Region region, Fqn fqn) {
49       Set JavaDoc set = getChildrenNames(fqn);
50       int size = (set == null) ? 0: set.size();
51       if(log_.isTraceEnabled()) {
52          log_.trace("nodeVisited(): is an aop node. fqn- " +fqn + " size of children is " +size);
53       }
54
55       if(set == null) return; // barren
56
Iterator JavaDoc it = set.iterator();
57       while(it.hasNext()) {
58          Object JavaDoc childName = it.next();
59          Fqn fqnKid = new Fqn(fqn, childName);
60          visitChildrenRecursively(region, fqnKid);
61          region.setVisitedNode(fqnKid);
62       }
63    }
64
65    private boolean isAopNode(Fqn fqn)
66    {
67       try {
68          return (cache_.peek(fqn, AOPInstance.KEY) != null) ? true: false;
69       } catch (Exception JavaDoc e) {
70          log_.warn("isAopNode(): cache get operation generated exception " +e);
71          return false;
72       }
73    }
74
75    public void nodeAdded(Fqn fqn)
76    {
77       if(isInternalNode(fqn)) return;
78
79       super.nodeAdded(fqn);
80    }
81
82    /**
83     * Note that this removes all children nodes as well. So we will need to put the children nodes
84     * into removed queue.
85     * @param fqn
86     */

87    public void nodeRemoved(Fqn fqn)
88    {
89       if(isInternalNode(fqn)) return;
90
91       super.nodeRemoved(fqn);
92    }
93
94    public void nodeModified(Fqn fqn) {
95       super.nodeModified(fqn);
96    }
97
98    // we are using the same eviction algorithm now.
99
protected EvictionAlgorithm getEvictionAlgorithm() {
100       return new LRUAlgorithm();
101    }
102
103    /**
104     * Check if this is an JBossInternal node. If it is, no need to proceed since we don't
105     * care about it.
106     * @param fqn
107     * @return
108     */

109    protected boolean isInternalNode(Fqn fqn) {
110       String JavaDoc startStr = (String JavaDoc)fqn.get(0);
111       if( startStr.equals(TreeCacheAop.JBOSS_INTERNAL.get(0))) return true;
112
113       return false;
114    }
115 }
116
Popular Tags