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.context;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.faces.application.ResourceDependency;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  
28  /**
29   *
30   * @author  Leonardo Uribe (latest modification by $Author: lu4242 $)
31   * @version $Revision: 1298130 $ $Date: 2012-03-07 16:26:48 -0500 (Wed, 07 Mar 2012) $
32   * @since 2.0.2
33   */
34  public class RequestViewContext
35  {
36  
37      public static final String VIEW_CONTEXT_KEY = "oam.VIEW_CONTEXT";
38      
39      private Map<ResourceDependency, Boolean> addedResources;
40      
41      // No lazy init: every view has one (UIView.class) or more classes to process   
42      private Map<Class<?>, Boolean> processedClasses = new HashMap<Class<?>,Boolean>();
43      
44      private Map<String, Boolean> renderTargetMap = null;
45  
46      static public RequestViewContext getCurrentInstance()
47      {
48          FacesContext ctx = FacesContext.getCurrentInstance();
49          return getCurrentInstance(ctx);
50      }
51      
52      static public RequestViewContext getCurrentInstance(FacesContext ctx)
53      {
54          return getCurrentInstance(ctx, ctx.getViewRoot());
55      }
56      
57      @SuppressWarnings("unchecked")
58      static public RequestViewContext getCurrentInstance(FacesContext ctx, UIViewRoot root)
59      {
60          Map<UIViewRoot, RequestViewContext> map
61                  = (Map<UIViewRoot, RequestViewContext>) ctx.getAttributes().get(VIEW_CONTEXT_KEY);
62          RequestViewContext rvc = null;        
63          if (map == null)
64          {
65              map = new HashMap<UIViewRoot, RequestViewContext>();
66              rvc = new RequestViewContext();
67              map.put(root, rvc);
68              ctx.getAttributes().put(VIEW_CONTEXT_KEY, map);
69              return rvc;
70          }
71          else
72          {
73              rvc = map.get(root); 
74              if (rvc == null)
75              {
76                  rvc = new RequestViewContext();
77                  map.put(root, rvc);
78              }
79              return rvc;
80          }
81      }
82  
83      public boolean isResourceDependencyAlreadyProcessed(ResourceDependency dependency)
84      {
85          if (addedResources == null)
86          {
87              return false;
88          }
89          return addedResources.containsKey(dependency); 
90      }
91      
92      public void setResourceDependencyAsProcessed(ResourceDependency dependency)
93      {
94          if (addedResources == null)
95          {
96              addedResources = new HashMap<ResourceDependency,Boolean>();
97          }
98          addedResources.put(dependency, true);
99      }
100 
101     public boolean isClassAlreadyProcessed(Class<?> inspectedClass)
102     {
103         return processedClasses.containsKey(inspectedClass);
104     }
105 
106     public void setClassProcessed(Class<?> inspectedClass)
107     {
108         processedClasses.put(inspectedClass, Boolean.TRUE);
109     }
110     
111     public boolean isRenderTarget(String target)
112     {
113         if (renderTargetMap != null)
114         {
115             return Boolean.TRUE.equals(renderTargetMap.get(target));
116         }
117         return false;
118     }
119     
120     public void setRenderTarget(String target, boolean value)
121     {
122         if (renderTargetMap == null)
123         {
124             renderTargetMap = new HashMap<String, Boolean>(8);
125         }
126         renderTargetMap.put(target, value);
127     }
128 }