openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r918993 - in /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans: intercept/ proxy/
Date Thu, 04 Mar 2010 14:07:50 GMT
Author: struberg
Date: Thu Mar  4 14:07:50 2010
New Revision: 918993

URL: http://svn.apache.org/viewvc?rev=918993&view=rev
Log:
OWB-314 cache @ApplicationScoped contextual instances in MethodHandler

this speeds up my typical application to ~ x4

Added:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/ApplicationScopedBeanIntereptorHandler.java
Modified:
    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

Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/ApplicationScopedBeanIntereptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/ApplicationScopedBeanIntereptorHandler.java?rev=918993&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/ApplicationScopedBeanIntereptorHandler.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/ApplicationScopedBeanIntereptorHandler.java
Thu Mar  4 14:07:50 2010
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.intercept;
+
+import javax.enterprise.context.spi.CreationalContext;
+
+import org.apache.webbeans.component.OwbBean;
+
+
+/**
+ * <p>This is a {@link javassist.util.proxy.MethodHandler.MethodHandler} especially
+ * made for &#064;ApplicationScoped beans.</p>
+ * 
+ * <p>Since there is only one single contextual instance of an &#064;ApplicationScoped
bean,
+ * we can simply cache this instance inside our bean. We only need to reload this instance
+ * if it is null. This happens at the first usage and after the MethodHandler got deserialized</p>

+ */
+public class ApplicationScopedBeanIntereptorHandler extends NormalScopedBeanInterceptorHandler

+{
+
+    private static final long serialVersionUID = 2113381529723088865L;
+
+    private Object cachedInstance = null;
+    
+    public ApplicationScopedBeanIntereptorHandler(OwbBean<?> bean, CreationalContext<?>
creationalContext)
+    {
+        super(bean, creationalContext);
+    }
+    
+    protected Object getContextualInstance(OwbBean<Object> bean, CreationalContext<Object>
cc)
+    {
+        if (cachedInstance == null) 
+        {
+            cachedInstance = super.getContextualInstance(bean, cc);
+        }
+        
+        return cachedInstance;
+    }
+}

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=918993&r1=918992&r2=918993&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
Thu Mar  4 14:07:50 2010
@@ -153,9 +153,9 @@
     // TODO this proxy cache should get moved to JavassistProxyFactory
     private static Map<OwbBean<?>, Class<?>> interceptorProxyClasses =
new ConcurrentHashMap<OwbBean<?>, Class<?>>();
 
-    protected InterceptorHandler(OwbBean<?> bean2)
+    protected InterceptorHandler(OwbBean<?> bean)
     {
-        this.bean = bean2;
+        this.bean = bean;
     }
 
     public Object invoke(Object instance, Method method, Method proceed, Object[] arguments,
CreationalContextImpl<?> ownerCreationalContext) throws Exception

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=918993&r1=918992&r2=918993&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
Thu Mar  4 14:07:50 2010
@@ -34,6 +34,7 @@
     private static final long serialVersionUID = -7169354477951284657L;
 
     private CreationalContext<?> creationalContext;
+    private BeanManagerImpl beanManager;
 
     public NormalScopedBeanInterceptorHandler(OwbBean<?> bean, CreationalContext<?>
creationalContext)
     {
@@ -41,28 +42,47 @@
         this.creationalContext = creationalContext;
     }
     
-    @SuppressWarnings("unchecked")
     @Override
     public Object invoke(Object instance, Method method, Method proceed, Object[] arguments)
throws Exception
     {
-        BeanManagerImpl beanManager = BeanManagerImpl.getManager();
-
-        //Context of the bean
-        Context webbeansContext = beanManager.getContext(bean.getScope());
-
-        //Get bean instance from context
-        Object webbeansInstance = webbeansContext.get((Contextual<Object>)this.bean,
(CreationalContext<Object>) creationalContext);
-
+        @SuppressWarnings("unchecked")
+        Object webbeansInstance = getContextualInstance((OwbBean<Object>) bean, (CreationalContext<Object>)
creationalContext);
         return super.invoke(webbeansInstance, method, proceed, arguments, (CreationalContextImpl<?>)
creationalContext);
     }
     
     protected <T> Object callAroundInvokes(Method proceed, Object[] arguments, List<InterceptorData>
stack) throws Exception
     {
-        InvocationContextImpl impl = new InvocationContextImpl(this.bean, null,proceed, arguments,
stack, InterceptorType.AROUND_INVOKE);
+        InvocationContextImpl impl = new InvocationContextImpl(bean, 
+                                                               getContextualInstance((OwbBean<Object>)
bean, (CreationalContext<Object>)creationalContext),
+                                                               proceed, arguments, stack,
InterceptorType.AROUND_INVOKE);
+        impl.setCreationalContext(creationalContext);
 
         return impl.proceed();
 
     }
     
+    protected BeanManagerImpl getBeanManager()
+    {
+        if (beanManager == null)
+        {
+            beanManager = BeanManagerImpl.getManager();
+        }
+        return beanManager;
+    }
     
+    /**
+     * @param bean
+     * @param cc the CreationalContext
+     * @return the underlying contextual instance, either cached or resolved from the context

+     */
+    protected Object getContextualInstance(OwbBean<Object> bean, CreationalContext<Object>
cc)
+    {
+        //Context of the bean
+        Context webbeansContext = getBeanManager().getContext(bean.getScope());
+
+        //Get bean instance from context
+        @SuppressWarnings("unchecked")
+        Object webbeansInstance = webbeansContext.get((Contextual<Object>)this.bean,
(CreationalContext<Object>) creationalContext);
+        return webbeansInstance;
+    }
 }

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=918993&r1=918992&r2=918993&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
Thu Mar  4 14:07:50 2010
@@ -26,6 +26,7 @@
 import javassist.util.proxy.ProxyFactory;
 import javassist.util.proxy.ProxyObject;
 
+import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.Decorator;
@@ -35,6 +36,7 @@
 import org.apache.webbeans.component.InjectionTargetBean;
 import org.apache.webbeans.decorator.WebBeansDecorator;
 import org.apache.webbeans.exception.WebBeansException;
+import org.apache.webbeans.intercept.ApplicationScopedBeanIntereptorHandler;
 import org.apache.webbeans.intercept.DependentScopedBeanInterceptorHandler;
 import org.apache.webbeans.intercept.InterceptorData;
 import org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler;
@@ -70,7 +72,13 @@
             
             if (!(bean instanceof WebBeansDecorator<?>) && !(bean instanceof
WebBeansInterceptor<?>))
             {
-                ((ProxyObject)result).setHandler(new NormalScopedBeanInterceptorHandler(bean,
creationalContext));
+                if (bean.getScope().equals(ApplicationScoped.class)){
+                    ((ProxyObject)result).setHandler(new ApplicationScopedBeanIntereptorHandler(bean,
creationalContext));
+                }
+                else 
+                {
+                    ((ProxyObject)result).setHandler(new NormalScopedBeanInterceptorHandler(bean,
creationalContext));
+                }
             }
         }
         catch (Exception e)



Mime
View raw message