[weld-commits] Weld SVN: r6582 - in extensions/trunk/src: main/java/org/jboss/weld/extensions/autoproxy and 3 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Mon Jun 28 02:24:39 EDT 2010


Author: swd847
Date: 2010-06-28 02:24:38 -0400 (Mon, 28 Jun 2010)
New Revision: 6582

Added:
   extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/
   extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java
   extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/DelegatingInjectionTarget.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoproxyTest.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloBob.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloGenerator.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloJane.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloService.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloServiceHandler.java
   extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/PersonName.java
Modified:
   extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
add experimental autoproxy support



Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.weld.extensions.autoproxy;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.InvocationHandler;
+
+/**
+ * If this annotation is applied to an interface it signifies that any 
+ * sub-interfaces that are seen in ProcessAnnotatedType should be proxied
+ * and registered as a Bean
+ * 
+ * @author Stuart Douglas <stuart at baileyroberts.com.au>
+ *
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.TYPE)
+ at Documented
+public @interface AutoProxy
+{
+   /**
+    * the proxy handler to use
+    */
+   Class<? extends InvocationHandler> value();
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.weld.extensions.autoproxy;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationHandler;
+
+import javax.enterprise.context.spi.CreationalContext;
+
+import org.jboss.weld.extensions.bean.BeanImpl;
+import org.jboss.weld.extensions.bean.BeanLifecycle;
+
+public class AutoProxyBeanLifecycle<T> implements BeanLifecycle<T>
+{
+
+   private final Class<? extends T> proxyClass;
+   private final Class<? extends InvocationHandler> handler;
+   private final Constructor<? extends T> constructor;
+
+   public AutoProxyBeanLifecycle(Class<? extends T> proxyClass, Class<? extends InvocationHandler> handler)
+   {
+      this.proxyClass = proxyClass;
+      this.handler = handler;
+      try
+      {
+         constructor = proxyClass.getConstructor(InvocationHandler.class);
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public T create(BeanImpl<T> bean, CreationalContext<T> creationalContext)
+   {
+      try
+      {
+         Object hinst = handler.newInstance();
+         T instance = constructor.newInstance(hinst);
+         creationalContext.push(instance);
+         bean.getInjectionTarget().inject(instance, creationalContext);
+         bean.getInjectionTarget().postConstruct(instance);
+         return instance;
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public void destroy(BeanImpl<T> bean, T instance, CreationalContext<T> creationalContext)
+   {
+      bean.getInjectionTarget().preDestroy(instance);
+      creationalContext.release();
+   }
+
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,50 @@
+package org.jboss.weld.extensions.autoproxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+
+import org.jboss.weld.extensions.bean.BeanBuilder;
+
+public class AutoProxyExtension implements Extension
+{
+   Set<Bean<?>> beans = new HashSet<Bean<?>>();
+   
+   public <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> event, BeanManager beanManager)
+   {
+
+      for(Class<?> c : event.getAnnotatedType().getJavaClass().getInterfaces())
+      {
+         if (c.isAnnotationPresent(AutoProxy.class))
+         {
+            AutoProxy an = c.getAnnotation(AutoProxy.class);
+            Class<? extends InvocationHandler> handlerClass = an.value();
+            Class<? extends X> proxyClass = (Class<? extends X>) Proxy.getProxyClass(event.getAnnotatedType().getJavaClass().getClassLoader(), event.getAnnotatedType().getJavaClass());
+            BeanBuilder<X> builder = new BeanBuilder<X>(event.getAnnotatedType(), beanManager).defineBeanFromAnnotatedType();
+            InjectionTarget<?> tgt = beanManager.createInjectionTarget(beanManager.createAnnotatedType(handlerClass));
+            DelegatingInjectionTarget<X> delegatingTarget = new DelegatingInjectionTarget(tgt);
+            builder.setInjectionTarget(delegatingTarget);
+            AutoProxyBeanLifecycle<X> life = new AutoProxyBeanLifecycle<X>(proxyClass, handlerClass);
+            builder.setBeanLifecycle(life);
+            beans.add(builder.create());
+         }
+      }
+   }
+
+   public void afterBeanDiscovery(@Observes AfterBeanDiscovery event)
+   {
+      for (Bean<?> b : beans)
+      {
+         event.addBean(b);
+      }
+   }
+}

Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/DelegatingInjectionTarget.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/DelegatingInjectionTarget.java	                        (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/autoproxy/DelegatingInjectionTarget.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,60 @@
+package org.jboss.weld.extensions.autoproxy;
+
+import java.lang.reflect.Proxy;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+/**
+ * class that injects the proxy handler rather than a proxy
+ * @author Stuart Douglas <stuart at baileyroberts.com.au>
+ *
+ * @param <T>
+ */
+public class DelegatingInjectionTarget<T> implements InjectionTarget<T>
+{
+
+   private final InjectionTarget delegate;
+
+   public DelegatingInjectionTarget(InjectionTarget<T> delegate)
+   {
+      this.delegate = delegate;
+   }
+
+   public void inject(T instance, CreationalContext<T> ctx)
+   {
+      Object handler = Proxy.getInvocationHandler(instance);
+      delegate.inject(handler, ctx);
+   }
+
+   public void postConstruct(T instance)
+   {
+      Object handler = Proxy.getInvocationHandler(instance);
+      delegate.postConstruct(handler);
+   }
+
+   public void preDestroy(T instance)
+   {
+      Object handler = Proxy.getInvocationHandler(instance);
+      delegate.preDestroy(handler);
+   }
+
+   public void dispose(T instance)
+   {
+      Object handler = Proxy.getInvocationHandler(instance);
+      delegate.dispose(handler);
+   }
+
+   public Set<InjectionPoint> getInjectionPoints()
+   {
+      return delegate.getInjectionPoints();
+   }
+
+   public T produce(CreationalContext<T> ctx)
+   {
+      throw new RuntimeException("Not applicable");
+   }
+
+}

Modified: extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
--- extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-06-28 06:23:02 UTC (rev 6581)
+++ extensions/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-06-28 06:24:38 UTC (rev 6582)
@@ -3,4 +3,5 @@
 org.jboss.weld.extensions.bean.lookup.IdentifiableBeanExtension
 org.jboss.weld.extensions.core.CoreExtension
 org.jboss.weld.extensions.interceptor.InterceptorExtension
-org.jboss.weld.extensions.managedproducer.ManagedProducerExtension
\ No newline at end of file
+org.jboss.weld.extensions.managedproducer.ManagedProducerExtension
+org.jboss.weld.extensions.autoproxy.AutoProxyExtension
\ No newline at end of file

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoproxyTest.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoproxyTest.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoproxyTest.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,25 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import javax.enterprise.inject.Default;
+import javax.enterprise.util.AnnotationLiteral;
+
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.weld.test.AbstractWeldTest;
+import org.testng.annotations.Test;
+
+ at Artifact
+public class AutoproxyTest extends AbstractWeldTest
+{
+
+   @Test
+   public void testAutoProxy()
+   {
+      HelloBob bob = getReference(HelloBob.class, new AnnotationLiteral<Default>()
+      {
+      });
+      assert bob.sayHello().equals("Hello Bob");
+      HelloJane jane = getReference(HelloJane.class);
+      assert jane.sayHello().equals("Hello Jane");
+   }
+
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloBob.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloBob.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloBob.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,10 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import javax.enterprise.inject.Default;
+
+ at Default
+public interface HelloBob extends HelloService
+{
+   @PersonName("Bob")
+   public String sayHello();
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloGenerator.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloGenerator.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloGenerator.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,9 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+public class HelloGenerator
+{
+   public String getHelloString()
+   {
+      return "Hello ";
+   }
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloJane.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloJane.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloJane.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,10 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import javax.enterprise.inject.Default;
+
+ at Default
+public interface HelloJane extends HelloService
+{
+   @PersonName("Jane")
+   public String sayHello();
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloService.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloService.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloService.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,9 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import org.jboss.weld.extensions.autoproxy.AutoProxy;
+
+ at AutoProxy(HelloServiceHandler.class)
+public interface HelloService
+{
+
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloServiceHandler.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloServiceHandler.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloServiceHandler.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,19 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+import javax.inject.Inject;
+
+public class HelloServiceHandler implements InvocationHandler
+{
+
+   @Inject
+   HelloGenerator generator;
+
+   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+   {
+      return generator.getHelloString() + method.getAnnotation(PersonName.class).value();
+   }
+
+}

Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/PersonName.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/PersonName.java	                        (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/autoproxy/PersonName.java	2010-06-28 06:24:38 UTC (rev 6582)
@@ -0,0 +1,10 @@
+package org.jboss.weld.extensions.test.autoproxy;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface PersonName
+{
+   String value();
+}



More information about the weld-commits mailing list