All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jakarta.faces.webapp.PreJsf2ExceptionHandlerFactory Maven / Gradle / Ivy

There is a newer version: 4.1.1
Show newest version
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package jakarta.faces.webapp;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.el.ELException;
import jakarta.faces.FacesException;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UpdateModelException;
import jakarta.faces.context.ExceptionHandler;
import jakarta.faces.context.ExceptionHandlerFactory;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.AbortProcessingException;
import jakarta.faces.event.ExceptionQueuedEvent;
import jakarta.faces.event.ExceptionQueuedEventContext;
import jakarta.faces.event.PhaseId;
import jakarta.faces.event.SystemEvent;

/**
 * 

* This {@link ExceptionHandlerFactory} instance produces Jakarta Server Faces 1.2 compatible {@link ExceptionHandler} * instances. The {@link ExceptionHandler#handle} method of the ExceptionHandler produced by this factory * must meet the following requirements *

*
* *
    * *
  • *

    * Any exceptions thrown before or after phase execution will be logged and swallowed. *

    *
  • * *
  • *

    * The implementation must examine the Exception within each of the unhandled exception events. If the * Exception is an instance of {@link UpdateModelException}, extract the {@link FacesMessage} from the * UpdateModelException. Log a SEVERE message to the log and queue the * FacesMessage on the {@link FacesContext}, using the clientId of the source component in a * call to {@link FacesContext#addMessage(java.lang.String, jakarta.faces.application.FacesMessage)} *

    *
  • * *
* *
* * @since 2.0 */ public class PreJsf2ExceptionHandlerFactory extends ExceptionHandlerFactory { public PreJsf2ExceptionHandlerFactory() { } // ------------------------------------ Methods from ExceptionHandlerFactory /** * @return a new {@link ExceptionHandler} that behaves in a fashion compatible with specifications prior to Jakarta * Server Faces 1.2 */ @Override public ExceptionHandler getExceptionHandler() { return new PreJsf2ExceptionHandler(); } // ---------------------------------------------------------- Nested Classes /** * Jakarta Server Faces 1.2-style ExceptionHandler implementation. */ private static final class PreJsf2ExceptionHandler extends ExceptionHandler { private static final Logger LOGGER = Logger.getLogger("jakarta.faces.webapp", "jakarta.faces.LogStrings"); private static final String LOG_BEFORE_KEY = "servere.webapp.prejsf2.exception.handler.log_before"; private static final String LOG_AFTER_KEY = "servere.webapp.prejsf2.exception.handler.log_after"; private static final String LOG_KEY = "servere.webapp.prejsf2.exception.handler.log"; private LinkedList unhandledExceptions; private LinkedList handledExceptions; private ExceptionQueuedEvent handled; // ------------------------------------------- Methods from ExceptionHandler /** * @see ExceptionHandler@getHandledExceptionQueuedEvent() */ @Override public ExceptionQueuedEvent getHandledExceptionQueuedEvent() { return handled; } /** * * * @since 2.0 */ @Override public void handle() throws FacesException { for (Iterator i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); try { Throwable t = context.getException(); if (isRethrown(t, context.inBeforePhase() || context.inAfterPhase())) { handled = event; Throwable unwrapped = getRootCause(t); if (unwrapped != null) { throw new FacesException(unwrapped.getMessage(), unwrapped); } else { if (t instanceof FacesException) { throw (FacesException) t; } else { throw new FacesException(t.getMessage(), t); } } } else { log(context); } } finally { if (handledExceptions == null) { handledExceptions = new LinkedList<>(); } handledExceptions.add(event); i.remove(); } } } /** * @see jakarta.faces.context.ExceptionHandler#isListenerForSource(Object) */ @Override public boolean isListenerForSource(Object source) { return source instanceof ExceptionQueuedEventContext; } /** * @see jakarta.faces.context.ExceptionHandler#processEvent(jakarta.faces.event.SystemEvent) */ @Override public void processEvent(SystemEvent event) throws AbortProcessingException { if (event != null) { if (unhandledExceptions == null) { unhandledExceptions = new LinkedList<>(); } unhandledExceptions.add((ExceptionQueuedEvent) event); } } /** * @see ExceptionHandler#getRootCause(Throwable) */ @Override public Throwable getRootCause(Throwable t) { if (t == null) { return null; } if (shouldUnwrap(t.getClass())) { Throwable root = t.getCause(); if (root != null) { Throwable tmp = getRootCause(root); if (tmp == null) { return root; } else { return tmp; } } else { return t; } } return t; } /** * @see jakarta.faces.context.ExceptionHandler#getUnhandledExceptionQueuedEvents() */ @Override public Iterable getUnhandledExceptionQueuedEvents() { return unhandledExceptions != null ? unhandledExceptions : Collections.emptyList(); } /** * @return * * @see jakarta.faces.context.ExceptionHandler#getHandledExceptionQueuedEvents() */ @Override public Iterable getHandledExceptionQueuedEvents() { return handledExceptions != null ? handledExceptions : Collections.emptyList(); } // --------------------------------------------------------- Private Methods /** * @param c Throwable implementation class * * @return true if c is FacesException.class or ELException.class */ private boolean shouldUnwrap(Class c) { return FacesException.class.equals(c) || ELException.class.equals(c); } private boolean isRethrown(Throwable t, boolean isBeforeOrAfterPhase) { return !isBeforeOrAfterPhase && !(t instanceof AbortProcessingException) && !(t instanceof UpdateModelException); } private void log(ExceptionQueuedEventContext exceptionContext) { Throwable t = exceptionContext.getException(); UIComponent c = exceptionContext.getComponent(); if (t instanceof UpdateModelException) { FacesContext context = FacesContext.getCurrentInstance(); FacesMessage message = ((UpdateModelException) t).getFacesMessage(); LOGGER.log(Level.SEVERE, message.getSummary(), t.getCause()); context.addMessage(c.getClientId(context), message); } else { boolean beforePhase = exceptionContext.inBeforePhase(); boolean afterPhase = exceptionContext.inAfterPhase(); PhaseId phaseId = exceptionContext.getPhaseId(); String key = getLoggingKey(beforePhase, afterPhase); if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, key, new Object[] { t.getClass().getName(), phaseId.toString(), c != null ? c.getClientId(exceptionContext.getContext()) : "", t.getMessage() }); LOGGER.log(Level.SEVERE, t.getMessage(), t); } } } private String getLoggingKey(boolean beforePhase, boolean afterPhase) { if (beforePhase) { return LOG_BEFORE_KEY; } else if (afterPhase) { return LOG_AFTER_KEY; } else { return LOG_KEY; } } } // END PreJsf2ExceptionHandler }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy