<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.deltaspike.jsf.impl.exception;

import java.lang.annotation.Annotation;
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.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import org.apache.deltaspike.core.api.exception.control.event.ExceptionToCatchEvent;
import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.core.spi.activation.Deactivatable;
import org.apache.deltaspike.core.util.ClassDeactivationUtils;
import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
import org.apache.deltaspike.jsf.api.config.JsfModuleConfig;

public class DeltaSpikeExceptionHandler extends ExceptionHandlerWrapper implements Deactivatable
{
    private final ExceptionHandler wrapped;

    private volatile Boolean initialized;
    
    private Annotation exceptionQualifier;
    private boolean isActivated = true;

    public DeltaSpikeExceptionHandler(ExceptionHandler wrapped)
    {
        this.isActivated = ClassDeactivationUtils.isActivated(getClass());
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped()
    {
        return wrapped;
    }

    @Override
    public void handle() throws FacesException
    {
        if (isActivated)
        {
            init();
            
            FacesContext context = FacesContext.getCurrentInstance();

            if (context.getResponseComplete())
            {
                return;
            }

            Iterable<ExceptionQueuedEvent> exceptionQueuedEvents = getUnhandledExceptionQueuedEvents();
            if (exceptionQueuedEvents != null && exceptionQueuedEvents.iterator() != null)
            {
                Iterator<ExceptionQueuedEvent> iterator = exceptionQueuedEvents.iterator();

                BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();

                while (iterator.hasNext())
                {
                    Throwable throwable = iterator.next().getContext().getException();
                    Throwable rootCause = getRootCause(throwable);

                    ExceptionToCatchEvent event = new ExceptionToCatchEvent(rootCause, exceptionQualifier);

                    beanManager.fireEvent(event);

                    if (event.isHandled())
                    {
                        iterator.remove();
                    }

                    // a handle method might redirect and set responseComplete
                    if (context.getResponseComplete())
                    {
                        break;
                    }
                }
            }
        }

        super.handle();
    }
    
    private synchronized void init()
    {<commit_msg>DELTASPIKE-529 better lazy init<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.deltaspike.jsf.impl.exception;

import java.lang.annotation.Annotation;
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.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import org.apache.deltaspike.core.api.exception.control.event.ExceptionToCatchEvent;
import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.core.spi.activation.Deactivatable;
import org.apache.deltaspike.core.util.ClassDeactivationUtils;
import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
import org.apache.deltaspike.jsf.api.config.JsfModuleConfig;

public class DeltaSpikeExceptionHandler extends ExceptionHandlerWrapper implements Deactivatable
{
    private final ExceptionHandler wrapped;

    private volatile Boolean initialized;
    
    private Annotation exceptionQualifier;
    private boolean isActivated = true;

    public DeltaSpikeExceptionHandler(ExceptionHandler wrapped)
    {
        this.isActivated = ClassDeactivationUtils.isActivated(getClass());
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped()
    {
        return wrapped;
    }

    @Override
    public void handle() throws FacesException
    {
        if (isActivated)
        {
            lazyInit();
            
            FacesContext context = FacesContext.getCurrentInstance();

            if (context.getResponseComplete())
            {
                return;
            }

            Iterable<ExceptionQueuedEvent> exceptionQueuedEvents = getUnhandledExceptionQueuedEvents();
            if (exceptionQueuedEvents != null && exceptionQueuedEvents.iterator() != null)
            {
                Iterator<ExceptionQueuedEvent> iterator = exceptionQueuedEvents.iterator();

                BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();

                while (iterator.hasNext())
                {
                    Throwable throwable = iterator.next().getContext().getException();
                    Throwable rootCause = getRootCause(throwable);

                    ExceptionToCatchEvent event = new ExceptionToCatchEvent(rootCause, exceptionQualifier);

                    beanManager.fireEvent(event);

                    if (event.isHandled())
                    {
                        iterator.remove();
                    }

                    // a handle method might redirect and set responseComplete
                    if (context.getResponseComplete())
                    {
                        break;
                    }
                }
            }
        }

        super.handle();
    }

    private void lazyInit()
    {
        if (this.initialized == null)
        {
            init();
        }
    }

    private synchronized void init()
    {
{'fraction_non_alphanumeric': 0.07540558079169371, 'fraction_numerical': 0.0014276443867618429, 'mean_word_length': 2.5626444752658344, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6446074', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1823, 'n_words': 494}