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