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.Comparator;
22  import java.util.List;
23  
24  import javax.el.ELResolver;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.myfaces.config.RuntimeConfig;
29  
30  /**
31   * Comparator for ELResolvers that shifts the Resolvers from
32   * the faces-config to the front.
33   * 
34   * @author Jakob Korherr (latest modification by $Author$)
35   * @version $Revision$ $Date$
36   *
37   * @since 1.2.10, 2.0.2
38   */
39  public class CustomFirstELResolverComparator implements Comparator<ELResolver>
40  {
41      
42      private List<ELResolver> _facesConfigResolvers;
43      
44      public int compare(ELResolver r1, ELResolver r2)
45      {
46          List<ELResolver> facesConfigResolvers = _getFacesConfigElResolvers();
47          
48          if (facesConfigResolvers == null)
49          {
50              // no el-resolvers in faces-config
51              return 0; // keep order
52          }
53          
54          boolean r1FromFacesConfig = facesConfigResolvers.contains(r1);
55          boolean r2FromFacesConfig = facesConfigResolvers.contains(r2);
56          
57          if (r1FromFacesConfig)
58          {
59              if (r2FromFacesConfig)
60              {
61                  // both are from faces-config
62                  return 0; // keep order
63              }
64              else
65              {
66                  // only r1 is from faces-config
67                  return -1;
68              }
69          }
70          else
71          {
72              if (r2FromFacesConfig)
73              {
74                  // only r2 is from faces-config
75                  return 1;
76              }
77              else
78              {
79                  // neither r1 nor r2 are from faces-config
80                  return 0; // keep order
81              }
82          }
83      }
84      
85      /**
86       * Returns a List of all ELResolvers from the faces-config.
87       * @return
88       */
89      private List<ELResolver> _getFacesConfigElResolvers()
90      {
91          if (_facesConfigResolvers == null)
92          {
93              ExternalContext externalContext
94                      = FacesContext.getCurrentInstance().getExternalContext();
95              RuntimeConfig runtimeConfig
96                      = RuntimeConfig.getCurrentInstance(externalContext);
97              _facesConfigResolvers 
98                      = runtimeConfig.getFacesConfigElResolvers();
99          }
100         
101         return _facesConfigResolvers;
102     }
103     
104 }