Author: pete.muir(a)jboss.org
Date: 2008-12-04 08:19:02 -0500 (Thu, 04 Dec 2008)
New Revision: 399
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/MetaDataCache.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java
Log:
concurrency support for metadata
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/MetaDataCache.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/MetaDataCache.java 2008-12-04
13:00:28 UTC (rev 398)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/MetaDataCache.java 2008-12-04
13:19:02 UTC (rev 399)
@@ -18,176 +18,82 @@
package org.jboss.webbeans;
import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Callable;
import org.jboss.webbeans.ejb.EjbMetaData;
-import org.jboss.webbeans.model.AnnotationModel;
import org.jboss.webbeans.model.BindingTypeModel;
import org.jboss.webbeans.model.ScopeModel;
import org.jboss.webbeans.model.StereotypeModel;
+import org.jboss.webbeans.util.ConcurrentCache;
import org.jboss.webbeans.util.Strings;
-import com.google.common.collect.ForwardingMap;
-
public class MetaDataCache
{
- private abstract class AnnotationModelMap<T extends AnnotationModel<?>>
extends ForwardingMap<Class<? extends Annotation>, T>
- {
+ private ConcurrentCache<Class<? extends Annotation>,
StereotypeModel<?>> stereotypes = new ConcurrentCache<Class<? extends
Annotation>, StereotypeModel<?>>();
- Map<Class<? extends Annotation>, T> delegate;
+ private ConcurrentCache<Class<? extends Annotation>, ScopeModel<?>>
scopes = new ConcurrentCache<Class<? extends Annotation>,
ScopeModel<?>>();
- public AnnotationModelMap()
- {
- delegate = new ConcurrentHashMap<Class<? extends Annotation>, T>();
- }
+ private ConcurrentCache<Class<? extends Annotation>,
BindingTypeModel<?>> bindingTypes = new ConcurrentCache<Class<? extends
Annotation>, BindingTypeModel<?>>();
- public <S extends Annotation> T putIfAbsent(Class<S> key)
- {
- if (!containsKey(key))
- {
- T model = createAnnotationModel(key);
- super.put(key, model);
- return model;
- }
- return (T) super.get(key);
- }
+ private ConcurrentCache<Class<?>, EjbMetaData<?>> ejbMetaDataMap =
new ConcurrentCache<Class<?>, EjbMetaData<?>>();
- protected abstract <S extends Annotation> T
createAnnotationModel(Class<S> type);
-
- @Override
- protected Map<Class<? extends Annotation>, T> delegate()
+ public <T extends Annotation> void addStereotype(final Class<T>
stereotype)
+ {
+ stereotypes.putIfAbsent(stereotype, new Callable<StereotypeModel<?>>()
{
- return delegate;
- }
- @Override
- public String toString()
- {
- return Strings.mapToString("AnnotationModelMap (annotation -> ?):
", delegate);
- }
-
+ public StereotypeModel<?> call() throws Exception
+ {
+ return new StereotypeModel<T>(stereotype);
+ }
+ });
}
- @SuppressWarnings("unchecked")
- private class ScopeModelMap extends AnnotationModelMap<ScopeModel<?>>
+ public StereotypeModel<?> getStereotype(Class<? extends Annotation>
annotationType)
{
-
- @Override
- public <S extends Annotation> ScopeModel<S> putIfAbsent(Class<S>
key)
- {
- return (ScopeModel<S>) super.putIfAbsent(key);
- }
-
- @Override
- protected <S extends Annotation> ScopeModel<?>
createAnnotationModel(Class<S> type)
- {
- return new ScopeModel<S>(type);
- }
-
- @Override
- public String toString()
- {
- return Strings.mapToString("ScopeModelMap (annotation -> ScopeModel):
", delegate);
- }
-
+ return stereotypes.getValue(annotationType);
}
- @SuppressWarnings("unchecked")
- private class BindingTypeModelMap extends
AnnotationModelMap<BindingTypeModel<?>>
+ public <T extends Annotation> ScopeModel<T> getScopeModel(final
Class<T> scopeType)
{
-
- @Override
- public <S extends Annotation> BindingTypeModel<S>
putIfAbsent(Class<S> key)
+ return scopes.putIfAbsent(scopeType, new Callable<ScopeModel<T>>()
{
- return (BindingTypeModel<S>) super.putIfAbsent(key);
- }
- @Override
- protected <S extends Annotation> BindingTypeModel<?>
createAnnotationModel(Class<S> type)
- {
- return new BindingTypeModel<S>(type);
- }
-
- @Override
- public String toString()
- {
- return Strings.mapToString("BindingTypeModelMap (annotation ->
BindingTypeModel): ", delegate);
- }
+ public ScopeModel<T> call() throws Exception
+ {
+ return new ScopeModel<T>(scopeType);
+ }
+
+ });
}
- private class EjbMetaDataMap extends ForwardingMap<Class<?>,
EjbMetaData<?>>
+ public <T extends Annotation> BindingTypeModel<T>
getBindingTypeModel(final Class<T> bindingType)
{
-
- private Map<Class<?>, EjbMetaData<?>> delegate;
-
- public EjbMetaDataMap()
+ return bindingTypes.putIfAbsent(bindingType, new
Callable<BindingTypeModel<T>>()
{
- delegate = new HashMap<Class<?>, EjbMetaData<?>>();
- }
- @Override
- protected Map<Class<?>, EjbMetaData<?>> delegate()
- {
- return delegate;
- }
-
- @SuppressWarnings("unchecked")
- public <T> EjbMetaData<T> putIfAbsent(Class<T> key)
- {
- if (!containsKey(key))
+ public BindingTypeModel<T> call() throws Exception
{
- EjbMetaData<T> ejbMetaData = new EjbMetaData<T>(key);
- super.put(key, ejbMetaData);
- return ejbMetaData;
+ return new BindingTypeModel<T>(bindingType);
}
- return (EjbMetaData<T>) super.get(key);
- }
-
- @Override
- public String toString()
- {
- return Strings.mapToString("EJBMetadataMap (class -> EJBMetaData):
", delegate);
- }
-
+
+ });
}
- private Map<Class<? extends Annotation>, StereotypeModel<?>>
stereotypes = new HashMap<Class<? extends Annotation>,
StereotypeModel<?>>();
-
- private ScopeModelMap scopes = new ScopeModelMap();
-
- private BindingTypeModelMap bindingTypes = new BindingTypeModelMap();
-
- private EjbMetaDataMap ejbMetaDataMap = new EjbMetaDataMap();
-
- public void addStereotype(StereotypeModel<?> stereotype)
+ public <T> EjbMetaData<T> getEjbMetaData(final Class<T> clazz)
{
- stereotypes.put(stereotype.getType(), stereotype);
- }
+ return ejbMetaDataMap.putIfAbsent(clazz, new
Callable<EjbMetaData<T>>()
+ {
- public StereotypeModel<?> getStereotype(Class<? extends Annotation>
annotationType)
- {
- return stereotypes.get(annotationType);
+ public EjbMetaData<T> call() throws Exception
+ {
+ return new EjbMetaData<T>(clazz);
+ }
+
+ });
}
- public <T extends Annotation> ScopeModel<T> getScopeModel(Class<T>
scopeType)
- {
- return scopes.putIfAbsent(scopeType);
- }
-
- public <T extends Annotation> BindingTypeModel<T>
getBindingTypeModel(Class<T> bindingType)
- {
- return bindingTypes.putIfAbsent(bindingType);
- }
-
- public <T> EjbMetaData<T> getEjbMetaData(Class<T> clazz)
- {
- return ejbMetaDataMap.putIfAbsent(clazz);
- }
-
@Override
public String toString()
{
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-04
13:00:28 UTC (rev 398)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ContextMap.java 2008-12-04
13:19:02 UTC (rev 399)
@@ -53,7 +53,7 @@
{
try
{
- return (AbstractContext) get(scopeType).get().iterator().next();
+ return (AbstractContext) getFuture(scopeType).get().iterator().next();
}
catch (InterruptedException e)
{
@@ -84,7 +84,7 @@
{
try
{
- return get(scopeType).get();
+ return getFuture(scopeType).get();
}
catch (InterruptedException e)
{
Modified: 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 2008-12-04
13:00:28 UTC (rev 398)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ConcurrentCache.java 2008-12-04
13:19:02 UTC (rev 399)
@@ -21,11 +21,42 @@
}
@SuppressWarnings("unchecked")
- public <T extends V> Future<T> get(K key)
+ public <T extends V> Future<T> getFuture(K key)
{
return (Future<T>) super.get(key);
}
+ public <T extends V> T getValue(K key)
+ {
+ Future<T> value = (Future<T>) map.get(key);
+ 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();
+ }
+ }
+ }
+
public <E> E putIfAbsent(K key, Callable<E> callable)
{
Future<E> value = (Future<E>) map.get(key);
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java 2008-12-04
13:00:28 UTC (rev 398)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java 2008-12-04
13:19:02 UTC (rev 399)
@@ -6,7 +6,6 @@
import javax.webbeans.Standard;
import org.jboss.webbeans.bootstrap.Bootstrap;
-import org.jboss.webbeans.model.StereotypeModel;
import org.jboss.webbeans.test.annotations.AnimalStereotype;
import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
import org.jboss.webbeans.test.annotations.FishStereotype;
@@ -47,14 +46,14 @@
protected void addStereotypes()
{
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<Decorator>(Decorator.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<Interceptor>(Interceptor.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<AnimalStereotype>(AnimalStereotype.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<HornedMammalStereotype>(HornedMammalStereotype.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<MammalStereotype>(MammalStereotype.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<FishStereotype>(FishStereotype.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<RiverFishStereotype>(RiverFishStereotype.class));
- manager.getMetaDataCache().addStereotype(new
StereotypeModel<RequestScopedAnimalStereotype>(RequestScopedAnimalStereotype.class));
+ manager.getMetaDataCache().addStereotype(Decorator.class);
+ manager.getMetaDataCache().addStereotype(Interceptor.class);
+ manager.getMetaDataCache().addStereotype(AnimalStereotype.class);
+ manager.getMetaDataCache().addStereotype(HornedMammalStereotype.class);
+ manager.getMetaDataCache().addStereotype(MammalStereotype.class);
+ manager.getMetaDataCache().addStereotype(FishStereotype.class);
+ manager.getMetaDataCache().addStereotype(RiverFishStereotype.class);
+ manager.getMetaDataCache().addStereotype(RequestScopedAnimalStereotype.class);
}
}
Show replies by date