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

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

There is a newer version: 3.0.0.Alpha1
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 java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.enterprise.inject.spi.ObserverMethod;
import javax.servlet.ServletContextEvent;

import org.jboss.weld.bootstrap.api.BootstrapService;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.config.WeldConfiguration;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.resolution.TypeSafeObserverResolver;
import org.jboss.weld.util.Function;
import org.jboss.weld.util.collections.Iterators;

/**
 * Hosts a {@link ObserverNotifier} that uses the global {@link TypeSafeObserverResolver} which has access to every enabled
 * observer method in the deployment. The underlying {@link ObserverNotifier} should be used every time an event is fired, except for
 * special cases such as {@link ServletContextEvent}, where the event is only fired to BDAs accessible from the web archive.
 *
 * @author Jozef Hartinger
 *
 */
public class GlobalObserverNotifierService implements BootstrapService {

    private static class BeanManagerToObserverMethodIterable implements Function>> {
        @Override
        public Iterator> apply(BeanManagerImpl manager) {
            return manager.getObservers().iterator();
        }
    }

    private final Set beanManagers;
    private final ObserverNotifier globalLenientObserverNotifier;
    private final ObserverNotifier globalStrictObserverNotifier;

    public GlobalObserverNotifierService(ServiceRegistry services, String contextId) {
        this.beanManagers = new CopyOnWriteArraySet();
        TypeSafeObserverResolver resolver = new TypeSafeObserverResolver(services.get(MetaAnnotationStore.class),
                createGlobalObserverMethodIterable(beanManagers), services.get(WeldConfiguration.class));
        this.globalLenientObserverNotifier = ObserverNotifier.of(contextId, resolver, services, false);
        this.globalStrictObserverNotifier = ObserverNotifier.of(contextId, resolver, services, true);
    }

    private static Iterable> createGlobalObserverMethodIterable(final Set beanManagers) {
        return new Iterable>() {
            @Override
            public Iterator> iterator() {
                Iterator>> observerMethodIterators = Iterators.transform(beanManagers.iterator(), new BeanManagerToObserverMethodIterable());
                return Iterators.concat(observerMethodIterators);
            }
        };
    }

    public void registerBeanManager(BeanManagerImpl manager) {
        this.beanManagers.add(manager);
    }

    public ObserverNotifier getGlobalLenientObserverNotifier() {
        return globalLenientObserverNotifier;
    }

    public ObserverNotifier getGlobalStrictObserverNotifier() {
        return globalStrictObserverNotifier;
    }

    public Iterable> getAllObserverMethods() {
        return createGlobalObserverMethodIterable(beanManagers);
    }

    @Override
    public void cleanupAfterBoot() {
        this.globalStrictObserverNotifier.clear();
        this.globalLenientObserverNotifier.clear();
    }

    @Override
    public void cleanup() {
        cleanupAfterBoot();
        this.beanManagers.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy