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.util;
20  
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import javax.el.ELContext;
25  
26  /**
27   * <p>
28   * Utility class for determining which specifications are available
29   * in the current process. See JIRA issue: http://issues.apache.org/jira/browse/MYFACES-2386
30   * </p>
31   *
32   * @author Jan-Kees van Andel
33   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
34   * @version $Revision: 963000 $ $Date: 2010-07-11 01:54:18 -0500 (Sun, 11 Jul 2010) $
35   * @since 2.0
36   */
37  public final class ExternalSpecifications
38  {
39  
40      //private static final Log log = LogFactory.getLog(BeanValidator.class);
41      private static final Logger log = Logger.getLogger(ExternalSpecifications.class.getName());
42  
43      private static volatile Boolean beanValidationAvailable;
44      private static volatile Boolean unifiedELAvailable;
45  
46      /**
47       * This method determines if Bean Validation is present.
48       *
49       * Eager initialization is used for performance. This means Bean Validation binaries
50       * should not be added at runtime after this variable has been set.
51       * @return true if Bean Validation is available, false otherwise.
52       */
53      public static boolean isBeanValidationAvailable()
54      {
55          if (beanValidationAvailable == null)
56          {
57              try
58              {
59                  try
60                  {
61                      beanValidationAvailable = (Class.forName("javax.validation.Validation") != null);
62                  }
63                  catch(ClassNotFoundException e)
64                  {
65                      beanValidationAvailable = Boolean.FALSE;
66                  }
67  
68                  if (beanValidationAvailable)
69                  {
70                      try
71                      {
72                          // Trial-error approach to check for Bean Validation impl existence.
73                          // If any Exception occurs here, we assume that Bean Validation is not available.
74                          // The cause may be anything, i.e. NoClassDef, config error...
75                          _ValidationUtils.tryBuildDefaultValidatorFactory();
76                      }
77                      catch (Throwable t)
78                      {
79                          //log.log(Level.FINE, "Error initializing Bean Validation (could be normal)", t);
80                          beanValidationAvailable = false;
81                      }
82                  }
83              }
84              catch (Throwable t)
85              {
86                  log.log(Level.FINE, "Error loading class (could be normal)", t);
87                  beanValidationAvailable = false;
88              }
89  
90              log.info("MyFaces Bean Validation support " + (beanValidationAvailable ? "enabled" : "disabled"));
91          }
92          return beanValidationAvailable;
93      }
94  
95      /**
96       * This method determines if Unified EL is present.
97       *
98       * Eager initialization is used for performance. This means Unified EL binaries
99       * should not be added at runtime after this variable has been set.
100      * @return true if UEL is available, false otherwise.
101      */
102     public static boolean isUnifiedELAvailable()
103     {
104         if (unifiedELAvailable == null)
105         {
106             try
107             {
108                 // Check if the UEL classes are available.
109                 // If the JSP EL classes are loaded first, UEL will not work
110                 // properly, hence it will be disabled.
111                 unifiedELAvailable = (
112                         Class.forName("javax.el.ValueReference") != null
113                      && Class.forName("javax.el.ValueExpression")
114                                 .getMethod("getValueReference", ELContext.class) != null
115                 );
116             }
117             catch (Throwable t)
118             {
119                 //log.log(Level.FINE, "Error loading class (could be normal)", t);
120                 unifiedELAvailable = false;
121             }
122 
123             log.info("MyFaces Unified EL support " + (unifiedELAvailable ? "enabled" : "disabled"));
124         }
125         return unifiedELAvailable;
126     }
127 
128     /**
129      * this class should not be instantiated.
130      */
131     private ExternalSpecifications()
132     {
133     }
134 
135 }