Skip to content

Commit

Permalink
WELD-1917 AbstractCDI - cache Instance by default
Browse files Browse the repository at this point in the history
- make AbstractCDI.getInstance() protected so that subclasses are able to override the default behavior
  • Loading branch information
mkouba committed Apr 13, 2015
1 parent cec77ac commit dd0d9c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 24 additions & 4 deletions impl/src/main/java/org/jboss/weld/AbstractCDI.java
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld;

import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;
import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.annotation.Annotation;
Expand All @@ -29,12 +30,16 @@

import org.jboss.weld.bean.builtin.BeanManagerProxy;
import org.jboss.weld.logging.BeanManagerLogger;
import org.jboss.weld.manager.BeanManagerImpl;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;

/**
* Abstract implementation of CDI which forwards all Instance methods to a delegate. Furthermore, it allows the calling class to be identified
* using the {@link #getCallingClassName()} method.
* Abstract implementation of CDI which forwards all Instance methods to a delegate. Furthermore, it allows the calling class to be identified using the
* {@link #getCallingClassName()} method.
*
* @author Jozef Hartinger
*
Expand All @@ -46,13 +51,22 @@ public abstract class AbstractCDI<T> extends CDI<T> {
// used for caller detection
protected final Set<String> knownClassNames;

private final LoadingCache<BeanManagerImpl, Instance<T>> instanceCache;

public AbstractCDI() {
ImmutableSet.Builder<String> names = ImmutableSet.builder();
for (Class<?> clazz = getClass(); clazz != CDI.class; clazz = clazz.getSuperclass()) {
names.add(clazz.getName());
}
names.add(Unmanaged.class.getName());
this.knownClassNames = names.build();
this.instanceCache = CacheBuilder.newBuilder().build(new CacheLoader<BeanManagerImpl, Instance<T>>() {

@Override
public Instance<T> load(BeanManagerImpl beanManager) throws Exception {
return cast(beanManager.getInstance(beanManager.createCreationalContext(null)));
}
});
}

@Override
Expand Down Expand Up @@ -113,7 +127,13 @@ protected String getCallingClassName() {
throw BeanManagerLogger.LOG.unableToIdentifyBeanManager();
}

private Instance<T> getInstance() {
return cast(BeanManagerProxy.unwrap(getBeanManager()).instance());
/**
* Subclasses are allowed to override the default behavior, i.e. to cache instance per BeanManager.
*
* @return the {@link Instance} the relevant calls are delegated to
*/
protected Instance<T> getInstance() {
return getCastCacheValue(instanceCache, BeanManagerProxy.unwrap(getBeanManager()));
}

}
Expand Up @@ -1390,6 +1390,10 @@ public Instance<Object> instance() {
return InstanceImpl.of(InstanceInjectionPoint.INSTANCE, createCreationalContext(null), this);
}

public <T> Instance<Object> getInstance(CreationalContext<?> ctx) {
return cast(InstanceImpl.of(InstanceInjectionPoint.INSTANCE, ctx, this));
}

@Override
public <T> BeanAttributes<T> createBeanAttributes(AnnotatedType<T> type) {
EnhancedAnnotatedType<T> clazz = services.get(ClassTransformer.class).getEnhancedAnnotatedType(type, getId());
Expand Down

0 comments on commit dd0d9c1

Please sign in to comment.