tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1178360 - in /openejb/trunk/openejb: container/openejb-core/src/main/java/org/apache/openejb/ container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/
Date Mon, 03 Oct 2011 09:45:40 GMT
Author: rmannibucau
Date: Mon Oct  3 09:45:40 2011
New Revision: 1178360

URL: http://svn.apache.org/viewvc?rev=1178360&view=rev
Log:
managing dynamic implementation proxies by cdi + allowing to use interceptors on them

Added:
    openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialInterceptor.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
    openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialBean.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java?rev=1178360&r1=1178359&r2=1178360&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
Mon Oct  3 09:45:40 2011
@@ -41,6 +41,7 @@ import org.apache.openejb.util.Index;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.proxy.DynamicProxyImplFactory;
+import org.apache.openejb.util.proxy.QueryProxy;
 import org.apache.webbeans.component.AbstractInjectionTargetBean;
 import org.apache.webbeans.component.InjectionTargetBean;
 import org.apache.webbeans.config.WebBeansContext;
@@ -62,6 +63,7 @@ import javax.enterprise.context.spi.Crea
 import javax.enterprise.inject.spi.Bean;
 import javax.naming.Context;
 import javax.persistence.EntityManagerFactory;
+import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
@@ -1227,25 +1229,35 @@ public class BeanContext extends Deploym
 		this.statefulTimeout = statefulTimeout;
 	}
 
+
+    public Class<Object> getManagedClass() {
+        if (isDynamicallyImplemented()) {
+            return (Class<Object>) getProxyClass();
+        }
+        return beanClass;
+    }
+
     public InstanceContext newInstance() throws Exception {
         ThreadContext callContext = new ThreadContext(this, null, Operation.INJECTION);
         ThreadContext oldContext = ThreadContext.enter(callContext);
 
         WebBeansContext webBeansContext = null;
         AbstractInjectionTargetBean<Object> beanDefinition = null;
-        ConstructorInjectionBean<Object> beanConstructor = null;
-        if (!isDynamicallyImplemented()) { // not a dynamic proxy implementation
-            webBeansContext = getModuleContext().getAppContext().getWebBeansContext();
-
-            beanDefinition = get(CdiEjbBean.class);
 
-            beanConstructor = new ConstructorInjectionBean<Object>(webBeansContext,
beanClass);
+        webBeansContext = getModuleContext().getAppContext().getWebBeansContext();
+        beanDefinition = get(CdiEjbBean.class);
 
-            if (beanDefinition == null) {
-                beanDefinition = beanConstructor;
+        if (isDynamicallyImplemented()) {
+            if (!InvocationHandler.class.isAssignableFrom(getProxyClass())) {
+                throw new OpenEJBException("proxy class can only be InvocationHandler");
             }
         }
 
+        ConstructorInjectionBean<Object> beanConstructor = new ConstructorInjectionBean<Object>(webBeansContext,
getManagedClass());
+        if (beanDefinition == null) {
+            beanDefinition = beanConstructor;
+        }
+
         try {
             final Context ctx = this.getJndiEnc();
             final Class beanClass = this.getBeanClass();
@@ -1253,20 +1265,20 @@ public class BeanContext extends Deploym
             CurrentCreationalContext<Object> currentCreationalContext = get(CurrentCreationalContext.class);
             CreationalContext<Object> creationalContext = (currentCreationalContext
!= null) ? currentCreationalContext.get() : null;
 
-            if (creationalContext == null && !isDynamicallyImplemented()) {
+            if (creationalContext == null) {
                 creationalContext = webBeansContext.getBeanManagerImpl().createCreationalContext(beanDefinition);
             }
 
             // Create bean instance
             final Object beanInstance;
+            final InjectionProcessor injectionProcessor = new InjectionProcessor(beanConstructor.create(creationalContext),
this.getInjections(), InjectionProcessor.unwrap(ctx));
             if (!isDynamicallyImplemented()) {
-                final InjectionProcessor injectionProcessor = new InjectionProcessor(beanConstructor.create(creationalContext),
this.getInjections(), InjectionProcessor.unwrap(ctx));
-
                 beanInstance = injectionProcessor.createInstance();
-
                 inject(beanInstance, creationalContext);
             } else {
-                beanInstance = DynamicProxyImplFactory.newProxy(this);
+                InvocationHandler handler = (InvocationHandler) injectionProcessor.createInstance();
+                beanInstance = DynamicProxyImplFactory.newProxy(this, handler);
+                inject(handler, creationalContext);
             }
 
             // Create interceptors
@@ -1356,7 +1368,7 @@ public class BeanContext extends Deploym
 
         AbstractInjectionTargetBean<Object> beanDefinition = get(CdiEjbBean.class);
 
-        final ConstructorInjectionBean<Object> beanConstructor = new ConstructorInjectionBean<Object>(webBeansContext,
beanClass);
+        final ConstructorInjectionBean<Object> beanConstructor = new ConstructorInjectionBean<Object>(webBeansContext,
getManagedClass());
 
         if (beanDefinition == null) {
             beanDefinition = beanConstructor;
@@ -1444,6 +1456,9 @@ public class BeanContext extends Deploym
     }
 
     public Class<?> getProxyClass() {
+        if (isDynamicallyImplemented() && proxyClass == null) {
+            return QueryProxy.class;
+        }
         return proxyClass;
     }
 

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java?rev=1178360&r1=1178359&r2=1178360&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
Mon Oct  3 09:45:40 2011
@@ -18,14 +18,12 @@
 package org.apache.openejb.util.proxy;
 
 import org.apache.openejb.BeanContext;
-import org.apache.openejb.Injection;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 
 import javax.naming.NamingException;
 import javax.persistence.EntityManager;
 import java.lang.reflect.Method;
-import java.util.List;
 
 /**
  * @author rmannibucau
@@ -33,29 +31,9 @@ import java.util.List;
 public class DynamicProxyImplFactory {
     private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, BeanContext.class);
 
-    public static Object newProxy(BeanContext context) {
-        java.lang.reflect.InvocationHandler invocationHandler = null;
-        if (context.getProxyClass() != null) {
-            Class<?> proxyClass = context.getProxyClass();
-            if (java.lang.reflect.InvocationHandler.class.isAssignableFrom(context.getProxyClass()))
{
-                try {
-                    invocationHandler = (java.lang.reflect.InvocationHandler) proxyClass.newInstance();
-                } catch (InstantiationException e) {
-                    LOGGER.warning("can't instantiate " + proxyClass.getName(), e);
-                } catch (IllegalAccessException e) {
-                    LOGGER.warning("can't access " + proxyClass.getName(), e);
-                }
-            }
-        }
-
-        // by default QueryProxy is used
-        if (invocationHandler == null) {
-            List<Injection> injection = context.getInjections(); // the entity manager
-            if (injection.size() < 1) {
-                throw new RuntimeException("a query dynamic bean should have at least one
PersistenceContext annotation");
-            }
-
-            String emLookupName = injection.get(injection.size() - 1).getJndiName();
+    public static Object newProxy(BeanContext context, java.lang.reflect.InvocationHandler
invocationHandler) {
+        if (invocationHandler instanceof QueryProxy) {
+            String emLookupName = context.getInjections().get(context.getInjections().size()
- 1).getJndiName();
             EntityManager em;
             try {
                 em = (EntityManager) context.getJndiEnc().lookup(emLookupName);
@@ -63,7 +41,7 @@ public class DynamicProxyImplFactory {
                 throw new RuntimeException("a dynamic bean should reference at least one
correct PersistenceContext", e);
             }
 
-            invocationHandler = new QueryProxy(em);
+            ((QueryProxy) invocationHandler).setEntityManager(em);
         }
 
         try {

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java?rev=1178360&r1=1178359&r2=1178360&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
Mon Oct  3 09:45:40 2011
@@ -71,7 +71,7 @@ public class QueryProxy implements Invoc
         NAMED, NATIVE, OTHER
     }
 
-    public QueryProxy(EntityManager entityManager) {
+    public void setEntityManager(EntityManager entityManager) {
         em = entityManager;
     }
 

Modified: openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialBean.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialBean.java?rev=1178360&r1=1178359&r2=1178360&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialBean.java
(original)
+++ openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialBean.java
Mon Oct  3 09:45:40 2011
@@ -19,12 +19,14 @@ package org.superbiz.dynamic;
 import org.apache.openejb.api.Proxy;
 
 import javax.ejb.Singleton;
+import javax.interceptor.Interceptors;
 
 /**
  * @author rmannibucau
  */
 @Singleton
 @Proxy(SocialHandler.class)
+@Interceptors(SocialInterceptor.class)
 public interface SocialBean {
     public String facebookStatus();
     public String twitterStatus();

Added: openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialInterceptor.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialInterceptor.java?rev=1178360&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialInterceptor.java
(added)
+++ openejb/trunk/openejb/examples/dynamic-implementation/src/main/java/org/superbiz/dynamic/SocialInterceptor.java
Mon Oct  3 09:45:40 2011
@@ -0,0 +1,24 @@
+package org.superbiz.dynamic;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author rmannibucau
+ */
+public class SocialInterceptor {
+    @AroundInvoke public Object around(InvocationContext context) throws Exception {
+        String mtd = context.getMethod().getName();
+        String address;
+        if (mtd.toLowerCase().contains("facebook")) {
+            address = "http://www.facebook.com";
+        } else if (mtd.toLowerCase().contains("twitter")) {
+            address = "http://twitter.com";
+        } else {
+            address ="no website for you";
+        }
+
+        System.out.println("go on " + address);
+        return context.proceed();
+    }
+}



Mime
View raw message