Created
December 9, 2010 16:52
-
-
Save mojavelinux/734963 to your computer and use it in GitHub Desktop.
Catch JSF integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import javax.enterprise.inject.spi.BeanManager; | |
import javax.faces.FacesException; | |
import javax.faces.context.ExceptionHandler; | |
import javax.faces.context.ExceptionHandlerWrapper; | |
import javax.faces.event.AbortProcessingException; | |
import javax.faces.event.ExceptionQueuedEvent; | |
import org.jboss.seam.exception.control.ExceptionToCatch; | |
import org.jboss.weld.extensions.core.Requires; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
@Requires("org.jboss.seam.exception.control.extension.CatchExtension") | |
public class CatchExceptionHandler extends ExceptionHandlerWrapper | |
{ | |
private transient Logger log = LoggerFactory.getLogger(CatchExceptionHandler.class); | |
private ExceptionHandler wrapped; | |
private BeanManager beanManager; | |
// default constructor required to make it a bean | |
public CatchExceptionHandler() {} | |
public CatchExceptionHandler(ExceptionHandler wrapped, BeanManager beanManager) | |
{ | |
this.wrapped = wrapped; | |
this.beanManager = beanManager; | |
} | |
@Override | |
public void handle() throws FacesException | |
{ | |
Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator(); | |
if (!it.hasNext()) | |
{ | |
return; | |
} | |
// example of multiple queued exceptions is two setter methods that failed to update | |
// or a failure in both before and after phase of phase listener | |
while (it.hasNext()) | |
{ | |
Throwable t = it.next().getContext().getException(); | |
if (!(t instanceof AbortProcessingException)) | |
{ | |
ExceptionToCatch catchEvent = new ExceptionToCatch(t); | |
beanManager.fireEvent(catchEvent); | |
if (catchEvent.isHandled()) | |
{ | |
System.out.println("handled"); | |
it.remove(); | |
} | |
} | |
} | |
if (getUnhandledExceptionQueuedEvents().iterator().hasNext()) | |
{ | |
log.debug("Exceptions remain, will be thrown outside of JSF"); | |
wrapped.handle(); | |
} | |
} | |
@Override | |
public ExceptionHandler getWrapped() | |
{ | |
return wrapped; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.enterprise.inject.spi.BeanManager; | |
import javax.faces.context.ExceptionHandler; | |
import javax.faces.context.ExceptionHandlerFactory; | |
import org.jboss.weld.extensions.beanManager.BeanManagerAccessor; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class CatchExceptionHandlerFactory extends ExceptionHandlerFactory | |
{ | |
private transient Logger log = LoggerFactory.getLogger(CatchExceptionHandler.class); | |
private ExceptionHandlerFactory parent; | |
private transient boolean catchUnavailable = false; | |
public CatchExceptionHandlerFactory(ExceptionHandlerFactory parent) | |
{ | |
this.parent = parent; | |
} | |
@Override | |
public ExceptionHandler getExceptionHandler() | |
{ | |
log.trace("Creating the JSF exception handler"); | |
if (catchUnavailable) | |
{ | |
log.trace("Catch integration previously disabled"); | |
return parent.getExceptionHandler(); | |
} | |
BeanManager beanManager = null; | |
try | |
{ | |
beanManager = BeanManagerAccessor.getBeanManager(); | |
} | |
catch (IllegalArgumentException e) | |
{ | |
log.info("Could not location BeanManager, Catch integration disabled"); | |
catchUnavailable = true; | |
return parent.getExceptionHandler(); | |
} | |
if (beanManager.getBeans(CatchExceptionHandler.class).isEmpty()) | |
{ | |
log.info("Catch not available, Catch integration disabled"); | |
catchUnavailable = true; | |
return parent.getExceptionHandler(); | |
} | |
log.info("Catch integration enabled"); | |
return new CatchExceptionHandler(parent.getExceptionHandler(), beanManager); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<faces-config version="2.0" | |
xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation=" | |
http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> | |
<factory> | |
<exception-handler-factory>com.acme.handler.CatchExceptionHandlerFactory</exception-handler-factory> | |
</factory> | |
</faces-config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment