Skip to content

Commit

Permalink
SerializableContextualHolder - optimized way to get the bean identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and jharting committed Mar 3, 2015
1 parent b3ba2a6 commit e743cd2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.enterprise.context.spi.CreationalContext;

import org.jboss.weld.Container;
import org.jboss.weld.bean.CommonBean;
import org.jboss.weld.bean.WrappedContextual;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.context.api.ContextualInstance;
Expand All @@ -32,6 +31,7 @@
import org.jboss.weld.logging.ContextLogger;
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.Beans;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

Expand Down Expand Up @@ -190,11 +190,7 @@ protected BeanIdentifier getId(Contextual<?> contextual) {
if (contextual instanceof WrappedContextual<?>) {
contextual = ((WrappedContextual<?>) contextual).delegate();
}
if (contextual instanceof CommonBean<?>) {
// There is not need to call ContextualStore.putIfAbsent() because it's called for all PassivationCapable beans during deployment
return ((CommonBean<?>) contextual).getIdentifier();
}
return serviceRegistry.get(ContextualStore.class).putIfAbsent(contextual);
return Beans.getIdentifier(contextual, serviceRegistry);
}

protected ServiceRegistry getServiceRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
Expand Down Expand Up @@ -107,7 +108,7 @@ private static final class SerializableContextualHolder<C extends Contextual<I>,
}

protected BeanIdentifier getId(C contextual, ContextualStore contextualStore) {
return contextualStore.putIfAbsent(contextual);
return Beans.getIdentifier(contextual, contextualStore);
}

protected ContextualStore getContextualStore() {
Expand Down
43 changes: 43 additions & 0 deletions impl/src/main/java/org/jboss/weld/util/Beans.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Alternative;
Expand Down Expand Up @@ -71,6 +72,7 @@
import org.jboss.weld.bean.RIBean;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.bootstrap.SpecializationAndEnablementRegistry;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.enablement.ModuleEnablement;
import org.jboss.weld.ejb.InternalEjbDescriptor;
import org.jboss.weld.ejb.spi.BusinessInterfaceDescriptor;
Expand All @@ -89,6 +91,8 @@
import org.jboss.weld.resolution.QualifierInstance;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.spi.ClassFileInfo;
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.bytecode.BytecodeUtils;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Formats;
Expand Down Expand Up @@ -677,4 +681,43 @@ static ImmutableSet.Builder<Type> omitIllegalBeanTypes(Set<Type> types, Enhanced
return builder;
}

/**
* @param contextual
* @param contextualStore
* @return the identifier for the given contextual
* @see #getIdentifier(Contextual, ContextualStore, ServiceRegistry)
*/
public static BeanIdentifier getIdentifier(Contextual<?> contextual, ContextualStore contextualStore) {
return getIdentifier(contextual, contextualStore, null);
}

/**
* @param contextual
* @param serviceRegistry
* @return the identifier for the given contextual
* @see #getIdentifier(Contextual, ContextualStore, ServiceRegistry)
*/
public static BeanIdentifier getIdentifier(Contextual<?> contextual, ServiceRegistry serviceRegistry) {
return getIdentifier(contextual, null, serviceRegistry);
}

/**
* A slightly optimized way to get the bean identifier - there is not need to call ContextualStore.putIfAbsent() for passivation capable beans because it's
* already called during bootstrap. See also {@link BeanManagerImpl#addBean(Bean)}.
*
* @param contextual
* @param contextualStore
* @param serviceRegistry
* @return the identifier for the given contextual
*/
private static BeanIdentifier getIdentifier(Contextual<?> contextual, ContextualStore contextualStore, ServiceRegistry serviceRegistry) {
if (contextual instanceof RIBean<?>) {
return ((RIBean<?>) contextual).getIdentifier();
}
if (contextualStore == null) {
contextualStore = serviceRegistry.get(ContextualStore.class);
}
return contextualStore.putIfAbsent(contextual);
}

}

0 comments on commit e743cd2

Please sign in to comment.