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

org.jboss.weld.event.ObserverNotifier Maven / Gradle / Ivy

There is a newer version: 6.0.0.Beta4
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, 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.event;

import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;

import javax.enterprise.inject.spi.ObserverMethod;

import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.literal.AnyLiteral;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.resolution.Resolvable;
import org.jboss.weld.resolution.ResolvableBuilder;
import org.jboss.weld.resolution.TypeSafeObserverResolver;
import org.jboss.weld.resources.SharedObjectCache;
import org.jboss.weld.transaction.spi.TransactionServices;
import org.jboss.weld.util.Observers;

/**
 * Provides event-related operations such sa observer method resolution and event delivery.
 *
 * @author Jozef Hartinger
 * @author David Allen
 *
 */
public class ObserverNotifier {

    public static ObserverNotifier of(TypeSafeObserverResolver resolver, ServiceRegistry services) {
        if (services.contains(TransactionServices.class)) {
            return new TransactionalObserverNotifier(resolver, services);
        } else {
            return new ObserverNotifier(resolver, services);
        }
    }

    private final TypeSafeObserverResolver resolver;
    private final SharedObjectCache sharedObjectCache;
    private final MetaAnnotationStore store;

    protected ObserverNotifier(TypeSafeObserverResolver resolver, ServiceRegistry services) {
        this.resolver = resolver;
        this.sharedObjectCache = services.get(SharedObjectCache.class);
        this.store = services.get(MetaAnnotationStore.class);
    }

    public  Set> resolveObserverMethods(T event, Annotation... bindings) {
        Observers.checkEventObjectType(sharedObjectCache, event);
        return this.resolveObserverMethods(event.getClass(), bindings);
    }

    public void fireEvent(Object event, Annotation... qualifiers) {
        fireEvent(event.getClass(), event, qualifiers);
    }

    public void fireEvent(Type eventType, Object event, Annotation... qualifiers) {
        Observers.checkEventObjectType(sharedObjectCache, event);
        notifyObservers(event, resolveObserverMethods(eventType, qualifiers));
    }

    public void fireEvent(Type eventType, Object event, Set qualifiers) {
        Observers.checkEventObjectType(sharedObjectCache, event);
        notifyObservers(event, resolveObserverMethods(eventType, qualifiers));
    }

    private  void notifyObservers(final T event, final Set> observers) {
        for (ObserverMethod observer : observers) {
            notifyObserver(event, observer);
        }
    }

    public  Set> resolveObserverMethods(Type eventType, Annotation... qualifiers) {
        // We can always cache as this is only ever called by Weld where we avoid non-static inner classes for annotation literals
        Resolvable resolvable = new ResolvableBuilder(store)
            .addTypes(sharedObjectCache.getTypeClosure(eventType))
            .addType(Object.class)
            .addQualifiers(qualifiers)
            .addQualifierIfAbsent(AnyLiteral.INSTANCE)
            .create();
        return cast(resolver.resolve(resolvable, true));
    }

    public  Set> resolveObserverMethods(Type eventType, Set qualifiers) {
        // We can always cache as this is only ever called by Weld where we avoid non-static inner classes for annotation literals
        Set typeClosure = sharedObjectCache.getTypeClosure(eventType);
        Resolvable resolvable = new ResolvableBuilder(store)
            .addTypes(typeClosure)
            .addType(Object.class)
            .addQualifiers(qualifiers)
            .addQualifierIfAbsent(AnyLiteral.INSTANCE)
            .create();
        return cast(resolver.resolve(resolvable, true));
    }

    public void clear() {
        resolver.clear();
    }

    protected  void notifyObserver(final T event, final ObserverMethod observer) {
        observer.notify(event);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy