001: /*
002: * $Id: ModuleConfigVerifier.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.plugins;
022:
023: import org.apache.struts.action.ActionServlet;
024: import org.apache.struts.action.PlugIn;
025: import org.apache.struts.config.ForwardConfig;
026: import org.apache.struts.config.MessageResourcesConfig;
027: import org.apache.struts.config.ModuleConfig;
028: import org.apache.struts.config.PlugInConfig;
029: import org.apache.struts.util.RequestUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import javax.servlet.ServletException;
034:
035: /**
036: * <p>Convenient implementation of {@link PlugIn} that performs as many
037: * verification tests on the information stored in the {@link ModuleConfig}
038: * for this module as is practical. Based on the setting of the
039: * <code>fatal</code> property (which defaults to <code>true</code>), the
040: * detection of any such errors will cause a <code>ServletException</code> to
041: * be thrown from the <code>init</code> method, which will ultimately cause
042: * the initialization of your Struts controller servlet to fail.</p>
043: *
044: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
045: * @since Struts 1.1
046: */
047: public class ModuleConfigVerifier implements PlugIn {
048:
049: /**
050: * Commons Logging instance.
051: */
052: private static Log LOG = LogFactory
053: .getLog(ModuleConfigVerifier.class);
054:
055: // ----------------------------------------------------- Instance Variables
056:
057: /**
058: * <p>The {@link ModuleConfig} instance for our module.</p>
059: */
060: protected ModuleConfig config = null;
061:
062: /**
063: * <p>The {@link ActionServlet} instance we are associated with.</p>
064: */
065: protected ActionServlet servlet = null;
066:
067: // ------------------------------------------------------------- Properties
068:
069: /**
070: * <p>Should the existence of configuration errors be fatal.</p>
071: */
072: private boolean fatal = true;
073:
074: /**
075: * <p>Return the "configuation errors are fatal" flag.</p>
076: */
077: public boolean isFatal() {
078: return (this .fatal);
079: }
080:
081: /**
082: * <p>Set the "configuration errors are fatal" flag.</p>
083: *
084: * @param fatal The new flag value
085: */
086: public void setFatal(boolean fatal) {
087: this .fatal = fatal;
088: }
089:
090: // --------------------------------------------------------- Public Methods
091:
092: /**
093: * <p>Receive notification that our owning module is being shut down.</p>
094: */
095: public void destroy() {
096: ; // No action required
097: }
098:
099: // See interface for Javadoc.
100: public void init(ActionServlet servlet, ModuleConfig config)
101: throws ServletException {
102: this .servlet = servlet;
103: this .config = config;
104:
105: boolean ok = true;
106:
107: LOG.info(servlet.getInternal().getMessage("configVerifying"));
108:
109: // Perform detailed validations of each portion of ModuleConfig
110: // :TODO: Complete methods to verify Action, Controller, et al, configurations.
111:
112: /*
113: if (!verifyActionConfigs()) {
114: ok = false;
115: }
116: */
117: if (!verifyActionMappingClass()) {
118: ok = false;
119: }
120:
121: /*
122: if (!verifyControllerConfig()) {
123: ok = false;
124: }
125: if (!verifyExceptionConfigs()) {
126: ok = false;
127: }
128: if (!verifyFormBeanConfigs()) {
129: ok = false;
130: }
131: */
132: if (!verifyForwardConfigs()) {
133: ok = false;
134: }
135:
136: if (!verifyMessageResourcesConfigs()) {
137: ok = false;
138: }
139:
140: if (!verifyPlugInConfigs()) {
141: ok = false;
142: }
143:
144: // Throw an exception on a fatal error
145: LOG.info(servlet.getInternal().getMessage("configCompleted"));
146:
147: if (!ok && isFatal()) {
148: throw new ServletException(servlet.getInternal()
149: .getMessage("configFatal"));
150: }
151: }
152:
153: // ------------------------------------------------------ Protected Methods
154:
155: /**
156: * <p>Return <code>true</code> if information returned by
157: * <code>config.getActionMappingClass</code> is all valid; otherwise, log
158: * error messages and return <code>false</code>.</p>
159: */
160: protected boolean verifyActionMappingClass() {
161: String amcName = config.getActionMappingClass();
162:
163: if (amcName == null) {
164: LOG.error(servlet.getInternal().getMessage(
165: "verifyActionMappingClass.missing"));
166:
167: return (false);
168: }
169:
170: try {
171: Class amcClass = RequestUtils.applicationClass(amcName);
172: } catch (ClassNotFoundException e) {
173: LOG.error(servlet.getInternal().getMessage(
174: "verifyActionMappingClass.invalid", amcName));
175:
176: return (false);
177: }
178:
179: return (true);
180: }
181:
182: /**
183: * <p>Return <code>true</code> if information returned by
184: * <code>config.findForwardConfigs</code> is all valid; otherwise, log
185: * error messages and return <code>false</code>.</p>
186: */
187: protected boolean verifyForwardConfigs() {
188: boolean ok = true;
189: ForwardConfig[] fcs = config.findForwardConfigs();
190:
191: for (int i = 0; i < fcs.length; i++) {
192: String path = fcs[i].getPath();
193:
194: if (path == null) {
195: LOG.error(servlet.getInternal().getMessage(
196: "verifyForwardConfigs.missing",
197: fcs[i].getName()));
198: ok = false;
199: } else if (!path.startsWith("/")) {
200: LOG.error(servlet.getInternal().getMessage(
201: "verifyForwardConfigs.invalid", path,
202: fcs[i].getName()));
203: }
204: }
205:
206: return (ok);
207: }
208:
209: /**
210: * <p>Return <code>true</code> if information returned by
211: * <code>config.findMessageResourcesConfigs</code> is all valid;
212: * otherwise, log error messages and return <code>false</code>.</p>
213: */
214: protected boolean verifyMessageResourcesConfigs() {
215: boolean ok = true;
216: MessageResourcesConfig[] mrcs = config
217: .findMessageResourcesConfigs();
218:
219: for (int i = 0; i < mrcs.length; i++) {
220: String factory = mrcs[i].getFactory();
221:
222: if (factory == null) {
223: LOG.error(servlet.getInternal().getMessage(
224: "verifyMessageResourcesConfigs.missing"));
225: ok = false;
226: } else {
227: try {
228: Class clazz = RequestUtils
229: .applicationClass(factory);
230: } catch (ClassNotFoundException e) {
231: LOG.error(servlet.getInternal().getMessage(
232: "verifyMessageResourcesConfigs.invalid",
233: factory));
234: ok = false;
235: }
236: }
237:
238: String key = mrcs[i].getKey();
239:
240: if (key == null) {
241: LOG.error(servlet.getInternal().getMessage(
242: "verifyMessageResourcesConfigs.key"));
243: }
244: }
245:
246: return (ok);
247: }
248:
249: /**
250: * <p>Return <code>true</code> if information returned by
251: * <code>config.findPluginConfigs</code> is all valid; otherwise, log
252: * error messages and return <code>false</code>.</p>
253: */
254: protected boolean verifyPlugInConfigs() {
255: boolean ok = true;
256: PlugInConfig[] pics = config.findPlugInConfigs();
257:
258: for (int i = 0; i < pics.length; i++) {
259: String className = pics[i].getClassName();
260:
261: if (className == null) {
262: LOG.error(servlet.getInternal().getMessage(
263: "verifyPlugInConfigs.missing"));
264: ok = false;
265: } else {
266: try {
267: Class clazz = RequestUtils
268: .applicationClass(className);
269: } catch (ClassNotFoundException e) {
270: LOG.error(servlet.getInternal().getMessage(
271: "verifyPlugInConfigs.invalid", className));
272: ok = false;
273: }
274: }
275: }
276:
277: return (ok);
278: }
279: }
|