Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.cache.LoadingCacheUtils.getCacheValue;
 import static org.jboss.weld.util.reflection.Reflections.cast;
 
 import java.util.Set;
 
 
 
Provides event-related operations such sa observer method resolution and event delivery.

Author(s):
Jozef Hartinger
David Allen
 
 public class ObserverNotifier {

    

Parameters:
resolver
services
strict indicates whether event type should be performed or not
Returns:
ObserverNotifier instance
 
     public static ObserverNotifier of(String contextIdTypeSafeObserverResolver resolverServiceRegistry servicesboolean strict) {
         if (services.contains(TransactionServices.class)) {
             return new TransactionalObserverNotifier(contextIdresolverservicesstrict);
         } else {
             return new ObserverNotifier(resolverservicesstrict);
         }
     }
 
     private static final RuntimeException NO_EXCEPTION_MARKER = new RuntimeException();
 
     private final TypeSafeObserverResolver resolver;
     private final SharedObjectCache sharedObjectCache;
     private final boolean strict;
     protected final CurrentEventMetadata currentEventMetadata;
 
     protected ObserverNotifier(TypeSafeObserverResolver resolverServiceRegistry servicesboolean strict) {
         this. = resolver;
         this. = services.get(SharedObjectCache.class);
         this. = strict;
         this. = services.get(CurrentEventMetadata.class);
         if (strict) {
              = CacheBuilder.newBuilder().build(new EventTypeCheck());
         } else {
              = null// not necessary
         }
     }
 
     public <T> ResolvedObservers<T> resolveObserverMethods(T eventAnnotation... bindings) {
         checkEventObjectType(event);
         return this.<T>resolveObserverMethods(buildEventResolvable(event.getClass(), bindings));
     }
 
     public <T> ResolvedObservers<T> resolveObserverMethods(Type eventTypeSet<Annotationqualifiers) {
         checkEventObjectType(eventType);
         return this.<T>resolveObserverMethods(buildEventResolvable(eventTypequalifiers));
    }
    public <T> ResolvedObservers<T> resolveObserverMethods(Resolvable resolvable) {
        return cast(.resolve(resolvabletrue));
    }
    public void fireEvent(Object eventEventMetadata metadataAnnotation... qualifiers) {
        fireEvent(event.getClass(), eventmetadataqualifiers);
    }
    public void fireEvent(Type eventTypeObject eventAnnotation... qualifiers) {
        fireEvent(eventTypeeventnullqualifiers);
    }
    public void fireEvent(Type eventTypeObject eventEventMetadata metadataAnnotation... qualifiers) {
        checkEventObjectType(eventType);
        // we use the array of qualifiers for resolution so that we can catch duplicate qualifiers
        notify(resolveObserverMethods(buildEventResolvable(eventTypequalifiers)), eventmetadata);
    }
    public void fireEvent(Object eventResolvable resolvable) {
        checkEventObjectType(event);
        notify(resolveObserverMethods(resolvable), eventnull);
    }
    public Resolvable buildEventResolvable(Type eventTypeSet<Annotationqualifiers) {
        // We can always cache as this is only ever called by Weld where we avoid non-static inner classes for annotation literals
        Set<TypetypeClosure = .getTypeClosureHolder(eventType).get();
        return new ResolvableBuilder(.getMetaAnnotationStore())
            .addTypes(typeClosure)
            .addType(Object.class)
            .addQualifiers(qualifiers)
            .create();
    }
    public Resolvable buildEventResolvable(Type eventTypeAnnotation... qualifiers) {
        // We can always cache as this is only ever called by Weld where we avoid non-static inner classes for annotation literals
        return new ResolvableBuilder(.getMetaAnnotationStore())
            .addTypes(.getTypeClosureHolder(eventType).get())
            .addType(Object.class)
            .addQualifiers(qualifiers)
            .create();
    }
    public void clear() {
        .clear();
        if ( != null) {
            .invalidateAll();
        }
    }
    public void checkEventObjectType(Object event) {
        checkEventObjectType(event.getClass());
    }
    public void checkEventObjectType(Type eventType) {
        if () {
            RuntimeException exception = getCacheValue(eventType);
            if (exception != ) {
                throw exception;
            }
        }
    }
    private static class EventTypeCheck extends CacheLoader<TypeRuntimeException> {
        @Override
        public RuntimeException load(Type eventType) {
            Type resolvedType = Types.getCanonicalType(eventType);
            /*
             * If the runtime type of the event object contains a type variable, the container must throw an IllegalArgumentException.
             */
            if (Types.containsUnresolvedTypeVariableOrWildcard(resolvedType)) {
                return ..typeParameterNotAllowedInEventType(eventType);
            }
            /*
             * If the runtime type of the event object is assignable to the type of a container lifecycle event, IllegalArgumentException
             * is thrown.
             */
            Class<?> resolvedClass = Reflections.getRawType(eventType);
            for (Class<?> containerEventType : .) {
                if (containerEventType.isAssignableFrom(resolvedClass)) {
                    return ..eventTypeNotAllowed(eventType);
                }
            }
            return ;
        }
    }
    public <T> void notify(Set<ObserverMethod<? super T>> observers, T eventEventMetadata metadata) {
        notify(ResolvedObservers.of(observers), eventmetadata);
    }
    public <T> void notify(ResolvedObservers<T> observers, T eventEventMetadata metadata) {
        if (!observers.isMetadataRequired()) {
            metadata = null;
        }
        notifySyncObservers(observers.getImmediateObservers(), eventmetadata);
        notifyTransactionObservers(observers.getTransactionObservers(), eventmetadata);
    }
    protected <T> void notifySyncObservers(Set<ObserverMethod<? super T>> observers, T eventEventMetadata metadata) {
        if (observers.isEmpty()) {
            return;
        }
        try {
            for (ObserverMethod<? super T> observer : observers) {
                observer.notify(event);
            }
        } finally {
            stack.pop();
        }
    }
    protected <T> void notifyTransactionObservers(Set<ObserverMethod<? super T>> observers, T eventEventMetadata metadata) {
        notifySyncObservers(observerseventmetadata); // no transaction support
    }
New to GrepCode? Check out our FAQ X