use of javax.servlet.UnavailableException in project sonarqube by SonarSource.
the class ActionServlet method processForwardConfigClass.
/**
* <p>Checks if the current forwardConfig is using the correct class based
* on the class of its configuration ancestor. If actionConfig is
* provided, then this method will process the forwardConfig as part
* of that actionConfig. If actionConfig is null, the forwardConfig
* will be processed as a global forward.</p>
*
* @param forwardConfig The forward to check.
* @param moduleConfig The config for the current module.
* @param actionConfig If applicable, the config for the current action.
* @return The forward config using the correct class as determined by the
* config's ancestor and its own overridden value.
* @throws UnavailableException if an instance of the forward config class
* cannot be created.
* @throws ServletException on class creation error
*/
protected ForwardConfig processForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
String ancestor = forwardConfig.getExtends();
if (ancestor == null) {
// Nothing to do, then
return forwardConfig;
}
// Make sure that this config is of the right class
ForwardConfig baseConfig = null;
if (actionConfig != null) {
// Look for this in the actionConfig
baseConfig = actionConfig.findForwardConfig(ancestor);
}
if (baseConfig == null) {
// Either this is a forwardConfig that inherits a global config,
// or actionConfig is null
baseConfig = moduleConfig.findForwardConfig(ancestor);
}
if (baseConfig == null) {
throw new UnavailableException("Unable to find " + "forward '" + ancestor + "' to extend.");
}
// Was our forwards's class overridden already?
if (forwardConfig.getClass().equals(ActionForward.class)) {
// Ensure that our forward is using the correct class
if (!baseConfig.getClass().equals(forwardConfig.getClass())) {
// Replace the config with an instance of the correct class
ForwardConfig newForwardConfig = null;
String baseConfigClassName = baseConfig.getClass().getName();
try {
newForwardConfig = (ForwardConfig) RequestUtils.applicationInstance(baseConfigClassName);
// copy the values
BeanUtils.copyProperties(newForwardConfig, forwardConfig);
} catch (Exception e) {
handleCreationException(baseConfigClassName, e);
}
// replace forwardConfig with newForwardConfig
if (actionConfig != null) {
actionConfig.removeForwardConfig(forwardConfig);
actionConfig.addForwardConfig(newForwardConfig);
} else {
// this is a global forward
moduleConfig.removeForwardConfig(forwardConfig);
moduleConfig.addForwardConfig(newForwardConfig);
}
forwardConfig = newForwardConfig;
}
}
return forwardConfig;
}
use of javax.servlet.UnavailableException in project sonarqube by SonarSource.
the class ActionServlet method processExceptionConfigClass.
/**
* <p>Checks if the current exceptionConfig is using the correct class
* based on the class of its configuration ancestor. If actionConfig is
* provided, then this method will process the exceptionConfig as part
* of that actionConfig. If actionConfig is null, the exceptionConfig
* will be processed as a global forward.</p>
*
* @param exceptionConfig The config to check.
* @param moduleConfig The config for the current module.
* @param actionConfig If applicable, the config for the current action.
* @return The exception config using the correct class as determined by
* the config's ancestor and its own overridden value.
* @throws ServletException if an instance of the exception config class
* cannot be created.
*/
protected ExceptionConfig processExceptionConfigClass(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
String ancestor = exceptionConfig.getExtends();
if (ancestor == null) {
// Nothing to do, then
return exceptionConfig;
}
// Make sure that this config is of the right class
ExceptionConfig baseConfig = null;
if (actionConfig != null) {
baseConfig = actionConfig.findExceptionConfig(ancestor);
}
if (baseConfig == null) {
// This means either there's no actionConfig anyway, or the
// ancestor is not defined within the action.
baseConfig = moduleConfig.findExceptionConfig(ancestor);
}
if (baseConfig == null) {
throw new UnavailableException("Unable to find " + "exception config '" + ancestor + "' to extend.");
}
// Was our config's class overridden already?
if (exceptionConfig.getClass().equals(ExceptionConfig.class)) {
// Ensure that our config is using the correct class
if (!baseConfig.getClass().equals(exceptionConfig.getClass())) {
// Replace the config with an instance of the correct class
ExceptionConfig newExceptionConfig = null;
String baseConfigClassName = baseConfig.getClass().getName();
try {
newExceptionConfig = (ExceptionConfig) RequestUtils.applicationInstance(baseConfigClassName);
// copy the values
BeanUtils.copyProperties(newExceptionConfig, exceptionConfig);
} catch (Exception e) {
handleCreationException(baseConfigClassName, e);
}
// replace exceptionConfig with newExceptionConfig
if (actionConfig != null) {
actionConfig.removeExceptionConfig(exceptionConfig);
actionConfig.addExceptionConfig(newExceptionConfig);
} else {
moduleConfig.removeExceptionConfig(exceptionConfig);
moduleConfig.addExceptionConfig(newExceptionConfig);
}
exceptionConfig = newExceptionConfig;
}
}
return exceptionConfig;
}
use of javax.servlet.UnavailableException in project sonarqube by SonarSource.
the class ActionServlet method init.
/**
* <p>Initialize this servlet. Most of the processing has been factored
* into support methods so that you can override particular functionality
* at a fairly granular level.</p>
*
* @throws ServletException if we cannot configure ourselves correctly
*/
public void init() throws ServletException {
final String configPrefix = "config/";
final int configPrefixLength = configPrefix.length() - 1;
// to the developer
try {
initInternal();
initOther();
initServlet();
initChain();
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
initModuleConfigFactory();
// Initialize modules as needed
ModuleConfig moduleConfig = initModuleConfig("", config);
initModuleMessageResources(moduleConfig);
initModulePlugIns(moduleConfig);
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
moduleConfig.freeze();
Enumeration names = getServletConfig().getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (!name.startsWith(configPrefix)) {
continue;
}
String prefix = name.substring(configPrefixLength);
moduleConfig = initModuleConfig(prefix, getServletConfig().getInitParameter(name));
initModuleMessageResources(moduleConfig);
initModulePlugIns(moduleConfig);
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
moduleConfig.freeze();
}
this.initModulePrefixes(this.getServletContext());
this.destroyConfigDigester();
} catch (UnavailableException ex) {
throw ex;
} catch (Throwable t) {
// The follow error message is not retrieved from internal message
// resources as they may not have been able to have been
// initialized
log.error("Unable to initialize Struts ActionServlet due to an " + "unexpected exception or error thrown, so marking the " + "servlet as unavailable. Most likely, this is due to an " + "incorrect or missing library dependency.", t);
throw new UnavailableException(t.getMessage());
}
}
use of javax.servlet.UnavailableException in project sonarqube by SonarSource.
the class TestActionServlet method notestProcessForwardConfigClassError.
/**
* Make sure the code throws the correct exception when it can't create an
* instance of the base config's custom class.
*/
public void notestProcessForwardConfigClassError() throws Exception {
ForwardConfig customBase = new CustomForwardConfigArg("success", "/success.jsp");
moduleConfig.addForwardConfig(customBase);
ForwardConfig customSub = new ActionForward();
customSub.setName("failure");
customSub.setExtends("success");
moduleConfig.addForwardConfig(customSub);
try {
actionServlet.processForwardConfigClass(customSub, moduleConfig, null);
fail("Exception should be thrown");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unexpected exception thrown.");
}
}
use of javax.servlet.UnavailableException in project sonarqube by SonarSource.
the class TestActionServlet method testInitModuleExceptionConfigsNullFormType.
/**
* Test that initModuleExceptionConfigs throws an exception when a handler
* with a null key is present.
*/
public void testInitModuleExceptionConfigsNullFormType() throws ServletException {
ExceptionConfig handler = new ExceptionConfig();
handler.setType("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(handler);
try {
actionServlet.initModuleExceptionConfigs(moduleConfig);
fail("An exception should've been thrown here.");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unrecognized exception thrown: " + e);
}
}
Aggregations