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