2626import java .util .Set ;
2727import java .util .SortedSet ;
2828import java .util .TreeSet ;
29- import java .util .concurrent .ConcurrentHashMap ;
3029
3130import org .apache .commons .logging .Log ;
3231import org .apache .commons .logging .LogFactory ;
3332import org .codehaus .groovy .grails .commons .GrailsControllerClass ;
3433import org .codehaus .groovy .grails .validation .ConstrainedProperty ;
3534import org .springframework .core .style .ToStringCreator ;
3635
36+ import com .googlecode .concurrentlinkedhashmap .ConcurrentLinkedHashMap ;
37+
3738/**
3839 * Default implementation of the UrlMappingsHolder interface that takes a list of mappings and
39- * then sorts them according to their precdence rules as defined in the implementation of Comparable.
40+ * then sorts them according to their precedence rules as defined in the implementation of Comparable.
4041 *
4142 * @see org.codehaus.groovy.grails.web.mapping.UrlMapping
4243 * @see Comparable
4849public class DefaultUrlMappingsHolder implements UrlMappingsHolder {
4950
5051 private static final transient Log LOG = LogFactory .getLog (DefaultUrlMappingsHolder .class );
52+ private static final int DEFAULT_MAX_WEIGHTED_CAPACITY = 5000 ;
5153
52- private Map <String , UrlMappingInfo > cachedMatches = new ConcurrentHashMap <String , UrlMappingInfo >();
53- private Map <String , List <UrlMappingInfo >> cachedListMatches = new ConcurrentHashMap <String , List <UrlMappingInfo >>();
54+ private int maxWeightedCacheCapacity ;
55+ private Map <String , UrlMappingInfo > cachedMatches ;
56+ private Map <String , List <UrlMappingInfo >> cachedListMatches ;
5457
5558 private List <UrlMapping > urlMappings = new ArrayList <UrlMapping >();
5659 private UrlMapping [] mappings ;
@@ -68,18 +71,34 @@ public class DefaultUrlMappingsHolder implements UrlMappingsHolder {
6871
6972 public DefaultUrlMappingsHolder (List <UrlMapping > mappings ) {
7073 urlMappings = mappings ;
74+ this .maxWeightedCacheCapacity = DEFAULT_MAX_WEIGHTED_CAPACITY ;
7175 initialize ();
7276 }
7377
7478 public DefaultUrlMappingsHolder (List <UrlMapping > mappings , List excludePatterns ) {
7579 urlMappings = mappings ;
7680 this .excludePatterns = excludePatterns ;
81+ this .maxWeightedCacheCapacity = DEFAULT_MAX_WEIGHTED_CAPACITY ;
7782 initialize ();
7883 }
79-
84+
85+ public DefaultUrlMappingsHolder (List <UrlMapping > mappings , List excludePatterns , int maxWeightedUrlCacheSize ){
86+ urlMappings = mappings ;
87+ this .excludePatterns = excludePatterns ;
88+ this .maxWeightedCacheCapacity = maxWeightedUrlCacheSize ;
89+ initialize ();
90+ }
91+
8092 private void initialize () {
8193 sortMappings ();
8294
95+ cachedMatches = new ConcurrentLinkedHashMap .Builder <String , UrlMappingInfo >()
96+ .maximumWeightedCapacity (maxWeightedCacheCapacity )
97+ .build ();
98+ cachedListMatches = new ConcurrentLinkedHashMap .Builder <String , List <UrlMappingInfo >>()
99+ .maximumWeightedCapacity (maxWeightedCacheCapacity )
100+ .build ();
101+
83102 mappings = urlMappings .toArray (new UrlMapping [urlMappings .size ()]);
84103
85104 for (UrlMapping mapping : mappings ) {
@@ -346,12 +365,13 @@ public String toString() {
346365 /**
347366 * A class used as a key to lookup a UrlMapping based on controller, action and parameter names
348367 */
368+ @ SuppressWarnings ("unchecked" )
349369 class UrlMappingKey implements Comparable {
350370 String controller ;
351371 String action ;
352- Set paramNames = Collections .EMPTY_SET ;
372+ Set < String > paramNames = Collections .EMPTY_SET ;
353373
354- public UrlMappingKey (String controller , String action , Set paramNames ) {
374+ public UrlMappingKey (String controller , String action , Set < String > paramNames ) {
355375 this .controller = controller ;
356376 this .action = action ;
357377 this .paramNames = paramNames ;
0 commit comments