Skip to content

Commit

Permalink
WELD-1291 Get rid of computing maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and jharting committed May 14, 2013
1 parent 93aff4b commit ab2975d
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 311 deletions.
18 changes: 9 additions & 9 deletions impl/src/main/java/org/jboss/weld/Weld.java
Expand Up @@ -22,7 +22,6 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.CDI;
Expand All @@ -34,8 +33,9 @@
import org.jboss.weld.logging.messages.BeanManagerMessage;
import org.jboss.weld.manager.BeanManagerImpl;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
* Provides convenient way to access the CDI container.
Expand All @@ -45,13 +45,13 @@
*/
public class Weld extends CDI<Object> {

private class ClassNameToBeanManager implements Function<String, BeanManagerProxy> {
private class ClassNameToBeanManager extends CacheLoader<String, BeanManagerProxy> {

/**
* Determines the correct {@link BeanManagerImpl} based on a class name of the caller.
*/
@Override
public BeanManagerProxy apply(String callerClassName) {
public BeanManagerProxy load(String callerClassName) {
return new BeanManagerProxy(findBeanManager(callerClassName));
}

Expand Down Expand Up @@ -79,12 +79,12 @@ public BeanManagerImpl findBeanManager(String callerClassName) {
}
}

private final ConcurrentMap<String, BeanManagerProxy> beanManagers;
private final LoadingCache<String, BeanManagerProxy> beanManagers;
// used for caller detection
private final Set<String> subclassNames;

public Weld() {
beanManagers = new MapMaker().weakValues().makeComputingMap(new ClassNameToBeanManager());
beanManagers = CacheBuilder.newBuilder().weakValues().build(new ClassNameToBeanManager());
Set<String> names = new HashSet<String>();
for (Class<?> clazz = getClass(); clazz != CDI.class; clazz = clazz.getSuperclass()) {
names.add(clazz.getName());
Expand Down Expand Up @@ -112,7 +112,7 @@ public BeanManagerProxy getBeanManager() {
if (state.equals(ContainerState.STOPPED) || state.equals(ContainerState.SHUTDOWN)) {
throw new IllegalStateException(BeanManagerMessage.BEAN_MANAGER_NOT_AVAILABLE);
}
return beanManagers.get(getCallingClassName());
return beanManagers.getUnchecked(getCallingClassName());
}

/**
Expand Down Expand Up @@ -178,7 +178,7 @@ public String toString() {
}

public void cleanup() {
beanManagers.clear();
beanManagers.invalidateAll();
}

@Override
Expand Down
Expand Up @@ -21,23 +21,23 @@

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.jboss.weld.bootstrap.api.Service;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.MapMaker;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

public class SlimAnnotatedTypeStoreImpl implements SlimAnnotatedTypeStore, Service {

private final ConcurrentMap<Class<?>, Set<SlimAnnotatedType<?>>> typesByClass;
private final LoadingCache<Class<?>, Set<SlimAnnotatedType<?>>> typesByClass;

public SlimAnnotatedTypeStoreImpl() {
this.typesByClass = new MapMaker().makeComputingMap(new Function<Class<?>, Set<SlimAnnotatedType<?>>>() {
this.typesByClass = CacheBuilder.newBuilder().build(new CacheLoader<Class<?>, Set<SlimAnnotatedType<?>>>() {
@Override
public Set<SlimAnnotatedType<?>> apply(Class<?> input) {
public Set<SlimAnnotatedType<?>> load(Class<?> input) {
return new CopyOnWriteArraySet<SlimAnnotatedType<?>>();
}
});
Expand All @@ -55,16 +55,16 @@ public <X> SlimAnnotatedType<X> get(Class<X> type, String suffix) {

@Override
public <X> Set<SlimAnnotatedType<X>> get(Class<X> type) {
return cast(Collections.unmodifiableSet(typesByClass.get(type)));
return cast(Collections.unmodifiableSet(typesByClass.getUnchecked(type)));
}

@Override
public <X> void put(SlimAnnotatedType<X> type) {
typesByClass.get(type.getJavaClass()).add(type);
typesByClass.getUnchecked(type.getJavaClass()).add(type);
}

@Override
public void cleanup() {
typesByClass.clear();
typesByClass.invalidateAll();
}
}
Expand Up @@ -21,11 +21,10 @@
import static org.jboss.weld.logging.messages.BeanMessage.BEAN_ID_CREATION_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.CREATED_NEW_CLIENT_PROXY_TYPE;
import static org.jboss.weld.logging.messages.BeanMessage.LOOKED_UP_CLIENT_PROXY;
import static org.jboss.weld.util.reflection.Reflections.cast;
import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;

import java.lang.reflect.Type;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import javax.enterprise.inject.spi.Bean;

Expand All @@ -37,8 +36,9 @@
import org.jboss.weld.util.Proxies.TypeInfo;
import org.slf4j.cal10n.LocLogger;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
* A proxy pool for holding scope adaptors (client proxies)
Expand All @@ -50,9 +50,9 @@ public class ClientProxyProvider {
private static final LocLogger log = loggerFactory().getLogger(BEAN);

private static final Object BEAN_NOT_PROXYABLE_MARKER = new Object();
private static final Function<Bean<Object>, Object> CREATE_BEAN_TYPE_CLOSURE_CLIENT_PROXY = new Function<Bean<Object>, Object>() {
private static final CacheLoader<Bean<Object>, Object> CREATE_BEAN_TYPE_CLOSURE_CLIENT_PROXY = new CacheLoader<Bean<Object>, Object>() {
@Override
public Object apply(Bean<Object> from) {
public Object load(Bean<Object> from) {
if (Proxies.isTypesProxyable(from)) {
return createClientProxy(from);
} else {
Expand Down Expand Up @@ -109,9 +109,9 @@ public boolean equals(Object obj) {
}
}

private static final Function<RequestedTypeHolder, Object> CREATE_REQUESTED_TYPE_CLOSURE_CLIENT_PROXY = new Function<ClientProxyProvider.RequestedTypeHolder, Object>() {
private static final CacheLoader<RequestedTypeHolder, Object> CREATE_REQUESTED_TYPE_CLOSURE_CLIENT_PROXY = new CacheLoader<ClientProxyProvider.RequestedTypeHolder, Object>() {
@Override
public Object apply(RequestedTypeHolder input) {
public Object load(RequestedTypeHolder input) {
Set<Type> requestedTypeClosure = Container.instance().services().get(SharedObjectCache.class).getTypeClosureHolder(input.requestedType).get();
if (Proxies.isTypesProxyable(requestedTypeClosure)) {
return createClientProxy(input.bean, requestedTypeClosure);
Expand All @@ -126,15 +126,16 @@ public Object apply(RequestedTypeHolder input) {
*
* @author Nicklas Karlsson
*/
private final ConcurrentMap<Bean<Object>, Object> beanTypeClosureProxyPool;
private final ConcurrentMap<RequestedTypeHolder, Object> requestedTypeClosureProxyPool;
private final LoadingCache<Bean<Object>, Object> beanTypeClosureProxyPool;
private final LoadingCache<RequestedTypeHolder, Object> requestedTypeClosureProxyPool;

/**
* Constructor
*/
public ClientProxyProvider() {
this.beanTypeClosureProxyPool = new MapMaker().makeComputingMap(CREATE_BEAN_TYPE_CLOSURE_CLIENT_PROXY);
this.requestedTypeClosureProxyPool = new MapMaker().makeComputingMap(CREATE_REQUESTED_TYPE_CLOSURE_CLIENT_PROXY);
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
this.beanTypeClosureProxyPool = cacheBuilder.build(CREATE_BEAN_TYPE_CLOSURE_CLIENT_PROXY);
this.requestedTypeClosureProxyPool = cacheBuilder.build(CREATE_REQUESTED_TYPE_CLOSURE_CLIENT_PROXY);
}

/**
Expand Down Expand Up @@ -167,7 +168,7 @@ private static <T> T createClientProxy(Bean<T> bean, Set<Type> types) {
}

public <T> T getClientProxy(final Bean<T> bean) {
T proxy = cast(beanTypeClosureProxyPool.get(bean));
T proxy = getCastCacheValue(beanTypeClosureProxyPool, bean);
if (proxy == BEAN_NOT_PROXYABLE_MARKER) {
throw Proxies.getUnproxyableTypesException(bean);
}
Expand All @@ -185,14 +186,14 @@ public <T> T getClientProxy(final Bean<T> bean) {
*/
public <T> T getClientProxy(final Bean<T> bean, Type requestedType) {
// let's first try to use the proxy that implements all the bean types
T proxy = cast(beanTypeClosureProxyPool.get(bean));
T proxy = getCastCacheValue(beanTypeClosureProxyPool, bean);
if (proxy == BEAN_NOT_PROXYABLE_MARKER) {
/*
* the bean may have a type that is not proxyable - this is not a problem as long as the unproxyable
* type is not in the type closure of the requested type
* https://issues.jboss.org/browse/WELD-1052
*/
proxy = cast(requestedTypeClosureProxyPool.get(new RequestedTypeHolder(requestedType, bean)));
proxy = getCastCacheValue(requestedTypeClosureProxyPool, new RequestedTypeHolder(requestedType, bean));
if (proxy == BEAN_NOT_PROXYABLE_MARKER) {
throw Proxies.getUnproxyableTypeException(requestedType);
}
Expand All @@ -213,8 +214,8 @@ public String toString() {
}

public void clear() {
this.beanTypeClosureProxyPool.clear();
this.requestedTypeClosureProxyPool.clear();
this.beanTypeClosureProxyPool.invalidateAll();
this.requestedTypeClosureProxyPool.invalidateAll();
}

}
15 changes: 9 additions & 6 deletions impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployer.java
Expand Up @@ -19,12 +19,12 @@
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BootstrapMessage.BEAN_IS_BOTH_INTERCEPTOR_AND_DECORATOR;
import static org.jboss.weld.logging.messages.BootstrapMessage.IGNORING_CLASS_DUE_TO_LOADING_ERROR;
import static org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue;
import static org.slf4j.ext.XLogger.Level.DEBUG;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

Expand Down Expand Up @@ -69,6 +69,8 @@
import org.slf4j.cal10n.LocLogger;
import org.slf4j.ext.XLogger;

import com.google.common.cache.LoadingCache;

/**
* @author Pete Muir
* @author Jozef Hartinger
Expand Down Expand Up @@ -196,7 +198,7 @@ public void registerAnnotatedTypes() {
}

public void createClassBeans() {
Map<Class<?>, Set<SlimAnnotatedType<?>>> otherWeldClasses = Multimaps.newConcurrentSetMultimap();
LoadingCache<Class<?>, Set<SlimAnnotatedType<?>>> otherWeldClasses = Multimaps.newConcurrentSetMultimap();

for (SlimAnnotatedType<?> annotatedType : getEnvironment().getAnnotatedTypes()) {
createClassBean(annotatedType, otherWeldClasses);
Expand All @@ -207,8 +209,8 @@ public void createClassBeans() {
continue;
}
if (ejbDescriptor.isSingleton() || ejbDescriptor.isStateful() || ejbDescriptor.isStateless()) {
if (otherWeldClasses.containsKey(ejbDescriptor.getBeanClass())) {
for (SlimAnnotatedType<?> annotatedType : otherWeldClasses.get(ejbDescriptor.getBeanClass())) {
if (otherWeldClasses.getIfPresent(ejbDescriptor.getBeanClass()) != null) {
for (SlimAnnotatedType<?> annotatedType : getCacheValue(otherWeldClasses, ejbDescriptor.getBeanClass())) {
EnhancedAnnotatedType<?> weldClass = classTransformer.getEnhancedAnnotatedType(annotatedType);
createSessionBean(ejbDescriptor, Reflections.<EnhancedAnnotatedType> cast(weldClass));
}
Expand All @@ -219,7 +221,8 @@ public void createClassBeans() {
}
}

protected void createClassBean(SlimAnnotatedType<?> annotatedType, Map<Class<?>, Set<SlimAnnotatedType<?>>> otherWeldClasses) {
protected void createClassBean(SlimAnnotatedType<?> annotatedType,
LoadingCache<Class<?>, Set<SlimAnnotatedType<?>>> otherWeldClasses) {
boolean managedBeanOrDecorator = !getEnvironment().getEjbDescriptors().contains(annotatedType.getJavaClass()) && Beans.isTypeManagedBeanOrDecoratorOrInterceptor(annotatedType);
if (managedBeanOrDecorator) {
containerLifecycleEvents.preloadProcessInjectionTarget(annotatedType.getJavaClass());
Expand All @@ -238,7 +241,7 @@ protected void createClassBean(SlimAnnotatedType<?> annotatedType, Map<Class<?>,
createManagedBean(weldClass);
}
} else {
otherWeldClasses.get(annotatedType.getJavaClass()).add(annotatedType);
getCacheValue(otherWeldClasses, annotatedType.getJavaClass()).add(annotatedType);
}
}

Expand Down

0 comments on commit ab2975d

Please sign in to comment.