openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r912000 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/ main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/intercept/ main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbean...
Date Fri, 19 Feb 2010 21:55:06 GMT
Author: struberg
Date: Fri Feb 19 21:55:05 2010
New Revision: 912000

URL: http://svn.apache.org/viewvc?rev=912000&view=rev
Log:
OWB-288 implement caching for proxies in a 1st version.

This is far from being perfect, but should be better than creating new proxies every time!

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DependentScopedBeanInterceptorHandler.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/NormalScopedBeanInterceptorHandler.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/business/tests/DependingInterceptorTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
Fri Feb 19 21:55:05 2010
@@ -52,6 +52,7 @@
         X instance = null;
         try
         {
+            //X TODO cache proxy class!
             ProxyFactory proxyFactory = JavassistProxyFactory.createProxyFactory(this);
             
             ResourceInjectionService resourceService = ServiceLoader.getService(ResourceInjectionService.class);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
Fri Feb 19 21:55:05 2010
@@ -25,7 +25,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -118,9 +117,6 @@
     /**Injection resolver instance*/
     private InjectionResolver injectionResolver = null;
 
-    /**Proxy map for the webbeans components*/
-    private Map<Bean<?>, Object> proxyMap = Collections.synchronizedMap(new IdentityHashMap<Bean<?>,
Object>());
-    
     /**XML configurator instance*/
     private WebBeansXMLConfigurator xmlConfigurator = null;
     
@@ -756,23 +752,11 @@
                 return instance;
             }            
             //Create Managed Bean Proxy
-            else
-            {   
-                if (this.proxyMap.containsKey(bean))
-                {
-                    instance = this.proxyMap.get(bean);
-                }
-                else
-                {
-                    instance = JavassistProxyFactory.createNormalScopedBeanProxy((AbstractOwbBean<?>)bean,creationalContext);
-                    
-                    this.proxyMap.put(bean, instance);   
-
-                    //push this proxy instance into creational context
-                    CreationalContextImpl<Object> temp = (CreationalContextImpl<Object>)creationalContext;
-                    temp.setProxyInstance(instance);
-                }
-            }            
+            instance = JavassistProxyFactory.createNormalScopedBeanProxy((AbstractOwbBean<?>)bean,creationalContext);
+            
+            //push this proxy instance into creational context
+            CreationalContextImpl<Object> temp = (CreationalContextImpl<Object>)creationalContext;
+            temp.setProxyInstance(instance);
         }
         //Create Pseudo-Scope Bean Instance
         else

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DependentScopedBeanInterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DependentScopedBeanInterceptorHandler.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DependentScopedBeanInterceptorHandler.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DependentScopedBeanInterceptorHandler.java
Fri Feb 19 21:55:05 2010
@@ -23,7 +23,7 @@
 
 import javax.enterprise.context.spi.CreationalContext;
 
-import org.apache.webbeans.component.AbstractOwbBean;
+import org.apache.webbeans.component.OwbBean;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 
 public class DependentScopedBeanInterceptorHandler extends InterceptorHandler
@@ -34,7 +34,7 @@
     
     private CreationalContext<?> creationalContext;
     
-    public DependentScopedBeanInterceptorHandler(AbstractOwbBean<?> bean, Object instance,
CreationalContext<?> creationalContext)
+    public DependentScopedBeanInterceptorHandler(OwbBean<?> bean, Object instance,
CreationalContext<?> creationalContext)
     {
         super(bean);        
         this.actualInstance = instance;

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
Fri Feb 19 21:55:05 2010
@@ -22,9 +22,12 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javassist.util.proxy.MethodHandler;
 import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.BeanManager;
@@ -141,20 +144,25 @@
  */
 public abstract class InterceptorHandler implements MethodHandler, Serializable
 {
+    private static final long serialVersionUID = 1L;
+
     private static final WebBeansLogger logger = WebBeansLogger.getLogger(InterceptorHandler.class);
     
     protected OwbBean<?> bean = null;
 
-    protected InterceptorHandler(AbstractOwbBean<?> bean)
+    // TODO this proxy cache should get moved to JavassistProxyFactory
+    private static Map<OwbBean<?>, Class<?>> interceptorProxyClasses =
new ConcurrentHashMap<OwbBean<?>, Class<?>>();
+
+    protected InterceptorHandler(OwbBean<?> bean2)
     {
-        this.bean = bean;
+        this.bean = bean2;
     }
 
     public Object invoke(Object instance, Method method, Method proceed, Object[] arguments,
CreationalContextImpl<?> ownerCreationalContext) throws Exception
     {
         try
         {
-            if (bean instanceof InjectionTargetBean)
+            if (bean instanceof InjectionTargetBean<?>)
             {
                 InjectionTargetBean<?> injectionTarget = (InjectionTargetBean<?>)
this.bean;
 
@@ -167,10 +175,17 @@
 
                     if (injectionTarget.getDecoratorStack().size() > 0)
                     {
-                        ProxyFactory delegateFactory = JavassistProxyFactory.createProxyFactory(bean);
+                        // TODO move this part into JavassistProxyFactory
+                        Class<?> proxyClass = interceptorProxyClasses.get(bean);
+                        if (proxyClass == null)
+                        {
+                            ProxyFactory delegateFactory = JavassistProxyFactory.createProxyFactory(bean);
+                            proxyClass = delegateFactory.createClass();
+                            interceptorProxyClasses.put(bean, proxyClass);
+                        }
+                        Object delegate = proxyClass.newInstance();
                         delegateHandler = new DelegateHandler();
-                        delegateFactory.setHandler(delegateHandler);
-                        Object delegate = delegateFactory.createClass().newInstance();
+                        ((ProxyObject)delegate).setHandler(delegateHandler);
 
                         // Gets component decorator stack
                         decorators = WebBeansDecoratorConfig.getDecoratorStack(injectionTarget,
instance, delegate, ownerCreationalContext);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/NormalScopedBeanInterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/NormalScopedBeanInterceptorHandler.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/NormalScopedBeanInterceptorHandler.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/NormalScopedBeanInterceptorHandler.java
Fri Feb 19 21:55:05 2010
@@ -25,7 +25,7 @@
 import javax.enterprise.context.spi.Contextual;
 import javax.enterprise.context.spi.CreationalContext;
 
-import org.apache.webbeans.component.AbstractOwbBean;
+import org.apache.webbeans.component.OwbBean;
 import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 
@@ -33,15 +33,12 @@
 {
     private static final long serialVersionUID = -7169354477951284657L;
 
-    // A creationalContext has a very short lifespan. So we can use a ThreadLocal to pass
it over
-    // if we make sure that it is cleaned up properly!
-    private static ThreadLocal<CreationalContext<Object>> creationalContxt =
new ThreadLocal<CreationalContext<Object>>();
+    private CreationalContext<?> creationalContext;
 
-    @SuppressWarnings("unchecked")
-    public NormalScopedBeanInterceptorHandler(AbstractOwbBean<?> bean, CreationalContext<?>
cc)
+    public NormalScopedBeanInterceptorHandler(OwbBean<?> bean, CreationalContext<?>
creationalContext)
     {
         super(bean);
-        creationalContxt.set((CreationalContext<Object>) cc);
+        this.creationalContext = creationalContext;
     }
     
     @SuppressWarnings("unchecked")
@@ -52,40 +49,11 @@
 
         //Context of the bean
         Context webbeansContext = beanManager.getContext(bean.getScope());
-        Object webbeansInstance = webbeansContext.get(this.bean);
-
-        CreationalContext<Object> cc = null;
-
-        if (webbeansInstance == null)
-        {
-            cc = creationalContxt.get();
-
-            if (cc == null)
-            {
-                // we need to create the CreationalContext ourself and store it
-                try
-                {
-                    cc = (CreationalContext<Object>) beanManager.createCreationalContext(bean);
-                    creationalContxt.set(cc);
-                    //Get bean instance from context
-                    webbeansInstance = webbeansContext.get((Contextual<Object>)this.bean,
cc);
-                }
-                finally
-                {
-                    // make sure that we remove the cc from the thread in the handler who
created it
-                    creationalContxt.remove();
-                }
-            }
-            else
-            {
-                //Get bean instance from context
-                webbeansInstance = webbeansContext.get((Contextual<Object>)this.bean,
cc);
-            }
-
-        }
 
+        //Get bean instance from context
+        Object webbeansInstance = webbeansContext.get((Contextual<Object>)this.bean,
(CreationalContext<Object>) creationalContext);
 
-        return super.invoke(webbeansInstance, method, proceed, arguments, (CreationalContextImpl<?>)
cc);
+        return super.invoke(webbeansInstance, method, proceed, arguments, (CreationalContextImpl<?>)
creationalContext);
     }
     
     protected <T> Object callAroundInvokes(Method proceed, Object[] arguments, List<InterceptorData>
stack) throws Exception

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
Fri Feb 19 21:55:05 2010
@@ -19,16 +19,19 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.Decorator;
 
 import org.apache.webbeans.annotation.WebBeansAnnotation;
-import org.apache.webbeans.component.AbstractOwbBean;
+import org.apache.webbeans.component.OwbBean;
 import org.apache.webbeans.component.InjectionTargetBean;
 import org.apache.webbeans.decorator.WebBeansDecorator;
 import org.apache.webbeans.exception.WebBeansException;
@@ -44,20 +47,31 @@
     {
 
     }
+    
+    private static Map<OwbBean<?>, Class<?>> normalScopedBeanProxyClasses
= new ConcurrentHashMap<OwbBean<?>, Class<?>>();
+    
+    private static Map<OwbBean<?>, Class<?>> dependentScopedBeanProxyClasses
= new ConcurrentHashMap<OwbBean<?>, Class<?>>();
 
-    public static Object createNormalScopedBeanProxy(AbstractOwbBean<?> bean, CreationalContext<?>
creationalContext)
+    public static Object createNormalScopedBeanProxy(OwbBean<?> bean, CreationalContext<?>
creationalContext)
     {
         Object result = null;
         try
         {
-            ProxyFactory fact = createProxyFactory(bean);
+            Class<?> proxyClass = normalScopedBeanProxyClasses.get(bean);
+            if (proxyClass == null)
+            {
+                ProxyFactory fact = createProxyFactory(bean);
 
-            if (!(bean instanceof WebBeansDecorator) && !(bean instanceof WebBeansInterceptor))
+                proxyClass = fact.createClass();
+                normalScopedBeanProxyClasses.put(bean, proxyClass);
+            }
+            
+            result = proxyClass.newInstance();
+            
+            if (!(bean instanceof WebBeansDecorator<?>) && !(bean instanceof
WebBeansInterceptor<?>))
             {
-                fact.setHandler(new NormalScopedBeanInterceptorHandler((AbstractOwbBean<?>)
bean, creationalContext));
+                ((ProxyObject)result).setHandler(new NormalScopedBeanInterceptorHandler(bean,
creationalContext));
             }
-
-            result = fact.createClass().newInstance();
         }
         catch (Exception e)
         {
@@ -67,14 +81,14 @@
         return result;
     }
     
-    public static Object createDependentScopedBeanProxy(AbstractOwbBean<?> bean, Object
actualInstance, CreationalContext<?> creastionalContext)
+    public static Object createDependentScopedBeanProxy(OwbBean<?> bean, Object actualInstance,
CreationalContext<?> creastionalContext)
     {
         Object result = null;
         
         List<InterceptorData> interceptors =  null;
         List<Decorator<?>> decorators = null;
         InjectionTargetBean<?> injectionTargetBean = null;
-        if(bean instanceof InjectionTargetBean)
+        if(bean instanceof InjectionTargetBean<?>)
         {
             injectionTargetBean = (InjectionTargetBean<?>)bean;
             interceptors = injectionTargetBean.getInterceptorStack();
@@ -97,11 +111,8 @@
                 {
                     continue;
                 }
-                else
-                {
-                    notInInterceptorClassAndLifecycle = true;
-                    break;
-                }
+                notInInterceptorClassAndLifecycle = true;
+                break;
             }
         }
         
@@ -113,14 +124,20 @@
         
         try
         {
-            ProxyFactory fact = createProxyFactory(bean);
-
-            if (!(bean instanceof WebBeansDecorator) && !(bean instanceof WebBeansInterceptor))
+            Class<?> proxyClass = dependentScopedBeanProxyClasses.get(bean);
+            if (proxyClass == null)
+            {
+                ProxyFactory fact = createProxyFactory(bean);
+                proxyClass = fact.createClass();
+                dependentScopedBeanProxyClasses.put(bean, proxyClass);
+            }
+            
+            result = proxyClass.newInstance();
+            if (!(bean instanceof WebBeansDecorator<?>) && !(bean instanceof
WebBeansInterceptor<?>))
             {
-                fact.setHandler(new DependentScopedBeanInterceptorHandler(bean, actualInstance,
creastionalContext));
+                ((ProxyObject)result).setHandler(new DependentScopedBeanInterceptorHandler(bean,
actualInstance, creastionalContext));
             }
 
-            result = fact.createClass().newInstance();
         }
         catch (Exception e)
         {
@@ -138,7 +155,7 @@
         Class<?> superClass = null;
         for (Type generic : types)
         {
-            Class<?> type = (Class<?>)ClassUtil.getClazz(generic);
+            Class<?> type = ClassUtil.getClazz(generic);
             
             if (type.isInterface())
             {

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/business/tests/DependingInterceptorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/business/tests/DependingInterceptorTest.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/business/tests/DependingInterceptorTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/business/tests/DependingInterceptorTest.java
Fri Feb 19 21:55:05 2010
@@ -92,7 +92,7 @@
         Assert.assertEquals(2, TransactionInterceptor.coount);
         
         Assert.assertNotSame(realInstance1, realInstance2);
-        Assert.assertSame(realInstance1.getMyService(), realInstance2.getMyService());
+        Assert.assertEquals(realInstance1.getMyService().getJ(), realInstance2.getMyService().getJ());
         
         shutDownContainer();
         

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java?rev=912000&r1=911999&r2=912000&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/portable/events/MyExtension.java
Fri Feb 19 21:55:05 2010
@@ -64,7 +64,6 @@
     
     public MyExtension()
     {
-        reset();
     }
 
     public void observeBeforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBean) 



Mime
View raw message