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.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.context.FacesContext;
29  import org.apache.myfaces.view.facelets.el.ELText;
30  
31  /**
32   *
33   * @author lu4242
34   */
35  public class RequestViewMetadata implements Serializable
36  {
37      public static final String RESOURCE_DEPENDENCY_KEY = "oam.component.resource.RDK";
38  
39      // No lazy init: every view has one (UIView.class) or more classes to process   
40      private Map<Class<?>, Boolean> processedClasses = new HashMap<Class<?>,Boolean>();
41      
42      private Map<ResourceDependency, Boolean> addedResources;
43      
44      private Map<Class<?>, Boolean> initialProcessedClasses;
45      private Map<ResourceDependency, Boolean> initialAddedResources;
46      
47      public RequestViewMetadata()
48      {
49          initialProcessedClasses = null;
50          initialAddedResources = null;
51      }
52      
53      /**
54       * Clone the current request view metadata into another instance, so 
55       * it can be used in a view.
56       * 
57       * @return 
58       */
59      public RequestViewMetadata cloneInstance()
60      {
61          RequestViewMetadata rvm = new RequestViewMetadata();
62          rvm.initialProcessedClasses = new HashMap<Class<?>, Boolean>(
63                  this.initialProcessedClasses != null ? 
64                      this.initialProcessedClasses : this.processedClasses);
65          if (this.initialAddedResources != null)
66          {
67              rvm.initialAddedResources = new HashMap<ResourceDependency, Boolean>(
68                      this.initialAddedResources);
69          }
70          else if (this.addedResources != null)
71          {
72              rvm.initialAddedResources = new HashMap<ResourceDependency, Boolean>(
73                      this.addedResources);
74          }
75          return rvm;
76      }
77      
78      public boolean isResourceDependencyAlreadyProcessed(ResourceDependency dependency)
79      {
80          if (initialAddedResources != null)
81          {
82              if (initialAddedResources.containsKey(dependency))
83              {
84                  return true;
85              }
86          }
87          if (addedResources == null)
88          {
89              return false;
90          }
91          return addedResources.containsKey(dependency); 
92      }
93      
94      public void setResourceDependencyAsProcessed(ResourceDependency dependency)
95      {
96          if (addedResources == null)
97          {
98              addedResources = new HashMap<ResourceDependency,Boolean>();
99          }
100         addedResources.put(dependency, true);
101     }
102 
103     public boolean isClassAlreadyProcessed(Class<?> inspectedClass)
104     {
105         if (initialProcessedClasses != null)
106         {
107             if (initialProcessedClasses.containsKey(inspectedClass))
108             {
109                 return true;
110             }
111         }
112         return processedClasses.containsKey(inspectedClass);
113     }
114 
115     public void setClassProcessed(Class<?> inspectedClass)
116     {
117         processedClasses.put(inspectedClass, Boolean.TRUE);
118     }
119     
120     public Map<String, List<ResourceDependency>> getResourceDependencyAnnotations(FacesContext context)
121     {
122         if (initialAddedResources == null && addedResources == null)
123         {
124             return Collections.emptyMap();
125         }
126         Map<String, List<ResourceDependency>> map = new HashMap<String, List<ResourceDependency>>();
127         //List<ResourceDependency> list = new ArrayList<ResourceDependency>();
128         if (initialAddedResources != null)
129         {
130             for (ResourceDependency annotation : initialAddedResources.keySet())
131             {
132                 String target = annotation.target();
133                 if (target != null && target.length() > 0)
134                 {
135                     target = ELText.parse(context.getApplication().getExpressionFactory(),
136                                           context.getELContext(), target).toString(context.getELContext());
137                 }
138                 else
139                 {
140                     target = "head";
141                 }
142                 List<ResourceDependency> list = map.get(target);
143                 if (list == null)
144                 {
145                     list = new ArrayList<ResourceDependency>();
146                     map.put(target, list);
147                 }
148                 list.add(annotation);
149             }
150         }
151         if (addedResources != null)
152         {
153             for (ResourceDependency annotation : addedResources.keySet())
154             {
155                 String target = annotation.target();
156                 if (target != null && target.length() > 0)
157                 {
158                     target = ELText.parse(context.getApplication().getExpressionFactory(),
159                                           context.getELContext(), target).toString(context.getELContext());
160                 }
161                 else
162                 {
163                     target = "head";
164                 }
165                 List<ResourceDependency> list = map.get(target);
166                 if (list == null)
167                 {
168                     list = new ArrayList<ResourceDependency>();
169                     map.put(target, list);
170                 }
171                 list.add(annotation);
172             }
173         }
174         return map;
175     }
176 }