Skip to content

Commit

Permalink
Refactored initialization of observer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Apr 14, 2012
1 parent af577c9 commit 694bab5
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 61 deletions.
Expand Up @@ -154,21 +154,21 @@ public AbstractBeanDeployer<E> deployBeans() {
}

public AbstractBeanDeployer<E> initializeObserverMethods() {
for (ObserverMethodImpl<?, ?> observer : getEnvironment().getObservers()) {
if (Observers.isObserverMethodEnabled(observer, manager)) {
observer.initialize();
for (ObserverInitializationContext<?, ?> observerInitializer : getEnvironment().getObservers()) {
if (Observers.isObserverMethodEnabled(observerInitializer.getObserver(), manager)) {
observerInitializer.initialize();
}
}
return this;
}

public AbstractBeanDeployer<E> deployObserverMethods() {
// TODO -- why do observers have to be the last?
for (ObserverMethodImpl<?, ?> observer : getEnvironment().getObservers()) {
if (Observers.isObserverMethodEnabled(observer, manager)) {
log.debug(FOUND_OBSERVER_METHOD, observer);
ProcessObserverMethodImpl.fire(manager, observer);
manager.addObserver(observer);
for (ObserverInitializationContext<?, ?> observerInitializer : getEnvironment().getObservers()) {
if (Observers.isObserverMethodEnabled(observerInitializer.getObserver(), manager)) {
log.debug(FOUND_OBSERVER_METHOD, observerInitializer.getObserver());
ProcessObserverMethodImpl.fire(manager, observerInitializer.getObserver());
manager.addObserver(observerInitializer.getObserver());
}
}
return this;
Expand Down Expand Up @@ -236,8 +236,9 @@ protected <X> void createObserverMethods(RIBean<X> declaringBean, EnhancedAnnota
}

protected <T, X> void createObserverMethod(RIBean<X> declaringBean, EnhancedAnnotatedMethod<T, ? super X> method) {
ObserverMethodImpl<T, ? super X> observer = ObserverFactory.create(method, declaringBean, manager);
getEnvironment().addObserverMethod(observer);
ObserverMethodImpl<T, X> observer = ObserverFactory.create(method, declaringBean, manager);
ObserverInitializationContext<T, ? super X> observerInitializer = ObserverInitializationContext.of(observer, method);
getEnvironment().addObserverMethod(observerInitializer);
}

protected <T> ManagedBean<T> createManagedBean(EnhancedAnnotatedType<T> weldClass) {
Expand Down
Expand Up @@ -34,8 +34,8 @@
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.slim.SlimAnnotatedType;
import org.jboss.weld.annotated.slim.backed.BackedAnnotatedType;
import org.jboss.weld.bean.AbstractBean;
Expand All @@ -55,7 +55,6 @@
import org.jboss.weld.bean.builtin.ExtensionBean;
import org.jboss.weld.ejb.EjbDescriptors;
import org.jboss.weld.ejb.InternalEjbDescriptor;
import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resolution.ResolvableBuilder;
Expand Down Expand Up @@ -84,7 +83,7 @@ public static BeanDeployerEnvironment newConcurrentEnvironment(EjbDescriptors ej
Sets.newSetFromMap(new ConcurrentHashMap<ProducerField<?, ?>, Boolean>()),
new ConcurrentHashMap<WeldMethodKey<?, ?>, ProducerMethod<?, ?>>(),
Sets.newSetFromMap(new ConcurrentHashMap<RIBean<?>, Boolean>()),
Sets.newSetFromMap(new ConcurrentHashMap<ObserverMethodImpl<?, ?>, Boolean>()),
Sets.newSetFromMap(new ConcurrentHashMap<ObserverInitializationContext<?, ?>, Boolean>()),
Sets.newSetFromMap(new ConcurrentHashMap<DisposalMethod<?, ?>, Boolean>()),
Sets.newSetFromMap(new ConcurrentHashMap<DisposalMethod<?, ?>, Boolean>()),
Sets.newSetFromMap(new ConcurrentHashMap<DecoratorImpl<?>, Boolean>()),
Expand All @@ -102,7 +101,7 @@ public static BeanDeployerEnvironment newConcurrentEnvironment(EjbDescriptors ej
private final Map<WeldMethodKey<?, ?>, ProducerMethod<?, ?>> producerMethodBeanMap;
private final Set<ProducerField<?, ?>> producerFields;
private final Set<RIBean<?>> beans;
private final Set<ObserverMethodImpl<?, ?>> observers;
private final Set<ObserverInitializationContext<?, ?>> observers;
private final Set<DisposalMethod<?, ?>> allDisposalBeans;
private final Set<DisposalMethod<?, ?>> resolvedDisposalBeans;
private final Set<DecoratorImpl<?>> decorators;
Expand All @@ -121,7 +120,7 @@ protected BeanDeployerEnvironment(
Set<ProducerField<?, ?>> producerFields,
Map<WeldMethodKey<?, ?>, ProducerMethod<?, ?>> producerMethodBeanMap,
Set<RIBean<?>> beans,
Set<ObserverMethodImpl<?, ?>> observers,
Set<ObserverInitializationContext<?, ?>> observers,
Set<DisposalMethod<?, ?>> allDisposalBeans,
Set<DisposalMethod<?, ?>> resolvedDisposalBeans,
Set<DecoratorImpl<?>> decorators,
Expand Down Expand Up @@ -158,7 +157,7 @@ protected BeanDeployerEnvironment(EjbDescriptors ejbDescriptors, BeanManagerImpl
new HashSet<ProducerField<?, ?>>(),
new HashMap<WeldMethodKey<?, ?>, ProducerMethod<?, ?>>(),
new HashSet<RIBean<?>>(),
new HashSet<ObserverMethodImpl<?, ?>>(),
new HashSet<ObserverInitializationContext<?, ?>>(),
new HashSet<DisposalMethod<?, ?>>(),
new HashSet<DisposalMethod<?, ?>>(),
new HashSet<DecoratorImpl<?>>(),
Expand Down Expand Up @@ -302,9 +301,9 @@ public void addDisposesMethod(DisposalMethod<?, ?> bean) {
addNewBeansFromInjectionPoints(bean);
}

public void addObserverMethod(ObserverMethodImpl<?, ?> observer) {
this.observers.add(observer);
addNewBeansFromInjectionPoints(observer.getNewInjectionPoints());
public void addObserverMethod(ObserverInitializationContext<?, ?> observerInitializer) {
this.observers.add(observerInitializer);
addNewBeansFromInjectionPoints(observerInitializer.getObserver().getNewInjectionPoints());
}

public void addNewBeansFromInjectionPoints(AbstractBean<?, ?> bean) {
Expand Down Expand Up @@ -349,7 +348,7 @@ public Set<InterceptorImpl<?>> getInterceptors() {
return Collections.unmodifiableSet(interceptors);
}

public Set<ObserverMethodImpl<?, ?>> getObservers() {
public Set<ObserverInitializationContext<?, ?>> getObservers() {
return Collections.unmodifiableSet(observers);
}

Expand Down
Expand Up @@ -39,7 +39,6 @@
import javax.enterprise.inject.spi.Interceptor;

import org.jboss.weld.bean.RIBean;
import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.executor.IterativeWorkerTaskFactory;
import org.jboss.weld.manager.BeanManagerImpl;
Expand Down Expand Up @@ -106,10 +105,10 @@ protected void doWork(Decorator<?> decorator) {
}

@Override
protected void validateObserverMethods(Iterable<ObserverMethodImpl<?, ?>> observers, final BeanManagerImpl beanManager) {
executor.invokeAllAndCheckForExceptions(new IterativeWorkerTaskFactory<ObserverMethodImpl<?, ?>>(observers) {
protected void doWork(ObserverMethodImpl<?, ?> observerMethod) {
for (InjectionPoint ip : observerMethod.getInjectionPoints()) {
protected void validateObserverMethods(Iterable<ObserverInitializationContext<?, ?>> observers, final BeanManagerImpl beanManager) {
executor.invokeAllAndCheckForExceptions(new IterativeWorkerTaskFactory<ObserverInitializationContext<?, ?>>(observers) {
protected void doWork(ObserverInitializationContext<?, ?> observerMethod) {
for (InjectionPoint ip : observerMethod.getObserver().getInjectionPoints()) {
validateInjectionPoint(ip, beanManager);
}
}
Expand Down
Expand Up @@ -68,12 +68,12 @@ public ExtensionBeanDeployer deployBeans() {
BeanDeployment beanDeployment = DeploymentStructures.getOrCreateBeanDeployment(deployment, beanManager, beanDeployments, contexts, clazz.getJavaClass());

ExtensionBean bean = new ExtensionBean(beanDeployment.getBeanManager(), clazz, extension);
Set<ObserverMethodImpl<?, ?>> observerMethods = new HashSet<ObserverMethodImpl<?, ?>>();
createObserverMethods(bean, beanDeployment.getBeanManager(), clazz, observerMethods);
Set<ObserverInitializationContext<?, ?>> observerMethodInitializers = new HashSet<ObserverInitializationContext<?, ?>>();
createObserverMethods(bean, beanDeployment.getBeanManager(), clazz, observerMethodInitializers);
beanDeployment.getBeanManager().addBean(bean);
for (ObserverMethodImpl<?, ?> observerMethod : observerMethods) {
observerMethod.initialize();
beanDeployment.getBeanManager().addObserver(observerMethod);
for (ObserverInitializationContext<?, ?> observerMethodInitializer : observerMethodInitializers) {
observerMethodInitializer.initialize();
beanDeployment.getBeanManager().addObserver(observerMethodInitializer.getObserver());
}
}
return this;
Expand All @@ -90,15 +90,16 @@ public void addExtension(Metadata<Extension> extension) {
this.extensions.add(extension);
}

protected <X> void createObserverMethods(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedType<? super X> annotatedClass, Set<ObserverMethodImpl<?, ?>> observerMethods) {
protected <X> void createObserverMethods(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedType<? super X> annotatedClass, Set<ObserverInitializationContext<?, ?>> observerMethodInitializers) {
for (EnhancedAnnotatedMethod<?, ? super X> method : Beans.getObserverMethods(annotatedClass)) {
createObserverMethod(declaringBean, beanManager, method, observerMethods);
createObserverMethod(declaringBean, beanManager, method, observerMethodInitializers);
}
}

protected <T, X> void createObserverMethod(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod<T, ? super X> method, Set<ObserverMethodImpl<?, ?>> observerMethods) {
protected <T, X> void createObserverMethod(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod<T, ? super X> method, Set<ObserverInitializationContext<?, ?>> observerMethodInitializers) {
ObserverMethodImpl<T, X> observer = ObserverFactory.create(method, declaringBean, beanManager);
observerMethods.add(observer);
ObserverInitializationContext<T, X> observerMethodInitializer = ObserverInitializationContext.of(observer, method);
observerMethodInitializers.add(observerMethodInitializer);
}

}
@@ -0,0 +1,43 @@
/*
* 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.bootstrap;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.event.ObserverMethodImpl;

public class ObserverInitializationContext<T, X> {

public static <T, X> ObserverInitializationContext<T, X> of(ObserverMethodImpl<T, X> observer, EnhancedAnnotatedMethod<T, ? super X> annotated) {
return new ObserverInitializationContext<T, X>(observer, annotated);
}

private final ObserverMethodImpl<T, X> observer;
private final EnhancedAnnotatedMethod<T, ? super X> annotated;

public ObserverInitializationContext(ObserverMethodImpl<T, X> observer, EnhancedAnnotatedMethod<T, ? super X> annotated) {
this.observer = observer;
this.annotated = annotated;
}

public void initialize() {
observer.initialize(annotated);
}

public ObserverMethodImpl<T, X> getObserver() {
return observer;
}
}
7 changes: 3 additions & 4 deletions impl/src/main/java/org/jboss/weld/bootstrap/Validator.java
Expand Up @@ -103,7 +103,6 @@
import org.jboss.weld.bean.builtin.AbstractBuiltInBean;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.exceptions.AmbiguousResolutionException;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
Expand Down Expand Up @@ -605,9 +604,9 @@ private void validateDisposalMethods(BeanDeployerEnvironment environment) {
}
}

protected void validateObserverMethods(Iterable<ObserverMethodImpl<?, ?>> observers, BeanManagerImpl beanManager) {
for (ObserverMethodImpl<?, ?> omi : observers) {
for (InjectionPoint ip : omi.getInjectionPoints())
protected void validateObserverMethods(Iterable<ObserverInitializationContext<?, ?>> observers, BeanManagerImpl beanManager) {
for (ObserverInitializationContext<?, ?> omi : observers) {
for (InjectionPoint ip : omi.getObserver().getInjectionPoints())
validateInjectionPoint(ip, beanManager);
}
}
Expand Down
24 changes: 6 additions & 18 deletions impl/src/main/java/org/jboss/weld/event/ObserverMethodImpl.java
Expand Up @@ -56,7 +56,6 @@
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.injection.attributes.SpecialParameterInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.Observers;

/**
Expand Down Expand Up @@ -89,8 +88,6 @@ public class ObserverMethodImpl<T, X> implements ObserverMethod<T> {
private final Set<WeldInjectionPoint<?, ?>> injectionPoints;
private final Set<WeldInjectionPoint<?, ?>> newInjectionPoints;

private volatile EnhancedAnnotatedMethod<T, ? super X> enhancedAnnotatedMethod;

/**
* Creates an Observer which describes and encapsulates an observer method
* (8.5).
Expand All @@ -102,7 +99,6 @@ public class ObserverMethodImpl<T, X> implements ObserverMethod<T> {
protected ObserverMethodImpl(final EnhancedAnnotatedMethod<T, ? super X> observer, final RIBean<X> declaringBean, final BeanManagerImpl manager) {
this.beanManager = manager;
this.declaringBean = declaringBean;
this.enhancedAnnotatedMethod = observer;
this.observerMethod = MethodInjectionPoint.ofObserverOrDisposerMethod(observer, declaringBean, manager);
this.eventType = observer.getEnhancedParameters(Observes.class).get(0).getBaseType();
this.id = new StringBuilder().append(ID_PREFIX).append(ID_SEPARATOR)/*.append(manager.getId()).append(ID_SEPARATOR)*/.append(ObserverMethod.class.getSimpleName()).append(ID_SEPARATOR).append(declaringBean.getBeanClass().getName()).append(".").append(observer.getSignature()).toString();
Expand Down Expand Up @@ -136,17 +132,17 @@ protected ObserverMethodImpl(final EnhancedAnnotatedMethod<T, ? super X> observe
* Performs validation of the observer method for compliance with the
* specifications.
*/
private void checkObserverMethod() {
private void checkObserverMethod(EnhancedAnnotatedMethod<T, ? super X> annotated) {
// Make sure exactly one and only one parameter is annotated with Observes
List<?> eventObjects = getEnhancedAnnotated().getEnhancedParameters(Observes.class);
List<?> eventObjects = annotated.getEnhancedParameters(Observes.class);
if (this.reception.equals(Reception.IF_EXISTS) && declaringBean.getScope().equals(Dependent.class)) {
throw new DefinitionException(INVALID_SCOPED_CONDITIONAL_OBSERVER, this);
}
if (eventObjects.size() > 1) {
throw new DefinitionException(MULTIPLE_EVENT_PARAMETERS, this);
}
// Check for parameters annotated with @Disposes
List<?> disposeParams = getEnhancedAnnotated().getEnhancedParameters(Disposes.class);
List<?> disposeParams = annotated.getEnhancedParameters(Disposes.class);
if (disposeParams.size() > 0) {
throw new DefinitionException(INVALID_DISPOSES_PARAMETER, this);
}
Expand All @@ -159,7 +155,7 @@ private void checkObserverMethod() {
throw new DefinitionException(INVALID_INITIALIZER, this);
}
boolean containerLifecycleObserverMethod = Observers.isContainerLifecycleObserverMethod(this);
for (EnhancedAnnotatedParameter<?, ?> parameter : getEnhancedAnnotated().getEnhancedParameters()) {
for (EnhancedAnnotatedParameter<?, ?> parameter : annotated.getEnhancedParameters()) {
if (parameter.isAnnotationPresent(Named.class) && parameter.getAnnotation(Named.class).value().equals("")) {
throw new DefinitionException(NON_FIELD_INJECTION_POINT_CANNOT_USE_NAMED, getMethod());
}
Expand Down Expand Up @@ -206,20 +202,12 @@ public TransactionPhase getTransactionPhase() {
return observerMethod;
}

public EnhancedAnnotatedMethod<T, ? super X> getEnhancedAnnotated() {
return Beans.checkEnhancedAnnotatedAvailable(enhancedAnnotatedMethod);
}

public void cleanupAfterBoot() {
this.enhancedAnnotatedMethod = null;
}

/**
* Completes initialization of the observer and allows derived types to
* override behavior.
*/
public void initialize() {
checkObserverMethod();
public void initialize(EnhancedAnnotatedMethod<T, ? super X> annotated) {
checkObserverMethod(annotated);
}

public void notify(final T event) {
Expand Down
Expand Up @@ -41,11 +41,6 @@ protected TransactionalObserverMethodImpl(EnhancedAnnotatedMethod<T, ? super X>
this.transactionPhase = transactionPhase;
}

@Override
public void initialize() {
super.initialize();
}

@Override
public void notify(T event) {
TransactionServices txs = beanManager.getServices().get(TransactionServices.class);
Expand Down

0 comments on commit 694bab5

Please sign in to comment.