Coverage Report - org.apache.myfaces.util.ExternalSpecifications
 
Classes in this File Line Coverage Branch Coverage Complexity
ExternalSpecifications
0%
0/40
0%
0/32
4.4
 
 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  0
     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  0
         if (beanValidationAvailable == null)
 60  
         {
 61  
             try
 62  
             {
 63  
                 try
 64  
                 {
 65  0
                     beanValidationAvailable = (Class.forName("javax.validation.Validation") != null);
 66  
                 }
 67  0
                 catch(ClassNotFoundException e)
 68  
                 {
 69  0
                     beanValidationAvailable = Boolean.FALSE;
 70  0
                 }
 71  
 
 72  0
                 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  0
                         _ValidationUtils.tryBuildDefaultValidatorFactory();
 80  
                     }
 81  0
                     catch (Throwable t)
 82  
                     {
 83  
                         //log.log(Level.FINE, "Error initializing Bean Validation (could be normal)", t);
 84  0
                         beanValidationAvailable = false;
 85  0
                     }
 86  
                 }
 87  
             }
 88  0
             catch (Throwable t)
 89  
             {
 90  0
                 log.log(Level.FINE, "Error loading class (could be normal)", t);
 91  0
                 beanValidationAvailable = false;
 92  0
             }
 93  
 
 94  0
             log.info("MyFaces Bean Validation support " + (beanValidationAvailable ? "enabled" : "disabled"));
 95  
         }
 96  0
         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  0
         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  0
                 unifiedELAvailable = (
 116  
                         Class.forName("javax.el.ValueReference") != null
 117  
                      && Class.forName("javax.el.ValueExpression")
 118  
                                 .getMethod("getValueReference", ELContext.class) != null
 119  
                 );
 120  
             }
 121  0
             catch (Throwable t)
 122  
             {
 123  
                 //log.log(Level.FINE, "Error loading class (could be normal)", t);
 124  0
                 unifiedELAvailable = false;
 125  0
             }
 126  
 
 127  0
             log.info("MyFaces Unified EL support " + (unifiedELAvailable ? "enabled" : "disabled"));
 128  
         }
 129  0
         return unifiedELAvailable;
 130  
     }
 131  
     
 132  
     public static boolean isCDIAvailable(ExternalContext externalContext)
 133  
     {
 134  0
         if (cdiAvailable == null)
 135  
         {
 136  
             try
 137  
             {
 138  0
                 cdiAvailable = Class.forName("javax.enterprise.inject.spi.BeanManager") != null;
 139  
             }
 140  0
             catch (Throwable t)
 141  
             {
 142  
                 //log.log(Level.FINE, "Error loading class (could be normal)", t);
 143  0
                 cdiAvailable = false;
 144  0
             }
 145  
 
 146  0
             log.info("MyFaces CDI support " + (cdiAvailable ? "enabled" : "disabled"));
 147  
         }
 148  
 
 149  0
         return cdiAvailable && 
 150  
                 externalContext.getApplicationMap().containsKey(AbstractFacesInitializer.CDI_BEAN_MANAGER_INSTANCE);
 151  
     }
 152  
     
 153  
     public static boolean isEL3Available()
 154  
     {
 155  0
         if (el3Available == null)
 156  
         {
 157  
             try
 158  
             {
 159  0
                 el3Available = Class.forName("javax.el.StaticFieldELResolver") != null ;
 160  
             }
 161  0
             catch (Throwable t)
 162  
             {
 163  0
                 el3Available = false;
 164  0
             }
 165  0
             log.info("MyFaces EL 3.0 support " + (el3Available ? "enabled" : "disabled"));
 166  
         }
 167  0
         return el3Available;
 168  
     }
 169  
 
 170  
     /**
 171  
      * this class should not be instantiated.
 172  
      */
 173  
     private ExternalSpecifications()
 174  0
     {
 175  0
     }
 176  
 
 177  
 }