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.el.unified;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  import javax.el.ELResolver;
27  import javax.faces.application.Application;
28  import javax.faces.context.FacesContext;
29  import javax.faces.el.PropertyResolver;
30  import javax.faces.el.VariableResolver;
31  
32  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
33  import org.apache.myfaces.config.RuntimeConfig;
34  import org.apache.myfaces.el.convert.PropertyResolverToELResolver;
35  import org.apache.myfaces.el.convert.VariableResolverToELResolver;
36  import org.apache.myfaces.shared.config.MyfacesConfig;
37  
38  /**
39   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
40   * @version $Revision: 1296050 $ $Date: 2012-03-02 04:48:51 +0000 (Fri, 02 Mar 2012) $
41   */
42  @SuppressWarnings("deprecation")
43  public class ResolverBuilderBase
44  {
45      
46      private static final Logger log = Logger.getLogger(ResolverBuilderBase.class.getName());
47      
48      /**
49       * Define a custom comparator class used to sort the ELResolvers.
50       * 
51       * <p>This is useful when it is necessary to put an ELResolver on top of other resolvers. Note set
52       * this param override the default ordering described by JSF spec section 5. 
53       * </p>
54       */
55      @JSFWebConfigParam(since = "1.2.10, 2.0.2", group="EL",
56              desc = "The Class of an Comparator&lt;ELResolver&gt; implementation.")
57      public static final String EL_RESOLVER_COMPARATOR = "org.apache.myfaces.EL_RESOLVER_COMPARATOR";
58      
59      private final RuntimeConfig _config;
60  
61      public ResolverBuilderBase(RuntimeConfig config)
62      {
63          _config = config;
64      }
65  
66      /**
67       * add the el resolvers from the faces config, the el resolver wrapper for variable resolver, the el resolver
68       * wrapper for the property resolver and the el resolvers added by {@link Application#addELResolver(ELResolver)}.
69       * The resolvers where only added if they are not null
70       * 
71       * @param resolvers
72       */
73      protected void addFromRuntimeConfig(List<ELResolver> resolvers)
74      {
75          if (_config.getFacesConfigElResolvers() != null)
76          {
77              for (ELResolver resolver : _config.getFacesConfigElResolvers())
78              {
79                  resolvers.add(resolver);
80              }
81          }
82  
83          FacesContext facesContext = FacesContext.getCurrentInstance();
84          if (facesContext == null)
85          {
86              // Should not happen, but if by some reason happens,
87              // initialize as usual.
88              if (_config.getVariableResolver() != null)
89              {
90                  resolvers.add(createELResolver(_config.getVariableResolver()));
91              }
92              else if (_config.getVariableResolverChainHead() != null)
93              {
94                  resolvers.add(createELResolver(_config.getVariableResolverChainHead()));
95              }
96  
97              if (_config.getPropertyResolver() != null)
98              {
99                  resolvers.add(createELResolver(_config.getPropertyResolver()));
100             }
101             else if (_config.getPropertyResolverChainHead() != null)
102             {
103                 resolvers.add(createELResolver(_config.getPropertyResolverChainHead()));
104             }
105         }
106         else if (facesContext != null && MyfacesConfig.getCurrentInstance(
107                 facesContext.getExternalContext()).isSupportJSPAndFacesEL())
108         {
109             if (_config.getVariableResolver() != null)
110             {
111                 resolvers.add(createELResolver(_config.getVariableResolver()));
112             }
113             else if (_config.getVariableResolverChainHead() != null)
114             {
115                 resolvers.add(createELResolver(_config.getVariableResolverChainHead()));
116             }
117 
118             if (_config.getPropertyResolver() != null)
119             {
120                 resolvers.add(createELResolver(_config.getPropertyResolver()));
121             }
122             else if (_config.getPropertyResolverChainHead() != null)
123             {
124                 resolvers.add(createELResolver(_config.getPropertyResolverChainHead()));
125             }
126         }
127 
128         if (_config.getApplicationElResolvers() != null)
129         {
130             for (ELResolver resolver : _config.getApplicationElResolvers())
131             {
132                 resolvers.add(resolver);
133             }
134         }
135     }
136     
137     /**
138      * Sort the ELResolvers with a custom Comparator provided by the user.
139      * @param resolvers
140      * @since 1.2.10, 2.0.2
141      */
142     @SuppressWarnings("unchecked")
143     protected void sortELResolvers(List<ELResolver> resolvers)
144     {
145         if (_config.getELResolverComparator() != null)
146         {
147             try
148             {
149                 // sort the resolvers
150                 Collections.sort(resolvers, _config.getELResolverComparator());
151             }
152             catch (Exception e)
153             {
154                 log.log(Level.WARNING, 
155                         "Could not sort ELResolvers with custom Comparator", e);
156             }
157         }
158     }
159 
160     protected ELResolver createELResolver(VariableResolver resolver)
161     {
162         return new VariableResolverToELResolver(resolver);
163     }
164 
165     protected ELResolver createELResolver(PropertyResolver resolver)
166     {
167         return new PropertyResolverToELResolver(resolver);
168     }
169 
170 }