1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.application;
20
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.faces.application.ProjectStage;
24 import javax.faces.context.FacesContext;
25 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
26 import org.apache.myfaces.shared.util.ConcurrentLRUCache;
27 import org.apache.myfaces.shared.util.WebConfigParamUtils;
28
29 public class CheckedViewIdsCache
30 {
31 private static final Logger LOG = Logger.getLogger(CheckedViewIdsCache.class.getName());
32 private static final String INSTANCE_KEY = CheckedViewIdsCache.class.getName();
33
34
35
36
37 @JSFWebConfigParam(defaultValue = "500", since = "2.0.2", group="viewhandler", tags="performance",
38 classType="java.lang.Integer",
39 desc="Controls the size of the cache used to 'remember' if a view exists or not.")
40 private static final String CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE";
41
42
43
44
45
46 @JSFWebConfigParam(defaultValue = "true", since = "2.0.2", expectedValues="true, false", group="viewhandler",
47 tags="performance",
48 desc="Enable or disable a cache used to 'remember' if a view exists or not and reduce the impact " +
49 "of sucesive calls to ExternalContext.getResource().")
50 private static final String CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE =
51 "org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED";
52
53 private volatile ConcurrentLRUCache<String, Boolean> cache = null;
54 private boolean enabled;
55 private int size;
56
57 private CheckedViewIdsCache()
58 {
59 }
60
61 public void init(FacesContext facesContext)
62 {
63
64 if (facesContext.isProjectStage(ProjectStage.Development))
65 {
66 enabled = false;
67 }
68 else
69 {
70
71 enabled = WebConfigParamUtils.getBooleanInitParameter(facesContext.getExternalContext(),
72 CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE,
73 true);
74 }
75
76 size = WebConfigParamUtils.getIntegerInitParameter(facesContext.getExternalContext(),
77 CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE,
78 500);
79
80 cache = new ConcurrentLRUCache<>((size * 4 + 3) / 3, size);
81
82 if (LOG.isLoggable(Level.FINE))
83 {
84 LOG.log(Level.FINE, "MyFaces CheckedViewIdsCache enabled=" + enabled + ", size=" + size);
85 }
86 }
87
88 public boolean isEnabled()
89 {
90 return enabled;
91 }
92
93 public ConcurrentLRUCache<String, Boolean> getCache()
94 {
95 return cache;
96 }
97
98 public static CheckedViewIdsCache getInstance(FacesContext facesContext)
99 {
100 CheckedViewIdsCache instance = (CheckedViewIdsCache)
101 facesContext.getExternalContext().getApplicationMap().get(INSTANCE_KEY);
102 if (instance == null)
103 {
104 instance = new CheckedViewIdsCache();
105 instance.init(facesContext);
106 facesContext.getExternalContext().getApplicationMap().put(INSTANCE_KEY, instance);
107 }
108
109 return instance;
110 }
111 }