View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Controls the size of the cache used to "remember" if a view exists or not.
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       * Enable or disable a cache used to "remember" if a view exists or not and reduce the impact of
44       * sucesive calls to ExternalContext.getResource().
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          // first, check if the ProjectStage is development and skip caching in this case
64          if (facesContext.isProjectStage(ProjectStage.Development))
65          {
66              enabled = false;
67          }
68          else
69          {
70              // in all ohter cases, make sure that the cache is not explicitly disabled via context param
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 }