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.view.facelets.pool.impl;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import org.apache.myfaces.config.RuntimeConfig;
28  import org.apache.myfaces.config.element.ViewPoolMapping;
29  import org.apache.myfaces.config.element.ViewPoolParameter;
30  import org.apache.myfaces.shared.util.ViewProtectionUtils;
31  import org.apache.myfaces.view.facelets.ViewPoolProcessor;
32  import org.apache.myfaces.view.facelets.pool.ViewPool;
33  import org.apache.myfaces.view.facelets.pool.ViewPoolFactory;
34  
35  /**
36   *
37   */
38  public class ViewPoolFactoryImpl extends ViewPoolFactory
39  {
40      private List<String> urlPatterns;
41      private List<ViewPool> viewPoolList;
42      private ViewPool defaultViewPool;
43      
44      public ViewPoolFactoryImpl(FacesContext context)
45      {
46          RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(context.getExternalContext());
47          // If no view pool mappings set, apply to all views.
48          if (runtimeConfig.getViewPoolMappings().isEmpty())
49          {
50              defaultViewPool = new ViewPoolImpl(context, new HashMap<String, String>());
51          }
52          urlPatterns = new ArrayList<String>();
53          viewPoolList = new ArrayList<ViewPool>();
54          for (ViewPoolMapping vpm : runtimeConfig.getViewPoolMappings())
55          {
56              urlPatterns.add(vpm.getUrlPattern());
57              Map<String,String> parameters = new HashMap<String, String>();
58              for (ViewPoolParameter param : vpm.getParameterList())
59              {
60                  parameters.put(param.getName(), param.getValue());
61              }
62              viewPoolList.add(new ViewPoolImpl(context, parameters));
63          }
64      }
65      
66      @Override
67      public ViewPool getViewPool(FacesContext context, UIViewRoot root)
68      {
69          for (int i = 0; i < urlPatterns.size(); i++)
70          {
71              String urlPattern = urlPatterns.get(i);
72              if (ViewProtectionUtils.matchPattern(root.getViewId(), urlPattern))
73              {
74                  return viewPoolList.get(i);
75              }
76          }
77          if (defaultViewPool != null)
78          {
79              // The default view pool applies to all views that does not have any view pool mapping,
80              // but only when oamEnableViewPool is set, so we need a check here when it is not to avoid
81              // use view pool on all views when only alwaysRecompile is set to true.
82              Boolean enableViewPool = (Boolean) root.getAttributes().get(ViewPoolProcessor.ENABLE_VIEW_POOL);
83              if (enableViewPool == null)
84              {
85                  return null;
86              }
87              return defaultViewPool;
88          }
89          return null;
90      }
91  }