
pl.bristleback.server.bristle.config.ExceptionHandlersInitializer Maven / Gradle / Ivy
package pl.bristleback.server.bristle.config;
import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.exceptions.handlers.ExceptionHandler;
import pl.bristleback.server.bristle.exceptions.handlers.ExceptionHandlersContainer;
import pl.bristleback.server.bristle.exceptions.handlers.ExceptionType;
import java.util.List;
import java.util.Map;
/**
* This class loads exception handlers using plugin settings taken from configuration file.
* Exception handlers may be Spring refs or simple instances created by plugin.
*
* Created on: 2010-10-24 00:26:35
*
* @author Wojciech Niemiec
*/
public final class ExceptionHandlersInitializer {
private static Logger log = Logger.getLogger(ExceptionHandlersInitializer.class.getName());
private static final String HANDLER_CLASS_SETTING_PREFIX = "exceptionHandler";
private ExceptionHandlersContainer exceptionHandlersContainer;
/**
* Loads exception handlers and stores results in exception handlers container.
*
* @param pluginSettings map of plugin settings taken from configuration file.
*/
public void loadExceptionsHandlers(Map pluginSettings) {
exceptionHandlersContainer = new ExceptionHandlersContainer();
List exceptionHandlerConfigurationsList = getHandlersInformationList(pluginSettings);
for (ClassConfiguration exceptionHandlerConfiguration : exceptionHandlerConfigurationsList) {
addHandler(exceptionHandlerConfiguration);
}
}
private void addHandler(ClassConfiguration exceptionHandlerConfiguration) {
ExceptionType exceptionType = getExceptionType(exceptionHandlerConfiguration.getName());
if (exceptionType != null) {
ExceptionHandler exceptionHandler = getExceptionHandler(exceptionHandlerConfiguration);
exceptionHandlersContainer.addHandler(exceptionType, exceptionHandler);
}
}
private ExceptionType getExceptionType(String exceptionTypeName) {
try {
return ExceptionType.valueOf(exceptionTypeName);
} catch (Exception e) {
return null;
}
}
private ExceptionHandler getExceptionHandler(ClassConfiguration exceptionHandlerConfiguration) {
return ClassConfigurationUtil.getInstanceFromConfiguration(ExceptionHandler.class, exceptionHandlerConfiguration);
}
private List getHandlersInformationList(Map pluginSettings) {
return ClassConfigurationUtil.getClassConfigurationsFromSettingsMap(HANDLER_CLASS_SETTING_PREFIX, pluginSettings);
}
/**
* Gets exception handlers container. Container must be loaded before by invoking
* {@link pl.bristleback.server.bristle.config.ExceptionHandlersInitializer#loadExceptionsHandlers(java.util.Map)} method.
*
* @return exception handlers container.
*/
public ExceptionHandlersContainer getExceptionHandlersContainer() {
return exceptionHandlersContainer;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy