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  import javax.faces.context.ExternalContext;
26  import org.apache.myfaces.webapp.AbstractFacesInitializer;
27  
28  /**
29   * <p>
30   * Utility class for determining which specifications are available
31   * in the current process. See JIRA issue: http://issues.apache.org/jira/browse/MYFACES-2386
32   * </p>
33   *
34   * @author Jan-Kees van Andel
35   * @author Jakob Korherr (latest modification by $Author$)
36   * @version $Revision$ $Date$
37   * @since 2.0
38   */
39  public final class ExternalSpecifications
40  {
41  
42      //private static final Log log = LogFactory.getLog(BeanValidator.class);
43      private static final Logger log = Logger.getLogger(ExternalSpecifications.class.getName());
44  
45      private static volatile Boolean beanValidationAvailable;
46      private static volatile Boolean unifiedELAvailable;
47      private static volatile Boolean cdiAvailable;
48      private static volatile Boolean el3Available;
49  
50      /**
51       * This method determines if Bean Validation is present.
52       *
53       * Eager initialization is used for performance. This means Bean Validation binaries
54       * should not be added at runtime after this variable has been set.
55       * @return true if Bean Validation is available, false otherwise.
56       */
57      public static boolean isBeanValidationAvailable()
58      {
59          if (beanValidationAvailable == null)
60          {
61              try
62              {
63                  try
64                  {
65                      beanValidationAvailable = (Class.forName("javax.validation.Validation") != null);
66                  }
67                  catch(ClassNotFoundException e)
68                  {
69                      beanValidationAvailable = Boolean.FALSE;
70                  }
71  
72                  if (beanValidationAvailable)
73                  {
74                      try
75                      {
76                          // Trial-error approach to check for Bean Validation impl existence.
77                          // If any Exception occurs here, we assume that Bean Validation is not available.
78                          // The cause may be anything, i.e. NoClassDef, config error...
79                          _ValidationUtils.tryBuildDefaultValidatorFactory();
80                      }
81                      catch (Throwable t)
82                      {
83                          //log.log(Level.FINE, "Error initializing Bean Validation (could be normal)", t);
84                          beanValidationAvailable = false;
85                      }
86                  }
87              }
88              catch (Throwable t)
89              {
90                  log.log(Level.FINE, "Error loading class (could be normal)", t);
91                  beanValidationAvailable = false;
92              }
93  
94              log.info("MyFaces Bean Validation support " + (beanValidationAvailable ? "enabled" : "disabled"));
95          }
96          return beanValidationAvailable;
97      }
98  
99      /**
100      * This method determines if Unified EL is present.
101      *
102      * Eager initialization is used for performance. This means Unified EL binaries
103      * should not be added at runtime after this variable has been set.
104      * @return true if UEL is available, false otherwise.
105      */
106     public static boolean isUnifiedELAvailable()
107     {
108         if (unifiedELAvailable == null)
109         {
110             try
111             {
112                 // Check if the UEL classes are available.
113                 // If the JSP EL classes are loaded first, UEL will not work
114                 // properly, hence it will be disabled.
115                 unifiedELAvailable = (
116                         Class.forName("javax.el.ValueReference") != null
117                      && Class.forName("javax.el.ValueExpression")
118                                 .getMethod("getValueReference", ELContext.class) != null
119                 );
120             }
121             catch (Throwable t)
122             {
123                 //log.log(Level.FINE, "Error loading class (could be normal)", t);
124                 unifiedELAvailable = false;
125             }
126 
127             log.info("MyFaces Unified EL support " + (unifiedELAvailable ? "enabled" : "disabled"));
128         }
129         return unifiedELAvailable;
130     }
131     
132     public static boolean isCDIAvailable(ExternalContext externalContext)
133     {
134         if (cdiAvailable == null)
135         {
136             try
137             {
138                 cdiAvailable = Class.forName("javax.enterprise.inject.spi.BeanManager") != null;
139             }
140             catch (Throwable t)
141             {
142                 //log.log(Level.FINE, "Error loading class (could be normal)", t);
143                 cdiAvailable = false;
144             }
145 
146             log.info("MyFaces CDI support " + (cdiAvailable ? "enabled" : "disabled"));
147         }
148 
149         return cdiAvailable && 
150                 externalContext.getApplicationMap().containsKey(AbstractFacesInitializer.CDI_BEAN_MANAGER_INSTANCE);
151     }
152     
153     public static boolean isEL3Available()
154     {
155         if (el3Available == null)
156         {
157             try
158             {
159                 el3Available = Class.forName("javax.el.StaticFieldELResolver") != null ;
160             }
161             catch (Throwable t)
162             {
163                 el3Available = false;
164             }
165             log.info("MyFaces EL 3.0 support " + (el3Available ? "enabled" : "disabled"));
166         }
167         return el3Available;
168     }
169 
170     /**
171      * this class should not be instantiated.
172      */
173     private ExternalSpecifications()
174     {
175     }
176 
177 }