1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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 | |
|
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 | |
} |