Coverage Report - org.apache.myfaces.config.FacesConfigValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
FacesConfigValidator
0%
0/46
0%
0/20
2.857
 
 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.net.MalformedURLException;
 22  
 import java.net.URL;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collection;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.faces.context.ExternalContext;
 29  
 
 30  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 31  
 import org.apache.myfaces.config.element.ManagedBean;
 32  
 import org.apache.myfaces.config.element.NavigationCase;
 33  
 import org.apache.myfaces.config.element.NavigationRule;
 34  
 import org.apache.myfaces.shared.util.ClassUtils;
 35  
 
 36  
 public class FacesConfigValidator
 37  
 {
 38  
 
 39  
     /**
 40  
      * Validate if the managed beans and navigations rules are correct.
 41  
      * 
 42  
      * <p>For example, it checks if the managed bean classes really exists, or if the 
 43  
      * navigation rules points to existing view files.</p>
 44  
      */
 45  
     @JSFWebConfigParam(since="2.0", defaultValue="false", expectedValues="true, false")
 46  
     public static final String VALIDATE_CONTEXT_PARAM = "org.apache.myfaces.VALIDATE";
 47  
     
 48  
     private FacesConfigValidator()
 49  0
     {
 50  
         // hidden 
 51  0
     }
 52  
 
 53  
     public static List<String> validate(ExternalContext ctx)
 54  
     {
 55  
         
 56  0
         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(ctx);
 57  
         
 58  0
         Map<String, ManagedBean> managedBeansMap = runtimeConfig.getManagedBeans();
 59  
         
 60  0
         Collection<? extends ManagedBean> managedBeans = null;
 61  0
         if (managedBeansMap != null)
 62  
         {
 63  0
             managedBeans = managedBeansMap.values();
 64  
         }
 65  
         
 66  0
         Collection<? extends NavigationRule> navRules = runtimeConfig.getNavigationRules();
 67  
         
 68  0
         return validate(managedBeans, navRules, ctx);
 69  
         
 70  
     }
 71  
     
 72  
     public static List<String> validate(Collection<? extends ManagedBean> managedBeans, 
 73  
                                         Collection<? extends NavigationRule> navRules, ExternalContext ctx)
 74  
     {
 75  
         
 76  0
         List<String> list = new ArrayList<String>();
 77  
         
 78  0
         if (managedBeans != null)
 79  
         {
 80  0
             validateManagedBeans(managedBeans, list);
 81  
         }
 82  
         
 83  0
         if (navRules != null)
 84  
         {
 85  0
             validateNavRules(navRules, list, ctx);
 86  
         }
 87  
         
 88  0
         return list;
 89  
     }
 90  
 
 91  
     private static void validateNavRules(Collection<? extends NavigationRule> navRules, List<String> list,
 92  
                                          ExternalContext ctx)
 93  
     {
 94  0
         for (NavigationRule navRule : navRules)
 95  
         {
 96  0
             validateNavRule(navRule, list, ctx);
 97  0
         }
 98  0
     }
 99  
     
 100  
     private static void validateNavRule(NavigationRule navRule, List<String> list, ExternalContext ctx)
 101  
     {
 102  0
         String fromId = navRule.getFromViewId();
 103  
         URL filePath;
 104  
         try
 105  
         {
 106  0
             filePath = ctx.getResource(fromId);
 107  
 
 108  0
             if(fromId != null && ! "*".equals(fromId) && filePath == null)
 109  
             {
 110  0
                 list.add("File for navigation 'from id' does not exist " + filePath);
 111  
             }            
 112  
         }
 113  0
         catch (MalformedURLException e)
 114  
         {
 115  0
             list.add("File for navigation 'from id' does not exist " + fromId);
 116  0
         }
 117  
         
 118  0
         for (NavigationCase caze : navRule.getNavigationCases())
 119  
         {
 120  
             try
 121  
             {
 122  0
                 URL toViewPath = ctx.getResource(caze.getToViewId());
 123  
                 
 124  0
                 if(toViewPath == null)
 125  
                 {
 126  0
                     list.add("File for navigation 'to id' does not exist " + toViewPath);
 127  
                 }
 128  
             }
 129  0
             catch (MalformedURLException e)
 130  
             {
 131  0
                 list.add("File for navigation 'from id' does not exist " + caze.getToViewId());
 132  0
             }
 133  0
         }
 134  0
     }
 135  
     
 136  
     private static void validateManagedBeans(Collection<? extends ManagedBean> managedBeans, List<String> list)
 137  
     {
 138  0
         for (ManagedBean managedBean : managedBeans)
 139  
         {
 140  0
             validateManagedBean(managedBean, list);
 141  0
         }
 142  0
     }
 143  
 
 144  
     private static void validateManagedBean(ManagedBean managedBean, List<String> list)
 145  
     {
 146  0
         String className = managedBean.getManagedBeanClassName();
 147  
         
 148  
         try
 149  
         {
 150  0
             ClassUtils.classForName(className);
 151  
         }
 152  0
         catch (ClassNotFoundException e)
 153  
         { 
 154  0
             String msg = "Could not locate class " 
 155  
                 + className + " for managed bean '" + managedBean.getManagedBeanName() + "'";
 156  
             
 157  0
             list.add(msg);
 158  0
         }
 159  0
     }
 160  
 }