Skip to content

Commit

Permalink
WELD-1430 SerializableContextualFactory is using bean identifier index
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and jharting committed Mar 12, 2014
1 parent 539e509 commit dac507f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
5 changes: 3 additions & 2 deletions impl/src/main/java/org/jboss/weld/bootstrap/WeldStartup.java
Expand Up @@ -251,13 +251,14 @@ private void addImplementationServices(ServiceRegistry services) {
}
services.add(MemberTransformer.class, new MemberTransformer(services.get(ClassTransformer.class)));
services.add(MetaAnnotationStore.class, new MetaAnnotationStore(services.get(ClassTransformer.class)));
services.add(ContextualStore.class, new ContextualStoreImpl(contextId));
BeanIdentifierIndex beanIdentifierIndex = new BeanIdentifierIndex();
services.add(BeanIdentifierIndex.class, beanIdentifierIndex);
services.add(ContextualStore.class, new ContextualStoreImpl(contextId, beanIdentifierIndex));
services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint());
services.add(SLSBInvocationInjectionPoint.class, new SLSBInvocationInjectionPoint());
services.add(CurrentEventMetadata.class, new CurrentEventMetadata());
services.add(SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry());
services.add(MissingDependenciesRegistry.class, new MissingDependenciesRegistry());
services.add(BeanIdentifierIndex.class, new BeanIdentifierIndex());

GlobalObserverNotifierService observerNotificationService = new GlobalObserverNotifierService(services, contextId);
services.add(GlobalObserverNotifierService.class, observerNotificationService);
Expand Down
Expand Up @@ -24,6 +24,7 @@

import org.jboss.weld.Container;
import org.jboss.weld.bean.ForwardingBean;
import org.jboss.weld.serialization.BeanIdentifierIndex;
import org.jboss.weld.serialization.spi.BeanIdentifier;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
Expand All @@ -43,18 +44,19 @@ private SerializableContextualFactory() {
}

@java.lang.SuppressWarnings({ "rawtypes", "unchecked" })
public static <C extends Contextual<I>, I> SerializableContextual<C, I> create(String contextId, C contextual, ContextualStore contextualStore) {
public static <C extends Contextual<I>, I> SerializableContextual<C, I> create(String contextId, C contextual, ContextualStore contextualStore,
BeanIdentifierIndex beanIdentifierIndex) {
if (contextual instanceof Bean) {
if (contextual instanceof PassivationCapable) {
return new PassivationCapableSerializableBean(contextId, Reflections.<Bean> cast(contextual), contextualStore);
return new PassivationCapableSerializableBean(contextId, Reflections.<Bean> cast(contextual), contextualStore, beanIdentifierIndex);
} else {
return new DefaultSerializableBean(contextId, Reflections.<Bean> cast(contextual), contextualStore);
return new DefaultSerializableBean(contextId, Reflections.<Bean> cast(contextual), contextualStore, beanIdentifierIndex);
}
} else {
if (contextual instanceof PassivationCapable) {
return new PassivationCapableSerializableContextual(contextId, contextual, contextualStore);
return new PassivationCapableSerializableContextual(contextId, contextual, contextualStore, beanIdentifierIndex);
} else {
return new DefaultSerializableContextual<C, I>(contextId, contextual, contextualStore);
return new DefaultSerializableContextual<C, I>(contextId, contextual, contextualStore, beanIdentifierIndex);
}
}
}
Expand All @@ -63,30 +65,42 @@ private static final class SerializableContextualHolder<C extends Contextual<I>,

private static final long serialVersionUID = 46941665668478370L;

// A directly serializable contextual
private C serializable;

@SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "A cache which is lazily loaded")
// A cached, transient version of the contextual
private transient C cached;

// the id of a non-serializable, passivation capable contextual
private BeanIdentifier id;
// Only one of the three fields is used at the same time - directly serializable contextual, bean identifier or index
private final C serializable;
private final BeanIdentifier identifier;
private final Integer identifierIndex;

private String contextId;
private final String contextId;

private transient ContextualStore cachedContextualStore;

public SerializableContextualHolder(String contextId, C contextual, ContextualStore contextualStore) {
private transient BeanIdentifierIndex beanIdentifierIndex;

SerializableContextualHolder(String contextId, C contextual, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
this.contextId = contextId;
this.cachedContextualStore = contextualStore;
if (contextual instanceof Serializable) {
// the contextual is serializable, so we can just use it
this.serializable = contextual;
this.identifier = null;
this.identifierIndex = null;
} else {
this.id = getId(contextual, contextualStore);
this.serializable = null;
BeanIdentifier beanIdentifier = getId(contextual, contextualStore);
// The index may not be built yet
Integer idx = beanIdentifierIndex.isBuilt() ? beanIdentifierIndex.getIndex(beanIdentifier) : null;
if (idx != null) {
this.identifierIndex = idx;
this.identifier = null;
} else {
this.identifierIndex = null;
this.identifier = beanIdentifier;
}
}

// cache the contextual
this.cached = contextual;
}
Expand All @@ -102,6 +116,13 @@ protected ContextualStore getContextualStore() {
return this.cachedContextualStore;
}

protected BeanIdentifierIndex getBeanIdentifierIndex() {
if (beanIdentifierIndex == null) {
beanIdentifierIndex = Container.instance(contextId).services().get(BeanIdentifierIndex.class);
}
return beanIdentifierIndex;
}

protected C get() {
if (cached == null) {
loadContextual();
Expand All @@ -111,12 +132,14 @@ protected C get() {

private void loadContextual() {
if (serializable != null) {
this.cached = serializable;
} else if (id != null) {
this.cached = getContextualStore().<C, I> getContextual(id);
cached = serializable;
} else if (identifierIndex != null) {
cached = getContextualStore().<C, I> getContextual(getBeanIdentifierIndex().getIdentifier(identifierIndex));
} else if (identifier != null) {
cached = getContextualStore().<C, I> getContextual(identifier);
}
if (this.cached == null) {
throw new IllegalStateException("Error restoring serialized contextual with id " + id);
if (cached == null) {
throw new IllegalStateException("Error restoring serialized contextual with id " + identifier);
}
}

Expand All @@ -128,8 +151,8 @@ private abstract static class AbstractSerializableBean<B extends Bean<I>, I> ext

private final SerializableContextualHolder<B, I> holder;

AbstractSerializableBean(String contextId, B bean, ContextualStore contextualStore) {
this.holder = new SerializableContextualHolder<B, I>(contextId, bean, contextualStore);
AbstractSerializableBean(String contextId, B bean, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
this.holder = new SerializableContextualHolder<B, I>(contextId, bean, contextualStore, beanIdentifierIndex);
}

@Override
Expand Down Expand Up @@ -166,8 +189,8 @@ private abstract static class AbstractSerializableContextual<C extends Contextua

private final SerializableContextualHolder<C, I> holder;

AbstractSerializableContextual(String contextId, C contextual, ContextualStore contextualStore) {
this.holder = new SerializableContextualHolder<C, I>(contextId, contextual, contextualStore);
AbstractSerializableContextual(String contextId, C contextual, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
this.holder = new SerializableContextualHolder<C, I>(contextId, contextual, contextualStore, beanIdentifierIndex);
}

@Override
Expand Down Expand Up @@ -200,8 +223,8 @@ private static class DefaultSerializableContextual<C extends Contextual<I>, I> e

private static final long serialVersionUID = -5102624795925717767L;

public DefaultSerializableContextual(String contextId, C contextual, ContextualStore contextualStore) {
super(contextId, contextual, contextualStore);
public DefaultSerializableContextual(String contextId, C contextual, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
super(contextId, contextual, contextualStore, beanIdentifierIndex);
}
}

Expand All @@ -211,8 +234,8 @@ private static class PassivationCapableSerializableContextual<C extends Contextu

private static final long serialVersionUID = -2753893863961869301L;

public PassivationCapableSerializableContextual(String contextId, C contextual, ContextualStore contextualStore) {
super(contextId, contextual, contextualStore);
public PassivationCapableSerializableContextual(String contextId, C contextual, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
super(contextId, contextual, contextualStore, beanIdentifierIndex);
}

@Override
Expand All @@ -225,8 +248,8 @@ private static class DefaultSerializableBean<B extends Bean<I>, I> extends Abstr

private static final long serialVersionUID = -8901252027789701049L;

public DefaultSerializableBean(String contextId, B bean, ContextualStore contextualStore) {
super(contextId, bean, contextualStore);
public DefaultSerializableBean(String contextId, B bean, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
super(contextId, bean, contextualStore, beanIdentifierIndex);
}
}

Expand All @@ -235,8 +258,8 @@ private static class PassivationCapableSerializableBean<B extends Bean<I> & Pass

private static final long serialVersionUID = 7458443513156329183L;

public PassivationCapableSerializableBean(String contextId, B bean, ContextualStore contextualStore) {
super(contextId, bean, contextualStore);
public PassivationCapableSerializableBean(String contextId, B bean, ContextualStore contextualStore, BeanIdentifierIndex beanIdentifierIndex) {
super(contextId, bean, contextualStore, beanIdentifierIndex);
}

@Override
Expand Down
Expand Up @@ -118,15 +118,19 @@ public int compare(BeanIdentifier o1, BeanIdentifier o2) {
reverseIndex = builder.build();
}

/**
*
* @return <code>true</code> if the index is built, <code>false</code> otherwise
*/
public boolean isBuilt() {
return index != null;
}

@Override
public void cleanup() {
index = null;
}

private boolean isBuilt() {
return index != null;
}

private void checkIsBuilt() {
if (!isBuilt()) {
throw new IllegalStateException("BeanIdentifier index not built!");
Expand Down
Expand Up @@ -57,8 +57,11 @@ public class ContextualStoreImpl implements ContextualStore {

private final String contextId;

public ContextualStoreImpl(String contextId) {
private final BeanIdentifierIndex beanIdentifierIndex;

public ContextualStoreImpl(String contextId, BeanIdentifierIndex beanIdentifierIndex) {
this.contextId = contextId;
this.beanIdentifierIndex = beanIdentifierIndex;
this.idGenerator = new AtomicInteger(0);
this.contextuals = new ConcurrentHashMap<Contextual<?>, BeanIdentifier>();
this.contextualsInverse = new ConcurrentHashMap<BeanIdentifier, Contextual<?>>();
Expand Down Expand Up @@ -132,7 +135,7 @@ public <C extends Contextual<I>, I> SerializableContextual<C, I> getSerializable
if (contextual instanceof SerializableContextual<?, ?>) {
return cast(contextual);
}
return SerializableContextualFactory.create(contextId, Reflections.<C>cast(contextual), this);
return SerializableContextualFactory.create(contextId, Reflections.<C> cast(contextual), this, beanIdentifierIndex);
}

public <C extends Contextual<I>, I> SerializableContextualInstance<C, I> getSerializableContextualInstance(Contextual<I> contextual, I instance, CreationalContext<I> creationalContext) {
Expand Down
Expand Up @@ -18,6 +18,10 @@

import static org.jboss.weld.bootstrap.api.helpers.RegistrySingletonProvider.STATIC_INSTANCE;

import java.util.Collections;

import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.Container;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.ManagedBean;
Expand All @@ -40,6 +44,7 @@
import org.jboss.weld.resources.DefaultResourceLoader;
import org.jboss.weld.resources.ReflectionCacheFactory;
import org.jboss.weld.resources.SharedObjectCache;
import org.jboss.weld.serialization.BeanIdentifierIndex;
import org.jboss.weld.serialization.ContextualStoreImpl;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.testng.Assert;
Expand All @@ -54,11 +59,13 @@ public class AccessibleManagerResolutionTest {

@BeforeMethod
public void beforeMethod() {
BeanIdentifierIndex beanIdentifierIndex = new BeanIdentifierIndex();
beanIdentifierIndex.build(Collections.<Bean<?>>emptySet());
this.typeStore = new TypeStore();
this.classTransformer = new ClassTransformer(typeStore, new SharedObjectCache(), ReflectionCacheFactory.newInstance(typeStore), RegistrySingletonProvider.STATIC_INSTANCE);
this.services = new SimpleServiceRegistry();
this.services.add(MetaAnnotationStore.class, new MetaAnnotationStore(classTransformer));
this.services.add(ContextualStore.class, new ContextualStoreImpl(STATIC_INSTANCE));
this.services.add(ContextualStore.class, new ContextualStoreImpl(STATIC_INSTANCE, beanIdentifierIndex));
this.services.add(ClassTransformer.class, classTransformer);
this.services.add(SharedObjectCache.class, new SharedObjectCache());
this.services.add(GlobalObserverNotifierService.class, new GlobalObserverNotifierService(services, RegistrySingletonProvider.STATIC_INSTANCE));
Expand Down

0 comments on commit dac507f

Please sign in to comment.