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  
20  package org.apache.myfaces.tobago.util;
21  
22  import javax.faces.application.Application;
23  import javax.faces.context.FacesContext;
24  
25  public enum FacesVersion {
26  
27    VERSION_11,
28    VERSION_12,
29    VERSION_20,
30    VERSION_21,
31    VERSION_22;
32  
33    private static FacesVersion currentVersion;
34    private static boolean mojarra;
35    private static boolean myfaces;
36  
37    static {
38      try {
39        currentVersion = VERSION_11;
40        Application.class.getMethod("getExpressionFactory");
41        currentVersion = VERSION_12;
42        Application.class.getMethod("getDefaultValidatorInfo");
43        currentVersion = VERSION_20;
44        FacesContext.class.getMethod("isReleased");
45        currentVersion = VERSION_21;
46        Application.class.getMethod("getFlowHandler");
47        currentVersion = VERSION_22;
48      } catch (NoSuchMethodException e) {
49        // ignore
50      }
51  
52      mojarra = isAvailable("com.sun.faces.application.ApplicationImpl");
53      myfaces = isAvailable("org.apache.myfaces.application.ApplicationImpl");
54  
55    }
56  
57    private static boolean isAvailable(String className) {
58      try {
59        try {
60          Class.forName(className, false, Thread.currentThread().getContextClassLoader());
61          return true;
62        } catch (ClassNotFoundException e) {
63          // ignore
64          try {
65            Class.forName(className, false, FacesVersion.class.getClassLoader());
66            return true;
67          } catch (ClassNotFoundException e1) {
68            // ignore
69          }
70        }
71      } catch (Exception e) {
72        // ignore
73      }
74      return false;
75    }
76  
77    /**
78     * Does the JSF is version 1.2 or higher
79     * @return Supports 1.2 or higher
80     */
81    public static boolean supports12() {
82      return currentVersion == VERSION_12
83          || currentVersion == VERSION_20
84          || currentVersion == VERSION_21
85          || currentVersion == VERSION_22;
86    }
87  
88    /**
89     * Does the JSF is version 2.0 or higher
90     * @return Supports 2.0 or higher
91     */
92    public static boolean supports20() {
93      return currentVersion == VERSION_20
94          || currentVersion == VERSION_21
95          || currentVersion == VERSION_22;
96    }
97  
98    /**
99     * Does the JSF is version 2.1 or higher
100    * @return Supports 2.1 or higher
101    */
102   public static boolean supports21() {
103     return currentVersion == VERSION_21
104         || currentVersion == VERSION_22;
105   }
106 
107   /**
108    * Does the JSF is version 2.2 or higher
109    * @return Supports 2.2 or higher
110    */
111   public static boolean supports22() {
112     return currentVersion == VERSION_22;
113   }
114 
115   public static boolean isMojarra() {
116     return mojarra;
117   }
118 
119   public static boolean isMyfaces() {
120     return myfaces;
121   }
122 }