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.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      {
50          // hidden 
51      }
52  
53      public static List<String> validate(ExternalContext ctx)
54      {
55          
56          RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(ctx);
57          
58          Map<String, ManagedBean> managedBeansMap = runtimeConfig.getManagedBeans();
59          
60          Collection<? extends ManagedBean> managedBeans = null;
61          if (managedBeansMap != null)
62          {
63              managedBeans = managedBeansMap.values();
64          }
65          
66          Collection<? extends NavigationRule> navRules = runtimeConfig.getNavigationRules();
67          
68          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          List<String> list = new ArrayList<String>();
77          
78          if (managedBeans != null)
79          {
80              validateManagedBeans(managedBeans, list);
81          }
82          
83          if (navRules != null)
84          {
85              validateNavRules(navRules, list, ctx);
86          }
87          
88          return list;
89      }
90  
91      private static void validateNavRules(Collection<? extends NavigationRule> navRules, List<String> list,
92                                           ExternalContext ctx)
93      {
94          for (NavigationRule navRule : navRules)
95          {
96              validateNavRule(navRule, list, ctx);
97          }
98      }
99      
100     private static void validateNavRule(NavigationRule navRule, List<String> list, ExternalContext ctx)
101     {
102         String fromId = navRule.getFromViewId();
103         URL filePath;
104         try
105         {
106             filePath = ctx.getResource(fromId);
107 
108             if(fromId != null && ! "*".equals(fromId) && filePath == null)
109             {
110                 list.add("File for navigation 'from id' does not exist " + filePath);
111             }            
112         }
113         catch (MalformedURLException e)
114         {
115             list.add("File for navigation 'from id' does not exist " + fromId);
116         }
117         
118         for (NavigationCase caze : navRule.getNavigationCases())
119         {
120             try
121             {
122                 URL toViewPath = ctx.getResource(caze.getToViewId());
123                 
124                 if(toViewPath == null)
125                 {
126                     list.add("File for navigation 'to id' does not exist " + toViewPath);
127                 }
128             }
129             catch (MalformedURLException e)
130             {
131                 list.add("File for navigation 'from id' does not exist " + caze.getToViewId());
132             }
133         }
134     }
135     
136     private static void validateManagedBeans(Collection<? extends ManagedBean> managedBeans, List<String> list)
137     {
138         for (ManagedBean managedBean : managedBeans)
139         {
140             validateManagedBean(managedBean, list);
141         }
142     }
143 
144     private static void validateManagedBean(ManagedBean managedBean, List<String> list)
145     {
146         String className = managedBean.getManagedBeanClassName();
147         
148         try
149         {
150             ClassUtils.classForName(className);
151         }
152         catch (ClassNotFoundException e)
153         { 
154             String msg = "Could not locate class " 
155                 + className + " for managed bean '" + managedBean.getManagedBeanName() + "'";
156             
157             list.add(msg);
158         }
159     }
160 }