[webbeans-commits] Webbeans SVN: r398 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: bean/proxy and 2 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Thu Dec 4 08:00:28 EST 2008
Author: pete.muir at jboss.org
Date: 2008-12-04 08:00:28 -0500 (Thu, 04 Dec 2008)
New Revision: 398
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Resolver.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java
Log:
Hide implementation of concurrent cache
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-03 22:49:55 UTC (rev 397)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-04 13:00:28 UTC (rev 398)
@@ -422,7 +422,7 @@
*/
public Context getContext(Class<? extends Annotation> scopeType)
{
- List<Context> contexts = contextMap.get(scopeType);
+ List<Context> contexts = contextMap.getContext(scopeType);
if (contexts.isEmpty())
{
throw new ContextNotActiveException("No active contexts for " + scopeType.getName());
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Resolver.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Resolver.java 2008-12-03 22:49:55 UTC (rev 397)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Resolver.java 2008-12-04 13:00:28 UTC (rev 398)
@@ -20,18 +20,12 @@
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.Callable;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import java.util.concurrent.FutureTask;
import javax.webbeans.NullableDependencyException;
import javax.webbeans.manager.Bean;
@@ -42,11 +36,10 @@
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.ForwardingAnnotatedItem;
import org.jboss.webbeans.model.BindingTypeModel;
+import org.jboss.webbeans.util.ConcurrentCache;
import org.jboss.webbeans.util.ListComparator;
import org.jboss.webbeans.util.Strings;
-import com.google.common.collect.ForwardingMap;
-
/**
* Implementation of Web Beans type safe and name based bean resolution
*
@@ -94,43 +87,10 @@
}
- /**
- * Type safe map for caching annotation metadata
- */
- @SuppressWarnings("unchecked")
- private class ConcurrentAnnotatedItemMap extends ForwardingMap<AnnotatedItem<?, ?>, Future>
- {
-
- private Map<AnnotatedItem<?, ?>, Future> delegate;
-
- public ConcurrentAnnotatedItemMap()
- {
- delegate = new ConcurrentHashMap<AnnotatedItem<?, ?>, Future>();
- }
-
- public <T> Future<Set<Bean<T>>> get(AnnotatedItem<T, ?> key)
- {
- return (Future<Set<Bean<T>>>) super.get(key);
- }
-
- @Override
- protected Map<AnnotatedItem<?, ?>, Future> delegate()
- {
- return delegate;
- }
-
- @Override
- public String toString()
- {
- return Strings.mapToString("AnnotatedItemMap (annotated item -> bean set): ", delegate);
- }
-
- }
-
- private ConcurrentAnnotatedItemMap resolvedInjectionPoints;
+ private ConcurrentCache<ResolvableAnnotatedItem<?, ?>, Set<Bean<?>>> resolvedInjectionPoints;
private Set<AnnotatedItem<?, ?>> injectionPoints;
- private Map<String, Future<Set<Bean<?>>>> resolvedNames;
+ private ConcurrentCache<String, Set<Bean<?>>> resolvedNames;
private ManagerImpl manager;
@@ -138,7 +98,8 @@
{
this.manager = manager;
this.injectionPoints = new HashSet<AnnotatedItem<?, ?>>();
- this.resolvedInjectionPoints = new ConcurrentAnnotatedItemMap();
+ this.resolvedInjectionPoints = new ConcurrentCache<ResolvableAnnotatedItem<?,?>, Set<Bean<?>>>();
+ this.resolvedNames = new ConcurrentCache<String, Set<Bean<?>>>();
}
/**
@@ -150,38 +111,14 @@
injectionPoints.addAll(elements);
}
- private void registerName(final String name)
+ private <T, S> Set<Bean<T>> registerInjectionPoint(final ResolvableAnnotatedItem<T, S> element)
{
- FutureTask<Set<Bean<?>>> task = new FutureTask<Set<Bean<?>>>(new Callable<Set<Bean<?>>>()
+ Callable<Set<Bean<T>>> callable = new Callable<Set<Bean<T>>>()
{
- public Set<Bean<?>> call() throws Exception
+ public Set<Bean<T>> call() throws Exception
{
- Set<Bean<?>> beans = new HashSet<Bean<?>>();
- for (Bean<?> bean : manager.getBeans())
- {
- if ((bean.getName() == null && name == null) || (bean.getName() != null && bean.getName().equals(name)))
- {
- beans.add(bean);
- }
- }
- return retainHighestPrecedenceBeans(beans, manager.getEnabledDeploymentTypes());
- }
-
- });
- resolvedNames.put(name, task);
-
- task.run();
- }
-
- private <T, S> void registerInjectionPoint(final AnnotatedItem<T, S> element)
- {
- FutureTask<Set<Bean<?>>> task = new FutureTask<Set<Bean<?>>>(new Callable<Set<Bean<?>>>()
- {
-
- public Set<Bean<?>> call() throws Exception
- {
- Set<Bean<?>> beans = retainHighestPrecedenceBeans(getMatchingBeans(element, manager.getBeans(), manager.getMetaDataCache()), manager.getEnabledDeploymentTypes());
+ Set<Bean<T>> beans = retainHighestPrecedenceBeans(getMatchingBeans(element, manager.getBeans(), manager.getMetaDataCache()), manager.getEnabledDeploymentTypes());
if (element.getType().isPrimitive())
{
for (Bean<?> bean : beans)
@@ -195,20 +132,8 @@
return beans;
}
- });
-
- resolvedInjectionPoints.put(new ResolvableAnnotatedItem<T, S>()
- {
-
- @Override
- public AnnotatedItem<T, S> delegate()
- {
- return element;
- }
-
- }, task);
-
- task.run();
+ };
+ return resolvedInjectionPoints.putIfAbsent(element, callable);
}
/**
@@ -217,8 +142,8 @@
*/
public void clear()
{
- resolvedInjectionPoints = new ConcurrentAnnotatedItemMap();
- resolvedNames = new HashMap<String, Future<Set<Bean<?>>>>();
+ this.resolvedInjectionPoints = new ConcurrentCache<ResolvableAnnotatedItem<?,?>, Set<Bean<?>>>();
+ resolvedNames = new ConcurrentCache<String, Set<Bean<?>>>();
}
/**
@@ -227,9 +152,17 @@
*/
public void resolveInjectionPoints()
{
- for (AnnotatedItem<?, ?> injectable : injectionPoints)
+ for (final AnnotatedItem injectable : injectionPoints)
{
- registerInjectionPoint(injectable);
+ registerInjectionPoint(new ResolvableAnnotatedItem<Object, Object>()
+ {
+
+ @Override
+ public AnnotatedItem<Object, Object> delegate()
+ {
+ return injectable;
+ }
+ });
}
}
@@ -241,7 +174,7 @@
{
Set<Bean<T>> beans = new HashSet<Bean<T>>();
- AnnotatedItem<T, S> element = new ResolvableAnnotatedItem<T, S>()
+ final ResolvableAnnotatedItem<T, S> element = new ResolvableAnnotatedItem<T, S>()
{
@Override
@@ -259,48 +192,7 @@
}
else
{
- if (!resolvedInjectionPoints.containsKey(element))
- {
- registerInjectionPoint(element);
- }
-
- boolean interupted = false;
- try
- {
- while (true)
- {
- try
- {
- return Collections.unmodifiableSet(resolvedInjectionPoints.get(element).get());
- }
- catch (ExecutionException e)
- {
- if (e.getCause() instanceof RuntimeException)
- {
- throw (RuntimeException) e.getCause();
- }
- else if (e.getCause() instanceof Error)
- {
- throw (Error) e.getCause();
- }
- else
- {
- throw new IllegalStateException(e.getCause());
- }
- }
- catch (InterruptedException e)
- {
- interupted = true;
- }
- }
- }
- finally
- {
- if (interupted)
- {
- Thread.currentThread().interrupt();
- }
- }
+ beans = registerInjectionPoint(element);
}
return Collections.unmodifiableSet(beans);
}
@@ -308,53 +200,28 @@
/**
* Get the possible beans for the given name
*/
- public Set<Bean<?>> get(String name)
+ public Set<Bean<?>> get(final String name)
{
- if (!resolvedNames.containsKey(name))
+ return resolvedNames.putIfAbsent(name, new Callable<Set<Bean<?>>>()
{
- registerName(name);
- }
- boolean interupted = false;
- try
- {
- while (true)
+ public Set<Bean<?>> call() throws Exception
{
- try
+ Set<Bean<?>> beans = new HashSet<Bean<?>>();
+ for (Bean<?> bean : manager.getBeans())
{
- return Collections.unmodifiableSet(resolvedNames.get(name).get());
- }
- catch (ExecutionException e)
- {
- if (e.getCause() instanceof RuntimeException)
+ if ((bean.getName() == null && name == null) || (bean.getName() != null && bean.getName().equals(name)))
{
- throw (RuntimeException) e.getCause();
+ beans.add(bean);
}
- else if (e.getCause() instanceof Error)
- {
- throw (Error) e.getCause();
- }
- else
- {
- throw new IllegalStateException(e.getCause());
- }
}
- catch (InterruptedException e)
- {
- interupted = true;
- }
+ return retainHighestPrecedenceBeans((Set) beans, manager.getEnabledDeploymentTypes());
}
- }
- finally
- {
- if (interupted)
- {
- Thread.currentThread().interrupt();
- }
- }
+
+ });
}
- private static Set<Bean<?>> retainHighestPrecedenceBeans(Set<Bean<?>> beans, List<Class<? extends Annotation>> enabledDeploymentTypes)
+ private static <T> Set<Bean<T>> retainHighestPrecedenceBeans(Set<Bean<T>> beans, List<Class<? extends Annotation>> enabledDeploymentTypes)
{
if (beans.size() > 0)
{
@@ -364,12 +231,12 @@
possibleDeploymentTypes.add(bean.getDeploymentType());
}
possibleDeploymentTypes.retainAll(enabledDeploymentTypes);
- Set<Bean<?>> trimmed = new HashSet<Bean<?>>();
+ Set<Bean<T>> trimmed = new HashSet<Bean<T>>();
if (possibleDeploymentTypes.size() > 0)
{
Class<? extends Annotation> highestPrecedencePossibleDeploymentType = possibleDeploymentTypes.last();
- for (Bean<?> bean : beans)
+ for (Bean<T> bean : beans)
{
if (bean.getDeploymentType().equals(highestPrecedencePossibleDeploymentType))
{
@@ -385,14 +252,14 @@
}
}
- private static Set<Bean<?>> getMatchingBeans(AnnotatedItem<?, ?> element, List<Bean<?>> beans, MetaDataCache metaDataCache)
+ private static <T> Set<Bean<T>> getMatchingBeans(AnnotatedItem<T, ?> element, List<Bean<?>> beans, MetaDataCache metaDataCache)
{
- Set<Bean<?>> resolvedBeans = new HashSet<Bean<?>>();
+ Set<Bean<T>> resolvedBeans = new HashSet<Bean<T>>();
for (Bean<?> bean : beans)
{
if (element.isAssignableFrom(bean.getTypes()) && containsAllBindingBindingTypes(element, bean.getBindingTypes(), metaDataCache))
{
- resolvedBeans.add(bean);
+ resolvedBeans.add((Bean<T>) bean);
}
}
return resolvedBeans;
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-03 22:49:55 UTC (rev 397)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-04 13:00:28 UTC (rev 398)
@@ -20,13 +20,8 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import java.util.concurrent.FutureTask;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
@@ -35,10 +30,8 @@
import javax.webbeans.manager.Bean;
import org.jboss.webbeans.ManagerImpl;
-import org.jboss.webbeans.util.Strings;
+import org.jboss.webbeans.util.ConcurrentCache;
-import com.google.common.collect.ForwardingMap;
-
/**
* A proxy pool for holding scope adaptors (client proxies)
*
@@ -53,43 +46,14 @@
*
* @author Nicklas Karlsson
*/
- private class Pool extends ForwardingMap<Bean<?>, Future<Object>>
- {
- Map<Bean<?>, Future<Object>> delegate;
-
- public Pool()
- {
- delegate = new ConcurrentHashMap<Bean<?>, Future<Object>>();
- }
-
- @SuppressWarnings("unchecked")
- public <T> Future<T> get(Bean<T> key)
- {
- return (Future<T>) super.get(key);
- }
-
- @Override
- protected Map<Bean<?>, Future<Object>> delegate()
- {
- return delegate;
- }
-
- @Override
- public String toString()
- {
- return Strings.mapToString("ProxyPool (bean -> proxy): ", delegate);
- }
-
- }
-
private ManagerImpl manager;
- private Pool pool;
+ private ConcurrentCache<Bean<? extends Object>, Object> pool;
public ProxyPool(ManagerImpl manager)
{
this.manager = manager;
- this.pool = new Pool();
+ this.pool = new ConcurrentCache<Bean<? extends Object>, Object>();
}
/**
@@ -182,66 +146,22 @@
* @param bean
* @return
*/
- public Object getClientProxy(final Bean<?> bean)
+ public <T> T getClientProxy(final Bean<T> bean)
{
- Future<?> clientProxy = pool.get(bean);
- if (clientProxy == null)
+ return pool.putIfAbsent(bean, new Callable<T>()
{
- FutureTask<Object> task = new FutureTask<Object>(new Callable<Object>()
+
+ public T call() throws Exception
{
-
- public Object call() throws Exception
+ int beanIndex = manager.getBeans().indexOf(bean);
+ if (beanIndex < 0)
{
- int beanIndex = manager.getBeans().indexOf(bean);
- if (beanIndex < 0)
- {
- throw new DefinitionException(bean + " is not known to the manager");
- }
- return createClientProxy(bean, beanIndex, manager);
+ throw new DefinitionException(bean + " is not known to the manager");
}
-
- });
- clientProxy = task;
- pool.put(bean, task);
- task.run();
- }
- boolean interrupted = false;
- try
- {
- while (true)
- {
- try
- {
- return clientProxy.get();
- }
- catch (InterruptedException e)
- {
- interrupted = true;
- }
- catch (ExecutionException e)
- {
- if (e.getCause() instanceof RuntimeException)
- {
- throw (RuntimeException) e.getCause();
- }
- else if (e.getCause() instanceof Error)
- {
- throw (Error) e.getCause();
- }
- else
- {
- throw new IllegalStateException(e.getCause());
- }
- };
+ return createClientProxy(bean, beanIndex, manager);
}
- }
- finally
- {
- if (interrupted)
- {
- Thread.currentThread().interrupt();
- }
- }
+
+ });
}
@Override
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java 2008-12-03 22:49:55 UTC (rev 397)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java 2008-12-04 13:00:28 UTC (rev 398)
@@ -18,18 +18,16 @@
package org.jboss.webbeans.contexts;
import java.lang.annotation.Annotation;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutionException;
import javax.webbeans.manager.Context;
+import org.jboss.webbeans.util.ConcurrentCache;
import org.jboss.webbeans.util.Strings;
-import com.google.common.collect.ForwardingMap;
-
/**
* A map from a scope to a list of contexts
*
@@ -37,42 +35,80 @@
* @author Pete Muir
*
*/
-public class ContextMap extends ForwardingMap<Class<? extends Annotation>, List<Context>>
+public class ContextMap extends ConcurrentCache<Class<? extends Annotation>, List<Context>>
{
- private Map<Class<? extends Annotation>, List<Context>> delegate;
- public ContextMap()
- {
- delegate = new ConcurrentHashMap<Class<? extends Annotation>, List<Context>>();
- }
-
/**
* Gets the dependent context
*
* @param scopeType The scope type to get
* @return The dependent context
*/
- public DependentContext getBuiltInContext(Class<? extends Annotation> scopeType)
+ public AbstractContext getBuiltInContext(Class<? extends Annotation> scopeType)
{
- // TODO Why can we request any scopetype and the cast it to dependent?
- return (DependentContext) get(scopeType).iterator().next();
+ boolean interrupted = false;
+ try
+ {
+ while (true)
+ {
+ try
+ {
+ return (AbstractContext) get(scopeType).get().iterator().next();
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ catch (ExecutionException e)
+ {
+ rethrow(e);
+ }
+ }
+ }
+ finally
+ {
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ }
}
-
- /**
- * Returns the delegate of the forwarding map
- *
- * @return the delegate
- */
- @Override
- protected Map<Class<? extends Annotation>, List<Context>> delegate()
+
+ public List<Context> getContext(Class<? extends Annotation> scopeType)
{
- return delegate;
+ boolean interrupted = false;
+ // TODO Why can we request any scopetype and the cast it to dependent?
+ try
+ {
+ while (true)
+ {
+ try
+ {
+ return get(scopeType).get();
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ catch (ExecutionException e)
+ {
+ rethrow(e);
+ }
+ }
+ }
+ finally
+ {
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ }
}
@Override
public String toString()
{
- return Strings.mapToString("ContextMap (scope type -> context list): ", delegate);
+ return Strings.mapToString("ContextMap (scope type -> context list): ", delegate());
}
/**
@@ -84,31 +120,16 @@
*/
public void add(Context context)
{
- List<Context> contexts = super.get(context.getScopeType());
- if (contexts == null)
+ List<Context> contexts = putIfAbsent(context.getScopeType(), new Callable<List<Context>>()
{
- synchronized (delegate)
+
+ public List<Context> call() throws Exception
{
- contexts = new CopyOnWriteArrayList<Context>();
- put(context.getScopeType(), contexts);
+ return new CopyOnWriteArrayList<Context>();
}
- contexts = super.get(context.getScopeType());
- }
+
+ });
contexts.add(context);
}
- /**
- * Gets a context list for a scope type
- *
- * @param scopeType The scope type
- * @return A list of contexts. An empty list is returned if there are no registered scopes of this type
- */
- public List<Context> get(Class<? extends Annotation> scopeType)
- {
- List<Context> contexts = super.get(scopeType);
- return contexts != null ? contexts : new ArrayList<Context>();
- }
-
-
-
}
\ No newline at end of file
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java 2008-12-04 13:00:28 UTC (rev 398)
@@ -0,0 +1,95 @@
+package org.jboss.webbeans.util;
+
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
+
+import com.google.common.collect.ForwardingMap;
+
+public class ConcurrentCache<K, V> extends ForwardingMap<K, Future<V>>
+{
+
+ private ConcurrentMap<K, Future<V>> map;
+
+ public ConcurrentCache()
+ {
+ map = new ConcurrentHashMap<K, Future<V>>();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends V> Future<T> get(K key)
+ {
+ return (Future<T>) super.get(key);
+ }
+
+ public <E> E putIfAbsent(K key, Callable<E> callable)
+ {
+ Future<E> value = (Future<E>) map.get(key);
+ if (value == null)
+ {
+ FutureTask<E> task = new FutureTask<E>(callable);
+ value = task;
+ map.put(key, (Future<V>) task);
+ task.run();
+ }
+ boolean interrupted = false;
+ try
+ {
+ while (true)
+ {
+ try
+ {
+ return value.get();
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ catch (ExecutionException e)
+ {
+ rethrow(e);
+ };
+ }
+ }
+ finally
+ {
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ @Override
+ protected Map<K, Future<V>> delegate()
+ {
+ return map;
+ }
+
+ @Override
+ public String toString()
+ {
+ return Strings.mapToString("ProxyPool (bean -> proxy): ", map);
+ }
+
+ protected void rethrow(ExecutionException e)
+ {
+ if (e.getCause() instanceof RuntimeException)
+ {
+ throw (RuntimeException) e.getCause();
+ }
+ else if (e.getCause() instanceof Error)
+ {
+ throw (Error) e.getCause();
+ }
+ else
+ {
+ throw new IllegalStateException(e.getCause());
+ }
+ }
+
+}
\ No newline at end of file
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the weld-commits
mailing list