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> Set<ObserverMethod<? super T>> resolveObserverMethods(T eventAnnotation... bindings) {
         checkEventObjectType(event);
         return this.<T>resolveObserverMethods(buildEventResolvable(event.getClass(), bindings));
     }
 
     public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(Type eventTypeSet<Annotationqualifiers) {
         checkEventObjectType(eventType);
         return this.<T>resolveObserverMethods(buildEventResolvable(eventTypequalifiers));
     }
 
    public void fireEvent(Object eventAnnotation... qualifiers) {
        fireEvent(event.getClass(), eventqualifiers);
    }
    public void fireEvent(Type eventTypeObject eventAnnotation... qualifiers) {
        checkEventObjectType(eventType);
        // we use the array of qualifiers for resolution so that we can catch duplicate qualifiers
        notifyObservers(eventresolveObserverMethods(buildEventResolvable(eventTypequalifiers)));
    }
    public void fireEvent(Object eventResolvable resolvable) {
        checkEventObjectType(event);
        notifyObservers(eventresolveObserverMethods(resolvable));
    }
    public <T> void fireEvent(Resolvable resolvableEventPacket<T> packet) {
        checkEventObjectType(packet.getType());
        notifyObservers(packetthis.<T>resolveObserverMethods(resolvable));
    }
    public <T> void notifyObservers(final EventPacket<T> eventPacketfinal Set<ObserverMethod<? super T>> observers) {
        .push(eventPacket);
        try {
            for (ObserverMethod<? super T> observer : observers) {
                notifyObserver(eventPacketobserver);
            }
        } finally {
            .pop();
        }
    }
    private <T> void notifyObservers(final T eventfinal Set<ObserverMethod<? super T>> observers) {
        for (ObserverMethod<? super T> observer : observers) {
            notifyObserver(eventobserver);
        }
    }
    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)
            .addQualifierIfAbsent(.)
            .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)
            .addQualifierIfAbsent(.)
            .create();
    }
    public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(Resolvable resolvable) {
        return cast(.resolve(resolvabletrue));
    }
    public void clear() {
        .clear();
        if ( != null) {
            .invalidateAll();
        }
    }
    protected <T> void notifyObserver(final EventPacket<T> eventPacketfinal ObserverMethod<? super T> observer) {
        notifyObserver(eventPacket.getPayload(), observer);
    }
    protected <T> void notifyObserver(final T eventfinal ObserverMethod<? super T> observer) {
        observer.notify(event);
    }
    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 ;
        }
    }
New to GrepCode? Check out our FAQ X