1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.plugins;
22
23 import org.apache.struts.action.ActionServlet;
24 import org.apache.struts.action.PlugIn;
25 import org.apache.struts.config.ForwardConfig;
26 import org.apache.struts.config.MessageResourcesConfig;
27 import org.apache.struts.config.ModuleConfig;
28 import org.apache.struts.config.PlugInConfig;
29 import org.apache.struts.util.RequestUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import javax.servlet.ServletException;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ModuleConfigVerifier implements PlugIn {
48
49
50
51
52 private static Log LOG = LogFactory.getLog(ModuleConfigVerifier.class);
53
54
55
56
57
58
59 protected ModuleConfig config = null;
60
61
62
63
64 protected ActionServlet servlet = null;
65
66
67
68
69
70
71 private boolean fatal = true;
72
73
74
75
76 public boolean isFatal() {
77 return (this.fatal);
78 }
79
80
81
82
83
84
85 public void setFatal(boolean fatal) {
86 this.fatal = fatal;
87 }
88
89
90
91
92
93
94 public void destroy() {
95 ;
96 }
97
98
99 public void init(ActionServlet servlet, ModuleConfig config)
100 throws ServletException {
101 this.servlet = servlet;
102 this.config = config;
103
104 boolean ok = true;
105
106 LOG.info(servlet.getInternal().getMessage("configVerifying"));
107
108
109
110
111
112
113
114
115
116 if (!verifyActionMappingClass()) {
117 ok = false;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 if (!verifyForwardConfigs()) {
132 ok = false;
133 }
134
135 if (!verifyMessageResourcesConfigs()) {
136 ok = false;
137 }
138
139 if (!verifyPlugInConfigs()) {
140 ok = false;
141 }
142
143
144 LOG.info(servlet.getInternal().getMessage("configCompleted"));
145
146 if (!ok && isFatal()) {
147 throw new ServletException(servlet.getInternal().getMessage("configFatal"));
148 }
149 }
150
151
152
153
154
155
156
157
158 protected boolean verifyActionMappingClass() {
159 String amcName = config.getActionMappingClass();
160
161 if (amcName == null) {
162 LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.missing"));
163
164 return (false);
165 }
166
167 try {
168 Class amcClass = RequestUtils.applicationClass(amcName);
169 } catch (ClassNotFoundException e) {
170 LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.invalid",
171 amcName));
172
173 return (false);
174 }
175
176 return (true);
177 }
178
179
180
181
182
183
184 protected boolean verifyForwardConfigs() {
185 boolean ok = true;
186 ForwardConfig[] fcs = config.findForwardConfigs();
187
188 for (int i = 0; i < fcs.length; i++) {
189 String path = fcs[i].getPath();
190
191 if (path == null) {
192 LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.missing",
193 fcs[i].getName()));
194 ok = false;
195 } else if (!path.startsWith("/")) {
196 LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.invalid",
197 path, fcs[i].getName()));
198 }
199 }
200
201 return (ok);
202 }
203
204
205
206
207
208
209 protected boolean verifyMessageResourcesConfigs() {
210 boolean ok = true;
211 MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();
212
213 for (int i = 0; i < mrcs.length; i++) {
214 String factory = mrcs[i].getFactory();
215
216 if (factory == null) {
217 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.missing"));
218 ok = false;
219 } else {
220 try {
221 Class clazz = RequestUtils.applicationClass(factory);
222 } catch (ClassNotFoundException e) {
223 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.invalid",
224 factory));
225 ok = false;
226 }
227 }
228
229 String key = mrcs[i].getKey();
230
231 if (key == null) {
232 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.key"));
233 }
234 }
235
236 return (ok);
237 }
238
239
240
241
242
243
244 protected boolean verifyPlugInConfigs() {
245 boolean ok = true;
246 PlugInConfig[] pics = config.findPlugInConfigs();
247
248 for (int i = 0; i < pics.length; i++) {
249 String className = pics[i].getClassName();
250
251 if (className == null) {
252 LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.missing"));
253 ok = false;
254 } else {
255 try {
256 Class clazz = RequestUtils.applicationClass(className);
257 } catch (ClassNotFoundException e) {
258 LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.invalid",
259 className));
260 ok = false;
261 }
262 }
263 }
264
265 return (ok);
266 }
267 }