use of javax.enterprise.inject.spi.ObserverMethod in project tomee by apache.
the class CdiPlugin method defineSessionBean.
@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
final BeanContext bc = findBeanContext(webBeansContext, clazz);
final Class<?> superClass = bc.getManagedClass().getSuperclass();
if (annotatedType.isAnnotationPresent(Specializes.class)) {
if (superClass != Object.class && !isSessionBean(superClass)) {
throw new DefinitionException("You can only specialize another EJB: " + clazz);
}
final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
}
if (!businessLocalInterfaces.isEmpty()) {
throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
}
}
final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<T>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
bc.set(CdiEjbBean.class, bean);
bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
validateDisposeMethods(bean);
validateScope(bean);
final Set<ObserverMethod<?>> observerMethods;
if (bean.isEnabled()) {
observerMethods = new ObserverMethodsBuilder<T>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean);
} else {
observerMethods = new HashSet<>();
}
final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
for (final ProducerMethodBean<?> producerMethod : producerMethods) {
final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
annotatedMethods.put(producerMethod, method);
}
final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
for (final ProducerFieldBean<?> producerField : producerFields) {
if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
throw new DefinitionException("In an EJB all producer fields should be static");
}
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
}
final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
for (final ObserverMethod<?> observerMethod : observerMethods) {
final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
final AnnotatedMethod<?> method = impl.getObserverMethod();
observerMethodsMap.put(observerMethod, method);
}
validateProduceMethods(bean, producerMethods);
validateObserverMethods(bean, observerMethodsMap);
final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
// Fires ProcessManagedBean
final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
beanManager.fireEvent(event, true);
event.setStarted();
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
// Fires ProcessProducerMethod
webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
// Fires ProcessProducerField
webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
// Fire ObservableMethods
webBeansUtil.fireProcessObservableMethodBeanEvent(observerMethodsMap);
webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessObserverMethod event observers for observer methods. Look at logs for further details");
if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
for (final ProducerMethodBean<?> producerMethod : producerMethods) {
beanManager.addBean(producerMethod);
}
for (final ProducerFieldBean<?> producerField : producerFields) {
beanManager.addBean(producerField);
}
}
beanManager.addBean(bean);
return bean;
}
use of javax.enterprise.inject.spi.ObserverMethod in project core by weld.
the class ObserverNotifier method notifyAsyncObservers.
protected <T, U extends T> CompletionStage<U> notifyAsyncObservers(List<ObserverMethod<? super T>> observers, U event, EventMetadata metadata, Executor executor, NotificationOptions options) {
if (executor == null) {
executor = asyncEventExecutor;
}
if (observers.isEmpty()) {
return AsyncEventDeliveryStage.completed(event, executor);
}
// We should always initialize and validate all notification options first
final NotificationMode mode = initModeOption(options.get(WeldNotificationOptions.MODE));
final Long timeout = initTimeoutOption(options.get(WeldNotificationOptions.TIMEOUT));
final Consumer<Runnable> securityContextActionConsumer = securityServices.getSecurityContextAssociator();
final ObserverExceptionHandler exceptionHandler;
CompletableFuture<U> completableFuture;
if (observers.size() > 1 && NotificationMode.PARALLEL.equals(mode)) {
// Attempt to notify async observers in parallel
exceptionHandler = new CollectingExceptionHandler(new CopyOnWriteArrayList<>());
List<CompletableFuture<T>> completableFutures = new ArrayList<>(observers.size());
for (ObserverMethod<? super T> observer : observers) {
completableFutures.add(CompletableFuture.supplyAsync(createSupplier(securityContextActionConsumer, event, metadata, exceptionHandler, false, () -> {
notifyAsyncObserver(observer, event, metadata, exceptionHandler);
}), executor));
}
completableFuture = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[] {})).thenApply((ignoredVoid) -> {
handleExceptions(exceptionHandler);
return event;
});
} else {
// Async observers are notified serially in a single worker thread
exceptionHandler = new CollectingExceptionHandler();
completableFuture = CompletableFuture.supplyAsync(createSupplier(securityContextActionConsumer, event, metadata, exceptionHandler, true, () -> {
for (ObserverMethod<? super T> observer : observers) {
notifyAsyncObserver(observer, event, metadata, exceptionHandler);
}
}), executor);
}
// If NotificationOptionKeys.TIMEOUT is set, we will trigger the counter and use CompletableFuture.anyOf()
if (timeout != null) {
completableFuture = CompletableFuture.anyOf(completableFuture, startTimer(timeout)).thenApply((ignoredObject) -> event);
}
return new AsyncEventDeliveryStage<>(completableFuture, executor);
}
use of javax.enterprise.inject.spi.ObserverMethod in project core by weld.
the class ProbeObserver method notify.
@Override
public void notify(Object event) {
EventMetadata metadata = currentEventMetadata.peek();
if (excludePattern != null && excludePattern.matcher(Formats.formatType(metadata.getType(), false)).matches()) {
ProbeLogger.LOG.eventExcluded(metadata.getType());
return;
}
boolean containerEvent = isContainerEvent(metadata.getQualifiers());
List<ObserverMethod<?>> observers = resolveObservers(metadata, containerEvent);
EventInfo info = new EventInfo(metadata.getType(), metadata.getQualifiers(), event, metadata.getInjectionPoint(), observers, containerEvent, System.currentTimeMillis());
probe.addEvent(info);
}
use of javax.enterprise.inject.spi.ObserverMethod in project tomee by apache.
the class WebappBeanManager method resolveObserverMethods.
@Override
public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(final T event, final EventMetadataImpl metadata) {
final Set<ObserverMethod<? super T>> set = new HashSet<>();
set.addAll(getNotificationManager().resolveObservers(event, metadata, false));
if (isEvent(event)) {
final BeanManagerImpl parentBm = getParentBm();
if (parentBm != null) {
set.addAll(parentBm.getNotificationManager().resolveObservers(event, metadata, false));
}
}
return set;
}
use of javax.enterprise.inject.spi.ObserverMethod in project core by weld.
the class Probe method findUnusedBeans.
private void findUnusedBeans() {
Collection<Bean<?>> beans = idToBean.values();
Collection<ObserverMethod<?>> observers = idToObserver.values();
for (Bean<?> bean : beans) {
BeanKind kind = BeanKind.from(bean);
if (BeanKind.BUILT_IN.equals(kind) || BeanKind.EXTENSION.equals(kind) || BeanKind.DECORATOR.equals(kind) || BeanKind.INTERCEPTOR.equals(kind)) {
continue;
}
if (bean.getName() != null) {
// Is annotated with @Named
continue;
}
if (!(BeanKind.PRODUCER_FIELD.equals(kind) || BeanKind.PRODUCER_METHOD.equals(kind)) && !getDeclaredProducers(bean).isEmpty()) {
// Has declared producers
continue;
}
if (Components.hasDependents(bean, beans, this)) {
// Has direct or potential (Instance<>) dependents
continue;
}
if (hasDeclaredObserversOrIsInjectedIntoObserver(bean, observers)) {
continue;
}
if (isInjectedIntoDisposer(bean, beans)) {
continue;
}
unusedBeans.add(bean);
}
}
Aggregations