![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.struts.plugins.ModuleConfigVerifier Maven / Gradle / Ivy
/*
* $Id: ModuleConfigVerifier.java 471754 2006-11-06 14:55:09Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts.plugins;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.MessageResourcesConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.PlugInConfig;
import org.apache.struts.util.RequestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletException;
/**
* Convenient implementation of {@link PlugIn} that performs as many
* verification tests on the information stored in the {@link ModuleConfig}
* for this module as is practical. Based on the setting of the
* fatal
property (which defaults to true
), the
* detection of any such errors will cause a ServletException
to
* be thrown from the init
method, which will ultimately cause
* the initialization of your Struts controller servlet to fail.
*
* @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
* @since Struts 1.1
*/
public class ModuleConfigVerifier implements PlugIn {
/**
* Commons Logging instance.
*/
private static Log LOG = LogFactory.getLog(ModuleConfigVerifier.class);
// ----------------------------------------------------- Instance Variables
/**
* The {@link ModuleConfig} instance for our module.
*/
protected ModuleConfig config = null;
/**
* The {@link ActionServlet} instance we are associated with.
*/
protected ActionServlet servlet = null;
// ------------------------------------------------------------- Properties
/**
* Should the existence of configuration errors be fatal.
*/
private boolean fatal = true;
/**
* Return the "configuation errors are fatal" flag.
*/
public boolean isFatal() {
return (this.fatal);
}
/**
* Set the "configuration errors are fatal" flag.
*
* @param fatal The new flag value
*/
public void setFatal(boolean fatal) {
this.fatal = fatal;
}
// --------------------------------------------------------- Public Methods
/**
* Receive notification that our owning module is being shut down.
*/
public void destroy() {
; // No action required
}
// See interface for Javadoc.
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
this.servlet = servlet;
this.config = config;
boolean ok = true;
LOG.info(servlet.getInternal().getMessage("configVerifying"));
// Perform detailed validations of each portion of ModuleConfig
// :TODO: Complete methods to verify Action, Controller, et al, configurations.
/*
if (!verifyActionConfigs()) {
ok = false;
}
*/
if (!verifyActionMappingClass()) {
ok = false;
}
/*
if (!verifyControllerConfig()) {
ok = false;
}
if (!verifyExceptionConfigs()) {
ok = false;
}
if (!verifyFormBeanConfigs()) {
ok = false;
}
*/
if (!verifyForwardConfigs()) {
ok = false;
}
if (!verifyMessageResourcesConfigs()) {
ok = false;
}
if (!verifyPlugInConfigs()) {
ok = false;
}
// Throw an exception on a fatal error
LOG.info(servlet.getInternal().getMessage("configCompleted"));
if (!ok && isFatal()) {
throw new ServletException(servlet.getInternal().getMessage("configFatal"));
}
}
// ------------------------------------------------------ Protected Methods
/**
* Return true
if information returned by
* config.getActionMappingClass
is all valid; otherwise, log
* error messages and return false
.
*/
protected boolean verifyActionMappingClass() {
String amcName = config.getActionMappingClass();
if (amcName == null) {
LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.missing"));
return (false);
}
try {
Class amcClass = RequestUtils.applicationClass(amcName);
} catch (ClassNotFoundException e) {
LOG.error(servlet.getInternal().getMessage("verifyActionMappingClass.invalid",
amcName));
return (false);
}
return (true);
}
/**
* Return true
if information returned by
* config.findForwardConfigs
is all valid; otherwise, log
* error messages and return false
.
*/
protected boolean verifyForwardConfigs() {
boolean ok = true;
ForwardConfig[] fcs = config.findForwardConfigs();
for (int i = 0; i < fcs.length; i++) {
String path = fcs[i].getPath();
if (path == null) {
LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.missing",
fcs[i].getName()));
ok = false;
} else if (!path.startsWith("/")) {
LOG.error(servlet.getInternal().getMessage("verifyForwardConfigs.invalid",
path, fcs[i].getName()));
}
}
return (ok);
}
/**
* Return true
if information returned by
* config.findMessageResourcesConfigs
is all valid;
* otherwise, log error messages and return false
.
*/
protected boolean verifyMessageResourcesConfigs() {
boolean ok = true;
MessageResourcesConfig[] mrcs = config.findMessageResourcesConfigs();
for (int i = 0; i < mrcs.length; i++) {
String factory = mrcs[i].getFactory();
if (factory == null) {
LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.missing"));
ok = false;
} else {
try {
Class clazz = RequestUtils.applicationClass(factory);
} catch (ClassNotFoundException e) {
LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.invalid",
factory));
ok = false;
}
}
String key = mrcs[i].getKey();
if (key == null) {
LOG.error(servlet.getInternal().getMessage("verifyMessageResourcesConfigs.key"));
}
}
return (ok);
}
/**
* Return true
if information returned by
* config.findPluginConfigs
is all valid; otherwise, log
* error messages and return false
.
*/
protected boolean verifyPlugInConfigs() {
boolean ok = true;
PlugInConfig[] pics = config.findPlugInConfigs();
for (int i = 0; i < pics.length; i++) {
String className = pics[i].getClassName();
if (className == null) {
LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.missing"));
ok = false;
} else {
try {
Class clazz = RequestUtils.applicationClass(className);
} catch (ClassNotFoundException e) {
LOG.error(servlet.getInternal().getMessage("verifyPlugInConfigs.invalid",
className));
ok = false;
}
}
}
return (ok);
}
}