
net.sf.itcb.common.web.vaadin.exception.ExceptionHandlerMapping Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of itcb-common-web-vaadin Show documentation
Show all versions of itcb-common-web-vaadin Show documentation
This module is the common portlet module. It defines the base on which the developements can be done by extending view, edit or help mode.
The newest version!
package net.sf.itcb.common.web.vaadin.exception;
import java.util.HashMap;
import java.util.Map;
import net.sf.itcb.common.web.vaadin.page.PageMappingProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import com.vaadin.terminal.Terminal;
/**
* The exception handler mapping stores a map in order to execute a specific mecanism for the exception.
* The pageMappingProcessor to do something in pages when an error occured
* @author Pierre Le Roux
*/
public class ExceptionHandlerMapping{
private Map exceptionMapping;
protected final Logger log = LoggerFactory.getLogger(getClass());
public ExceptionHandlerMapping() {
this.exceptionMapping = new HashMap();
}
/**
* Set the exception mapping in a map :
*
* - key : the name of the exception class that has to be managed (or subclasses)
* - value : the ExceptionHandler that will handle the exception
*
* @param exceptionMapping
*/
@Required
public void setExceptionMapping(Map exceptionMapping) {
log.debug("Initializing exception handlers...");
try {
for (Map.Entry entry : exceptionMapping.entrySet()) {
log.debug("{} Specific handler set", entry.getKey());
this.exceptionMapping.put(Class.forName(entry.getKey()), entry.getValue());
}
}
catch(ClassNotFoundException cnfe) {
log.error("ExceptionHandlerMapping is not set because a class doesn't exist in classpath.", cnfe);
}
}
/**
* Handle an exception according to the exception mapping set by Spring injection
* @param e the exception thrown that has to be handle
* @param pageMappingProcessor the user pageMappingProcessor in order to display exceptions...
*/
public void handleError(Terminal.ErrorEvent error, PageMappingProcessor pageMappingProcessor) {
if(error != null) {
Throwable t = error.getThrowable().getCause();
if(t != null) {
ExceptionHandler handler = exceptionMapping.get(t.getClass());
if (handler == null) {
for (Class entry : exceptionMapping.keySet()) {
if( entry.isInstance(t) ) {
handler = exceptionMapping.get(entry);
break;
}
}
}
if(handler != null) {
handler.handleError(error, pageMappingProcessor);
return;
}
}
}
// If no exception handler for this exception, we only log it
log.error("An unexpected error occurs : ", error.getThrowable().getMessage());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy