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