Coverage Report - org.apache.myfaces.config.FacesConfigValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
FacesConfigValidator
0%
0/43
0%
0/24
0
 
 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.config;
 20  
 
 21  
 import java.io.File;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.faces.context.ExternalContext;
 29  
 
 30  
 import org.apache.myfaces.config.element.ManagedBean;
 31  
 import org.apache.myfaces.config.element.NavigationCase;
 32  
 import org.apache.myfaces.config.element.NavigationRule;
 33  
 import org.apache.myfaces.shared_impl.util.ClassUtils;
 34  
 
 35  
 public class FacesConfigValidator
 36  
 {
 37  
 
 38  
     public static final String VALIDATE_CONTEXT_PARAM = "org.apache.myfaces.VALIDATE";
 39  
     
 40  0
     private FacesConfigValidator(){
 41  
         // hidden 
 42  0
     }
 43  
 
 44  
     public static List<String> validate(ExternalContext ctx, String ctxPath){
 45  
         
 46  0
         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(ctx);
 47  
         
 48  0
         Map managedBeansMap = runtimeConfig.getManagedBeans();
 49  
         
 50  0
         Iterator managedBeans = managedBeansMap == null ? null : 
 51  
                                 managedBeansMap.values() == null ? null :
 52  
                                     managedBeansMap.values().iterator();
 53  
         
 54  0
         Iterator<NavigationRule> navRules = runtimeConfig.getNavigationRules() == null ? 
 55  
                             null : runtimeConfig.getNavigationRules().iterator();
 56  
         
 57  0
         return validate(managedBeans, navRules, ctxPath);
 58  
         
 59  
     }
 60  
     
 61  
     public static List<String> validate(Iterator managedBeans, Iterator<NavigationRule> navRules, String ctxPath){
 62  
         
 63  0
         List<String> list = new ArrayList<String>();
 64  
         
 65  0
         if(navRules != null)
 66  0
             validateNavRules(navRules, list, ctxPath);
 67  
         
 68  0
         if(managedBeans != null)
 69  0
             validateManagedBeans(managedBeans, list);
 70  
         
 71  0
         return list;
 72  
     }
 73  
 
 74  
     private static void validateNavRules(Iterator<NavigationRule> navRules, List<String> list, String ctxPath){
 75  
         
 76  0
         while(navRules.hasNext()){
 77  
             
 78  0
             NavigationRule navRule = navRules.next();
 79  
             
 80  0
             validateNavRule(navRule, list, ctxPath);
 81  
             
 82  0
         }
 83  
         
 84  0
     }
 85  
     
 86  
     private static void validateNavRule(NavigationRule navRule, List<String> list, String ctxPath){
 87  
         
 88  0
         String fromId = navRule.getFromViewId();
 89  0
         String filePath = ctxPath + fromId;
 90  
         
 91  0
         if(fromId != null && ! "*".equals(fromId) && ! new File(filePath).exists())
 92  0
             list.add("File for navigation 'from id' does not exist " + filePath);
 93  
         
 94  0
         Collection cases = navRule.getNavigationCases();
 95  
         
 96  0
         Iterator iterator = cases.iterator();
 97  
         
 98  0
         while(iterator.hasNext()){
 99  
             
 100  0
             NavigationCase caze = (NavigationCase) iterator.next();
 101  
             
 102  0
             String toViewPath = ctxPath + caze.getToViewId();
 103  
             
 104  0
             if(! new File(toViewPath).exists())
 105  0
                 list.add("File for navigation 'to id' does not exist " + toViewPath);
 106  
             
 107  0
         }
 108  
         
 109  0
     }
 110  
     
 111  
     private static void validateManagedBeans(Iterator managedBeans, List<String> list){
 112  
         
 113  0
         while(managedBeans.hasNext()){
 114  
             
 115  0
             ManagedBean managedBean = (ManagedBean) managedBeans.next();
 116  
             
 117  0
             validateManagedBean(managedBean, list);
 118  
             
 119  0
         }
 120  
         
 121  0
     }
 122  
 
 123  
     private static void validateManagedBean(ManagedBean managedBean, List<String> list){
 124  
         
 125  0
         String className = managedBean.getManagedBeanClassName();
 126  
         
 127  
         try
 128  
         {
 129  0
             ClassUtils.classForName(className);
 130  
         }
 131  0
         catch (ClassNotFoundException e)
 132  
         { 
 133  
             
 134  0
             String msg = "Could not locate class " 
 135  
                 + className + " for managed bean '" + managedBean.getManagedBeanName() + "'";
 136  
             
 137  0
             list.add(msg);
 138  
             
 139  0
         }
 140  
 
 141  0
     }
 142  
 
 143  
 }