KickJava   Java API By Example, From Geeks To Geeks.

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


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
10 import org.jboss.cache.TreeCache;
11 import org.jboss.logging.Logger;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Factory to create region from configuration, to track region,
18  * and to resolve naming conflict for regions. Note that in addition to
19  * user-specified regions, there is also a global cache <code>_default_</code>
20  * region that covers everything else.
21  *
22  * @author Ben Wang 02-2004
23  * @version $Id: RegionManager.java,v 1.5.2.2 2005/04/04 05:44:17 bwang00 Exp $
24  */

25 public class RegionManager
26 {
27    private Logger log_ = Logger.getLogger(RegionManager.class);
28    public final static int CAPACITY = 200000;
29    private Map JavaDoc regionMap_ = new HashMap JavaDoc();
30    // optimization
31
private Region[] regions_;
32    private EvictionPolicy policy_;
33    // There is global cache wide default values if no region is found.
34
final static String JavaDoc DEFAULT_REGION = "/_default_/";
35
36    public RegionManager(EvictionPolicy policy)
37    {
38       policy_ = policy;
39       regions_ = null;
40    }
41
42    /**
43     * Create a region based on fqn.
44     *
45     * @param fqn The region identifier.
46     * @param algorithm EvictionAlgorithm that associates with this region.
47     * @throws RegionNameConflictException
48     */

49    public Region createRegion(String JavaDoc fqn, EvictionAlgorithm algorithm)
50    throws RegionNameConflictException
51    {
52       if (log_.isDebugEnabled())
53       {
54          log_.debug("createRegion(): creating region for fqn- " + fqn);
55       }
56
57       String JavaDoc newFqn = appendFqn(fqn);
58       checkConflict(newFqn);
59       Region region = new Region(newFqn, policy_, algorithm);
60       regionMap_.put(newFqn, region);
61       return region;
62    }
63
64    public void removeRegion(String JavaDoc fqn)
65    {
66       regionMap_.remove(fqn);
67    }
68
69    /**
70     * Append the fqn with "/" if necessary
71     *
72     * @param fqn
73     * @return
74     */

75    private String JavaDoc appendFqn(String JavaDoc fqn)
76    {
77       if (!fqn.endsWith(TreeCache.SEPARATOR))
78          return fqn + TreeCache.SEPARATOR;
79       else
80          return fqn;
81    }
82
83    public boolean hasRegion(String JavaDoc myFqn)
84    {
85       String JavaDoc newFqn = appendFqn(myFqn);
86       return regionMap_.containsKey(newFqn);
87    }
88
89    public Region getRegion(String JavaDoc myFqn)
90    {
91       Region[] regions = getRegions();
92       // TODO. Is not needed if fqn.toString is appended with SEPARATOR.
93
String JavaDoc myRFqn = appendFqn(myFqn);
94       // TODO need further optimization in regex matching
95

96       // Do it in reverse order such that children fqn gets matched first.
97
for (int i = (regions.length - 1); i >= 0; i--)
98       {
99          String JavaDoc fqn = regions[i].getFqn();
100          if (myRFqn.startsWith(fqn)) return regions[i];
101       }
102
103       if (log_.isTraceEnabled())
104       {
105          log_.trace("getRegion(): not user-specified region found for this fqn- " + myFqn
106          + " will use the global default region");
107       }
108       return (Region) regionMap_.get(DEFAULT_REGION);
109    }
110
111    public Region[] getRegions()
112    {
113       // optimization
114
if (regions_ != null && regions_.length == regionMap_.size())
115          return regions_;
116
117       Object JavaDoc[] objs = regionMap_.values().toArray();
118       Region[] regions = new Region[objs.length];
119       for (int i = 0; i < objs.length; i++)
120       {
121          regions[i] = (Region) objs[i];
122       }
123
124       if (log_.isDebugEnabled())
125       {
126          log_.debug("getRegions(): size of region " + regions.length);
127       }
128
129       regions_ = regions;
130       return regions;
131    }
132
133    /**
134     * Check for conflict in the current regions. There is a conflict
135     * <p/>
136     * if fqn is any parent fqn of the current regions.
137     *
138     * @param myFqn Current fqn for potential new region.
139     * @throws RegionNameConflictException to indicate a region name conflict has ocurred.
140     */

141    public void checkConflict(String JavaDoc myFqn) throws RegionNameConflictException
142    {
143       // Step x. Loop thru the region map and compare the fqn. Order is important.
144
// Not very efficient if there is a lot of regions.
145
// First comer wins. E.g., /a/b/c and then /a/b/c will have /a/b in different
146
// region than /a/b. But if /a/b and /a/b/c, then we will /a/b/c will be
147
// the same region as /a/b. That is, we check if a fqn is within a region
148
// by looping thru regions and then check if my fqn is a child of region
149
// fqn. It it is, viola, and take this and exit.
150
Region[] regions = getRegions();
151       for (int i = 0; i < regions.length; i++)
152       {
153          String JavaDoc fqn = regions[i].getFqn();
154          if (myFqn.equals(fqn) || myFqn.startsWith(fqn))
155          { // fqn is a child of myFqn.
156
throw new RegionNameConflictException("RegionManager.checkConflict(): new region fqn "
157             + myFqn + " is in conflict with current region fqn- " + fqn);
158          }
159       }
160       // We are clear then.
161
}
162 }
163
Popular Tags