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

org.jboss.weld.bootstrap.events.ProcessObserverMethodImpl Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed 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.jboss.weld.bootstrap.events;

import static org.jboss.weld.util.Observers.validateObserverMethod;

import java.lang.reflect.Type;

import jakarta.enterprise.inject.spi.AnnotatedMethod;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ObserverMethod;
import jakarta.enterprise.inject.spi.ProcessObserverMethod;
import jakarta.enterprise.inject.spi.configurator.ObserverMethodConfigurator;

import org.jboss.weld.bootstrap.events.configurator.ObserverMethodConfiguratorImpl;
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Preconditions;

/**
 * Implementation of the event used to notify observers for each observer method that is added.
 *
 * @author David Allen
 * @author Martin Kouba
 */
public class ProcessObserverMethodImpl extends AbstractDefinitionContainerEvent
        implements ProcessObserverMethod {

    public static  ObserverMethod fire(BeanManagerImpl beanManager, AnnotatedMethod beanMethod,
            ObserverMethod observerMethod) {
        return fire(new ProcessObserverMethodImpl<>(beanManager, beanMethod, observerMethod));
    }

    protected static  ObserverMethod fire(ProcessObserverMethodImpl event) {
        event.fire();
        if (event.vetoed) {
            return null;
        }
        return event.observerMethod;
    }

    private final AnnotatedMethod beanMethod;
    private final ObserverMethod initialObserverMethod;
    private ObserverMethodConfiguratorImpl configurator;

    protected ObserverMethod observerMethod;
    protected boolean vetoed;

    // we need this to ensure that configurator and set method are not invoked within one observer
    private boolean observerMethodSet;

    ProcessObserverMethodImpl(BeanManagerImpl beanManager, AnnotatedMethod beanMethod, ObserverMethod observerMethod) {
        this(beanManager, beanMethod, observerMethod, ProcessObserverMethod.class);
    }

    @SuppressWarnings("rawtypes")
    ProcessObserverMethodImpl(BeanManagerImpl beanManager, AnnotatedMethod beanMethod, ObserverMethod observerMethod,
            Class rawType) {
        super(beanManager, rawType, new Type[] { observerMethod.getObservedType(), observerMethod.getBeanClass() });
        this.beanMethod = beanMethod;
        this.initialObserverMethod = observerMethod;
        this.observerMethod = observerMethod;
    }

    public AnnotatedMethod getAnnotatedMethod() {
        checkWithinObserverNotification();
        return beanMethod;
    }

    public ObserverMethod getObserverMethod() {
        checkWithinObserverNotification();
        return observerMethod;
    }

    @Override
    public void setObserverMethod(ObserverMethod observerMethod) {
        if (configurator != null) {
            throw BootstrapLogger.LOG.configuratorAndSetMethodBothCalled(ProcessObserverMethod.class.getSimpleName(),
                    getReceiver());
        }
        Preconditions.checkArgumentNotNull(observerMethod, "observerMethod");
        checkWithinObserverNotification();
        replaceObserverMethod(observerMethod);
        observerMethodSet = true;
    }

    @Override
    public ObserverMethodConfigurator configureObserverMethod() {
        if (observerMethodSet) {
            throw BootstrapLogger.LOG.configuratorAndSetMethodBothCalled(ProcessObserverMethod.class.getSimpleName(),
                    getReceiver());
        }
        checkWithinObserverNotification();
        if (configurator == null) {
            configurator = new ObserverMethodConfiguratorImpl<>(observerMethod, getReceiver());
        }
        BootstrapLogger.LOG.configureObserverMethodCalled(getReceiver(), observerMethod);
        return configurator;
    }

    @Override
    public void veto() {
        checkWithinObserverNotification();
        vetoed = true;
    }

    public boolean isDirty() {
        return observerMethod != initialObserverMethod;
    }

    @Override
    public void postNotify(Extension extension) {
        super.postNotify(extension);
        if (configurator != null) {
            replaceObserverMethod(configurator.complete());
            configurator = null;
        }
        observerMethodSet = false;
    }

    private void replaceObserverMethod(ObserverMethod observerMethod) {
        validateObserverMethod(observerMethod, getBeanManager(), initialObserverMethod);
        this.observerMethod = observerMethod;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy