Skip to content

Instantly share code, notes, and snippets.

@rmannibucau
Created January 21, 2013 09:48
Show Gist options
  • Select an option

  • Save rmannibucau/4584921 to your computer and use it in GitHub Desktop.

Select an option

Save rmannibucau/4584921 to your computer and use it in GitHub Desktop.
Index: webbeans-impl/src/main/java/org/apache/webbeans/container/BeanCacheKey.java
===================================================================
--- webbeans-impl/src/main/java/org/apache/webbeans/container/BeanCacheKey.java (revision 1436220)
+++ webbeans-impl/src/main/java/org/apache/webbeans/container/BeanCacheKey.java (working copy)
@@ -161,9 +161,8 @@
}
if (qualifiers != null)
{
- for (int i = 0; i < qualifiers.length; i++)
- {
- computedHashCode = 31 * computedHashCode + getQualifierHashCode(qualifiers[i]);
+ for (Annotation qualifier1 : qualifiers) {
+ computedHashCode = 31 * computedHashCode + getQualifierHashCode(qualifier1);
}
}
return computedHashCode;
Index: webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
===================================================================
--- webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java (revision 1436220)
+++ webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java (working copy)
@@ -423,15 +423,12 @@
return beansXMLScanner.getBeansXml(injectionPointBeanClass);
}
- /**
- * Resolution by type.
- *
- * @param injectionPointType injection point api type
- * @param qualifiers qualifiers of the injection point
- * @return set of resolved beans
- */
public Set<Bean<?>> implResolveByType(Type injectionPointType, Class<?> injectinPointClass, Annotation... qualifiers)
{
+ return implResolveByType(null, injectionPointType, injectinPointClass, qualifiers);
+ }
+
+ public BeanCacheKey computeKey(Type injectionPointType, Class<?> injectinPointClass, Annotation... qualifiers) {
ScannerService scannerService = webBeansContext.getScannerService();
String bdaBeansXMLFilePath = null;
if (scannerService.isBDABeansXmlScanningEnabled())
@@ -448,6 +445,22 @@
bdaBeansXMLFilePath = getBDABeansXMLPath(injectinPointClass);
}
+ return new BeanCacheKey(injectionPointType, bdaBeansXMLFilePath, qualifiers);
+ }
+
+ /**
+ * Resolution by type.
+ *
+ * @param injectionPointType injection point api type
+ * @param qualifiers qualifiers of the injection point
+ * @return set of resolved beans
+ */
+ public Set<Bean<?>> implResolveByType(BeanCacheKey cacheKey, Type injectionPointType, Class<?> injectinPointClass, Annotation... qualifiers)
+ {
+ if (cacheKey == null) {
+ cacheKey = computeKey(injectionPointType, injectinPointClass, qualifiers);
+ }
+
boolean currentQualifier = false;
if (isInstanceOrEventInjection(injectionPointType))
@@ -463,8 +476,6 @@
}
}
- BeanCacheKey cacheKey = new BeanCacheKey(injectionPointType, bdaBeansXMLFilePath, qualifiers);
-
Set<Bean<?>> resolvedComponents = resolvedBeansByType.get(cacheKey);
if (resolvedComponents != null)
{
Index: webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
===================================================================
--- webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java (revision 1436220)
+++ webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java (working copy)
@@ -18,32 +18,35 @@
*/
package org.apache.webbeans.inject.instance;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanCacheKey;
+import org.apache.webbeans.container.BeanManagerImpl;
+import org.apache.webbeans.container.InjectionResolver;
+import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.portable.InjectionPointProducer;
+import org.apache.webbeans.util.ClassUtil;
+import org.apache.webbeans.util.InjectionExceptionUtil;
+import org.apache.webbeans.util.OwbCustomObjectInputStream;
+import org.apache.webbeans.util.WebBeansUtil;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.Instance;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.util.TypeLiteral;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.Instance;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.InjectionPoint;
-import javax.enterprise.util.TypeLiteral;
-
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.container.BeanManagerImpl;
-import org.apache.webbeans.container.InjectionResolver;
-import org.apache.webbeans.context.creational.CreationalContextImpl;
-import org.apache.webbeans.portable.InjectionPointProducer;
-import org.apache.webbeans.util.ClassUtil;
-import org.apache.webbeans.util.InjectionExceptionUtil;
-import org.apache.webbeans.util.OwbCustomObjectInputStream;
-import org.apache.webbeans.util.WebBeansUtil;
-
/**
* Implements the {@link Instance} interface.
*
@@ -62,11 +65,13 @@
private InjectionPoint injectionPoint;
/** Qualifier annotations appeared on the injection point */
- private Set<Annotation> qualifierAnnotations = new HashSet<Annotation>();
+ private Annotation[] anns;
private WebBeansContext webBeansContext;
private CreationalContext<?> parentCreationalContext;
+ private BeanCacheKey cacheKey = null;
+ private final ConcurrentMap<BeanCacheKey, InstanceImpl<T>> instancesCache = new ConcurrentHashMap<BeanCacheKey, InstanceImpl<T>>();
/**
* Creates new instance.
@@ -75,7 +80,6 @@
* @param injectionPoint null or injection point
* @param webBeansContext
* @param creationalContext will get used for creating &#064;Dependent beans
- * @param ownerInstance the object the current Instance got injected into
* @param annotations qualifier annotations
*/
public InstanceImpl(Type injectionClazz, InjectionPoint injectionPoint, WebBeansContext webBeansContext,
@@ -85,13 +89,32 @@
this.injectionPoint = injectionPoint;
this.parentCreationalContext = creationalContext;
+ final Set<Annotation> qualifierAnnotations = new HashSet<Annotation>();
for (Annotation ann : annotations)
{
qualifierAnnotations.add(ann);
}
+ anns = new Annotation[qualifierAnnotations.size()];
+ anns = qualifierAnnotations.toArray(anns);
+
this.webBeansContext = webBeansContext;
+
+ computeCacheKey();
}
+ private void computeCacheKey() {
+ InjectionResolver injectionResolver = webBeansContext.getBeanManagerImpl().getInjectionResolver();
+
+ Bean<?> injectionPointBean = injectionPoint.getBean();
+ Class<?> injectionPointClass = null;
+ if (injectionPointBean != null)
+ {
+ injectionPointClass = injectionPointBean.getBeanClass();
+ }
+
+ cacheKey = injectionResolver.computeKey(injectionClazz, injectionPointClass, anns);
+ }
+
/**
* Returns the bean instance with given qualifier annotations.
*
@@ -105,9 +128,6 @@
InjectionPointProducer.setThreadLocal(injectionPoint);
try
{
- Annotation[] anns = new Annotation[qualifierAnnotations.size()];
- anns = qualifierAnnotations.toArray(anns);
-
Set<Bean<?>> beans = resolveBeans();
BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
@@ -152,20 +172,17 @@
*/
private Set<Bean<?>> resolveBeans()
{
- Annotation[] anns = new Annotation[qualifierAnnotations.size()];
- anns = qualifierAnnotations.toArray(anns);
-
InjectionResolver injectionResolver = webBeansContext.getBeanManagerImpl().getInjectionResolver();
- InjectionResolver resolver = injectionResolver;
Bean<?> injectionPointBean = injectionPoint.getBean();
Class<?> injectionPointClass = null;
if (injectionPointBean != null)
{
injectionPointClass = injectionPointBean.getBeanClass();
}
- Set<Bean<?>> beans = resolver.implResolveByType(injectionClazz, injectionPointClass, anns);
- return resolver.resolveAll(beans);
+
+ Set<Bean<?>> beans = injectionResolver.implResolveByType(cacheKey, injectionClazz, injectionPointClass, anns);
+ return injectionResolver.resolveAll(beans);
}
/**
@@ -193,12 +210,33 @@
*/
public Instance<T> select(Annotation... qualifiers)
{
- Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
- InstanceImpl<T> newInstance = new InstanceImpl<T>(injectionClazz, injectionPoint, webBeansContext, parentCreationalContext, newQualifiersArray);
+ final BeanCacheKey key = new BeanCacheKey(InstanceImpl.class, null, qualifiers); // todo: QualifierKey
+ InstanceImpl<T> instance = instancesCache.get(key);
+ if (instance != null) {
+ return instance;
+ }
- return newInstance;
+ final Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
+
+ instance = new InstanceImpl<T>(injectionClazz, injectionPoint, webBeansContext, parentCreationalContext, newQualifiersArray);
+ InstanceImpl<T> old = instancesCache.putIfAbsent(key, instance);
+ if (old != null) {
+ instance = old;
+ }
+
+ return instance;
}
+ // cache if no values - todo: check we couldn't cache with values
+ private static boolean isCacheable(final Annotation[] qualifiers) {
+ for (Annotation a : qualifiers) {
+ if (a.annotationType().getDeclaredMethods().length > 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Returns total qualifiers array
*
@@ -208,7 +246,7 @@
private Annotation[] getAdditionalQualifiers(Annotation[] qualifiers)
{
webBeansContext.getAnnotationManager().checkQualifierConditions(qualifiers);
- Set<Annotation> newQualifiers = new HashSet<Annotation>(qualifierAnnotations);
+ Set<Annotation> newQualifiers = new HashSet<Annotation>(Arrays.asList(anns));
if (qualifiers != null && qualifiers.length > 0)
{
@@ -225,7 +263,7 @@
Annotation[] newQualifiersArray = new Annotation[newQualifiers.size()];
newQualifiersArray = newQualifiers.toArray(newQualifiersArray);
-
+
return newQualifiersArray;
}
@@ -287,7 +325,7 @@
{
ObjectOutputStream oos = new ObjectOutputStream(op);
oos.writeObject(injectionClazz);
- oos.writeObject(qualifierAnnotations);
+ oos.writeObject(anns);
oos.writeObject(injectionPoint);
oos.flush();
@@ -298,7 +336,7 @@
webBeansContext = WebBeansContext.currentInstance();
final ObjectInputStream inputStream = new OwbCustomObjectInputStream(in, WebBeansUtil.getCurrentClassLoader());
injectionClazz = (Type)inputStream.readObject();
- qualifierAnnotations = (Set<Annotation>)inputStream.readObject();
+ anns = (Annotation[])inputStream.readObject();
injectionPoint = (InjectionPoint) inputStream.readObject();
}
@@ -312,7 +350,7 @@
builder.append(",with qualifier annotations {");
int i = 0;
- for (Annotation qualifier : qualifierAnnotations)
+ for (Annotation qualifier : anns)
{
if (i != 0)
{
@@ -320,6 +358,7 @@
}
builder.append(qualifier.toString());
+ i++;
}
builder.append("}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment