KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > plugins > ModuleConfigVerifier


1 /*
2  * $Id: ModuleConfigVerifier.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.plugins;
21
22
23 import javax.servlet.ServletException JavaDoc;
24 import org.apache.struts.action.ActionServlet;
25 import org.apache.struts.action.PlugIn;
26 import org.apache.struts.config.ForwardConfig;
27 import org.apache.struts.config.MessageResourcesConfig;
28 import org.apache.struts.config.ModuleConfig;
29 import org.apache.struts.config.PlugInConfig;
30 import org.apache.struts.util.RequestUtils;
31
32
33 /**
34  * <p>Convenient implementation of {@link PlugIn} that performs as many
35  * verification tests on the information stored in the {@link ModuleConfig}
36  * for this module as is practical. Based on the setting of the
37  * <code>fatal</code> property (which defaults to <code>true</code>), the
38  * detection of any such errors will cause a <code>ServletException</code>
39  * to be thrown from the <code>init</code> method, which will ultimately
40  * cause the initialization of your Struts controller servlet to fail.</p>
41  *
42  * <p>Under all circumstances, errors that are detected will be logged via
43  * calls to <code>ServletContext.log</code>.</p>
44  *
45  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
46  * @since Struts 1.1
47  */

48
49 public class ModuleConfigVerifier implements PlugIn {
50
51
52     // ----------------------------------------------------- Instance Variables
53

54
55     /**
56      * <p>The {@link ModuleConfig} instance for our module.</p>
57      */

58     protected ModuleConfig config = null;
59
60
61     /**
62      * <p>The {@link ActionServlet} instance we are associated with.</p>
63      */

64     protected ActionServlet servlet = null;
65
66
67     // ------------------------------------------------------------- Properties
68

69
70     /**
71      * <p>Should the existence of configuration errors be fatal.</p>
72      */

73     private boolean fatal = true;
74
75
76     /**
77      * <p>Return the "configuation errors are fatal" flag.</p>
78      */

79     public boolean isFatal() {
80
81         return (this.fatal);
82
83     }
84
85
86     /**
87      * <p>Set the "configuration errors are fatal" flag.</p>
88      *
89      * @param fatal The new flag value
90      */

91     public void setFatal(boolean fatal) {
92
93         this.fatal = fatal;
94
95     }
96
97
98     // --------------------------------------------------------- Public Methods
99

100
101     /**
102      * <p>Receive notification that our owning module is being
103      * shut down.</p>
104      */

105     public void destroy() {
106
107         ; // No action required
108

109     }
110
111
112         // See interface for Javadoc.
113
public void init(ActionServlet servlet, ModuleConfig config)
114         throws ServletException JavaDoc {
115
116         this.servlet = servlet;
117         this.config = config;
118         boolean ok = true;
119         log(servlet.getInternal().getMessage("configVerifying"));
120
121         // Perform detailed validations of each portion of ModuleConfig
122
// :TODO: Complete methods to verify Action, Controller, et al, configurations.
123
/*
124         if (!verifyActionConfigs()) {
125             ok = false;
126         }
127         */

128         if (!verifyActionMappingClass()) {
129             ok = false;
130         }
131         /*
132         if (!verifyControllerConfig()) {
133             ok = false;
134         }
135         if (!verifyDataSourceConfigs()) {
136             ok = false;
137         }
138         if (!verifyExceptionConfigs()) {
139             ok = false;
140         }
141         if (!verifyFormBeanConfigs()) {
142             ok = false;
143         }
144         */

145         if (!verifyForwardConfigs()) {
146             ok = false;
147         }
148         if (!verifyMessageResourcesConfigs()) {
149             ok = false;
150         }
151         if (!verifyPlugInConfigs()) {
152             ok = false;
153         }
154
155         // Throw an exception on a fatal error
156
log(servlet.getInternal().getMessage("configCompleted"));
157         if (!ok && isFatal()) {
158             throw new ServletException JavaDoc
159                 (servlet.getInternal().getMessage("configFatal"));
160         }
161
162
163     }
164
165
166
167     // ------------------------------------------------------ Protected Methods
168

169
170     /**
171      * <p>Log the specified message to our servlet context log, after a
172      * header including the module prefix.</p>
173      *
174      * @param message The message to be logged
175      */

176     protected void log(String JavaDoc message) {
177
178         String JavaDoc output = "[" + config.getPrefix() + "]: " + message;
179         servlet.log(output);
180
181     }
182
183
184     /**
185      * <p>Return <code>true</code> if information returned by
186      * <code>config.getActionMappingClass</code> is all valid;
187      * otherwise, log error messages and return <code>false</code>.</p>
188      */

189     protected boolean verifyActionMappingClass() {
190
191         String JavaDoc amcName = config.getActionMappingClass();
192         if (amcName == null) {
193             log(servlet.getInternal().getMessage
194                 ("verifyActionMappingClass.missing"));
195             return (false);
196         }
197         try {
198             Class JavaDoc amcClass = RequestUtils.applicationClass(amcName);
199         } catch (ClassNotFoundException JavaDoc e) {
200             log(servlet.getInternal().getMessage
201                 ("verifyActionMappingClass.invalid", amcName));
202             return (false);
203         }
204         return (true);
205
206     }
207
208
209     /**
210      * <p>Return <code>true</code> if information returned by
211      * <code>config.findForwardConfigs</code> is all valid;
212      * otherwise, log error messages and return <code>false</code>.</p>
213      */

214     protected boolean verifyForwardConfigs() {
215
216         boolean ok = true;
217         ForwardConfig fcs[] = config.findForwardConfigs();
218         for (int i = 0; i < fcs.length; i++) {
219             String JavaDoc path = fcs[i].getPath();
220             if (path == null) {
221                 log(servlet.getInternal().getMessage
222                     ("verifyForwardConfigs.missing",
223                      fcs[i].getName()));
224                 ok = false;
225             } else if (!path.startsWith("/")) {
226                 log(servlet.getInternal().getMessage
227                     ("verifyForwardConfigs.invalid", path,
228                      fcs[i].getName()));
229             }
230         }
231         return (ok);
232
233     }
234
235
236     /**
237      * <p>Return <code>true</code> if information returned by
238      * <code>config.findMessageResourcesConfigs</code> is all valid;
239      * otherwise, log error messages and return <code>false</code>.</p>
240      */

241     protected boolean verifyMessageResourcesConfigs() {
242
243         boolean ok = true;
244         MessageResourcesConfig mrcs[] = config.findMessageResourcesConfigs();
245         for (int i = 0; i < mrcs.length; i++) {
246             String JavaDoc factory = mrcs[i].getFactory();
247             if (factory == null) {
248                 log(servlet.getInternal().getMessage
249                     ("verifyMessageResourcesConfigs.missing"));
250                 ok = false;
251             } else {
252                 try {
253                     Class JavaDoc clazz = RequestUtils.applicationClass(factory);
254                 } catch (ClassNotFoundException JavaDoc e) {
255                     log(servlet.getInternal().getMessage
256                         ("verifyMessageResourcesConfigs.invalid",
257                          factory));
258                     ok = false;
259                 }
260             }
261             String JavaDoc key = mrcs[i].getKey();
262             if (key == null) {
263                 log(servlet.getInternal().getMessage
264                     ("verifyMessageResourcesConfigs.key"));
265             }
266         }
267         return (ok);
268
269     }
270
271
272     /**
273      * <p>Return <code>true</code> if information returned by
274      * <code>config.findPluginConfigs</code> is all valid;
275      * otherwise, log error messages and return <code>false</code>.</p>
276      */

277     protected boolean verifyPlugInConfigs() {
278
279         boolean ok = true;
280         PlugInConfig pics[] = config.findPlugInConfigs();
281         for (int i = 0; i < pics.length; i++) {
282             String JavaDoc className = pics[i].getClassName();
283             if (className == null) {
284                 log(servlet.getInternal().getMessage
285                     ("verifyPlugInConfigs.missing"));
286                 ok = false;
287             } else {
288                 try {
289                     Class JavaDoc clazz = RequestUtils.applicationClass(className);
290                 } catch (ClassNotFoundException JavaDoc e) {
291                     log(servlet.getInternal().getMessage
292                         ("verifyPlugInConfigs.invalid", className));
293                     ok = false;
294                 }
295             }
296         }
297         return (ok);
298
299     }
300
301
302 }
303
Popular Tags