1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.plugins;
19
20 import org.apache.struts.action.ActionServlet;
21 import org.apache.struts.action.PlugIn;
22 import org.apache.struts.config.ForwardConfig;
23 import org.apache.struts.config.MessageResourcesConfig;
24 import org.apache.struts.config.ModuleConfig;
25 import org.apache.struts.config.PlugInConfig;
26 import org.apache.struts.util.RequestUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import javax.servlet.ServletException;
31
32 /***
33 * <p>Convenient implementation of {@link PlugIn} that performs as many
34 * verification tests on the information stored in the {@link ModuleConfig}
35 * for this module as is practical. Based on the setting of the
36 * <code>fatal</code> property (which defaults to <code>true</code>), the
37 * detection of any such errors will cause a <code>ServletException</code> to
38 * be thrown from the <code>init</code> method, which will ultimately cause
39 * the initialization of your Struts controller servlet to fail.</p>
40 *
41 * @version $Rev: 377024 $ $Date: 2006-02-11 12:39:19 -0700 (Sat, 11 Feb 2006) $
42 * @since Struts 1.1
43 */
44 public class ModuleConfigVerifier implements PlugIn {
45
46 /***
47 * Commons Logging instance.
48 */
49 private static Log LOG = LogFactory.getLog(ModuleConfigVerifier.class);
50
51
52
53 /***
54 * <p>The {@link ModuleConfig} instance for our module.</p>
55 */
56 protected ModuleConfig config = null;
57
58 /***
59 * <p>The {@link ActionServlet} instance we are associated with.</p>
60 */
61 protected ActionServlet servlet = null;
62
63
64
65 /***
66 * <p>Should the existence of configuration errors be fatal.</p>
67 */
68 private boolean fatal = true;
69
70 /***
71 * <p>Return the "configuation errors are fatal" flag.</p>
72 */
73 public boolean isFatal() {
74 return (this.fatal);
75 }
76
77 /***
78 * <p>Set the "configuration errors are fatal" flag.</p>
79 *
80 * @param fatal The new flag value
81 */
82 public void setFatal(boolean fatal) {
83 this.fatal = fatal;
84 }
85
86
87
88 /***
89 * <p>Receive notification that our owning module is being shut down.</p>
90 */
91 public void destroy() {
92 ;
93 }
94
95
96 public void init(ActionServlet servlet, ModuleConfig config)
97 throws ServletException {
98 this.servlet = servlet;
99 this.config = config;
100
101 boolean ok = true;
102
103 LOG.info(servlet.getInternal().getMessage("configVerifying"));
104
105
106
107
108
109
110
111
112
113 if (!verifyActionMappingClass()) {
114 ok = false;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 if (!verifyForwardConfigs()) {
129 ok = false;
130 }
131
132 if (!verifyMessageResourcesConfigs()) {
133 ok = false;
134 }
135
136 if (!verifyPlugInConfigs()) {
137 ok = false;
138 }
139
140
141 LOG.info(servlet.getInternal().getMessage("configCompleted"));
142
143 if (!ok && isFatal()) {
144 throw new ServletException(servlet.getInternal().getMessage("configFatal"));
145 }
146 }
147
148
149
150 /***
151 * <p>Return <code>true</code> if information returned by
152 * <code>config.getActionMappingClass</code> is all valid; otherwise, log
153 * error messages and return <code>false</code>.</p>
154 */
155 protected boolean verifyActionMappingClass() {
156 String amcName = config.getActionMappingClass();
157
158 if (amcName == null) {
159 LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.missing"));
160
161 return (false);
162 }
163
164 try {
165 Class amcClass = RequestUtils.applicationClass(amcName);
166 } catch (ClassNotFoundException e) {
167 LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.invalid",
168 amcName));
169
170 return (false);
171 }
172
173 return (true);
174 }
175
176 /***
177 * <p>Return <code>true</code> if information returned by
178 * <code>config.findForwardConfigs</code> is all valid; otherwise, log
179 * error messages and return <code>false</code>.</p>
180 */
181 protected boolean verifyForwardConfigs() {
182 boolean ok = true;
183 ForwardConfig[] fcs = config.findForwardConfigs();
184
185 for (int i = 0; i < fcs.length; i++) {
186 String path = fcs[i].getPath();
187
188 if (path == null) {
189 LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.missing",
190 fcs[i].getName()));
191 ok = false;
192 } else if (!path.startsWith("/")) {
193 LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.invalid",
194 path, fcs[i].getName()));
195 }
196 }
197
198 return (ok);
199 }
200
201 /***
202 * <p>Return <code>true</code> if information returned by
203 * <code>config.findMessageResourcesConfigs</code> is all valid;
204 * otherwise, log error messages and return <code>false</code>.</p>
205 */
206 protected boolean verifyMessageResourcesConfigs() {
207 boolean ok = true;
208 MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();
209
210 for (int i = 0; i < mrcs.length; i++) {
211 String factory = mrcs[i].getFactory();
212
213 if (factory == null) {
214 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.missing"));
215 ok = false;
216 } else {
217 try {
218 Class clazz = RequestUtils.applicationClass(factory);
219 } catch (ClassNotFoundException e) {
220 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.invalid",
221 factory));
222 ok = false;
223 }
224 }
225
226 String key = mrcs[i].getKey();
227
228 if (key == null) {
229 LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.key"));
230 }
231 }
232
233 return (ok);
234 }
235
236 /***
237 * <p>Return <code>true</code> if information returned by
238 * <code>config.findPluginConfigs</code> is all valid; otherwise, log
239 * error messages and return <code>false</code>.</p>
240 */
241 protected boolean verifyPlugInConfigs() {
242 boolean ok = true;
243 PlugInConfig[] pics = config.findPlugInConfigs();
244
245 for (int i = 0; i < pics.length; i++) {
246 String className = pics[i].getClassName();
247
248 if (className == null) {
249 LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.missing"));
250 ok = false;
251 } else {
252 try {
253 Class clazz = RequestUtils.applicationClass(className);
254 } catch (ClassNotFoundException e) {
255 LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.invalid",
256 className));
257 ok = false;
258 }
259 }
260 }
261
262 return (ok);
263 }
264 }