From 35450d109bbc2fd775d10678958e55eb3e13477c Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 18:10:08 +1000 Subject: [PATCH 1/6] add .gitignore --- .gitignore | 1 + docs/.gitignore | 1 + impl/.gitignore | 1 + 3 files changed, 3 insertions(+) create mode 100644 .gitignore create mode 100644 docs/.gitignore create mode 100644 impl/.gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e970233 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.project \ No newline at end of file diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 0000000..e970233 --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1 @@ +.project \ No newline at end of file diff --git a/impl/.gitignore b/impl/.gitignore new file mode 100644 index 0000000..e970233 --- /dev/null +++ b/impl/.gitignore @@ -0,0 +1 @@ +.project \ No newline at end of file From aae36012d04d67d7318808adf4938d374d1e082b Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 20:45:28 +1000 Subject: [PATCH 2/6] First implementation of AutoProxy --- .../weld/extensions/autoproxy/AutoProxy.java | 52 +++++++++ .../autoproxy/AutoProxyBeanLifecycle.java | 85 ++++++++++++++ .../autoproxy/AutoProxyExtension.java | 69 ++++++++++++ .../autoproxy/AutoProxyHandler.java | 103 +++++++++++++++++ .../autoproxy/AutoProxyMethodHandler.java | 105 ++++++++++++++++++ .../test/autoproxy/AutoProxyTest.java | 52 +++++++++ .../test/autoproxy/EchoService.java | 29 +++++ .../test/autoproxy/EchoServiceHandler.java | 29 +++++ .../extensions/test/autoproxy/HelloWorld.java | 7 ++ 9 files changed, 531 insertions(+) create mode 100644 impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java create mode 100644 impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java create mode 100644 impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java create mode 100644 impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java create mode 100644 impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java new file mode 100644 index 0000000..10cff1e --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java @@ -0,0 +1,52 @@ +/* + * 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; + +/** + * Meta annotation that is used to specify an invocation handler for an + * automatically implmented bean. + * + * If the annotation that is annotated with this meta-annotation is applied to + * an interface or abstract class then the container will automatically provide + * a concrete implementation of the class/interface, and delegate all calls to + * abstract methods through the handler class specified by this annotations. + * + * The handler class must have a method with the following signature: + * + *
+ *  @AroundInvoke public Object aroundInvoke(final InvocationContext invocation) throws Exception
+ * 
+ * + * This is the same as an intercepter class. This handler can be injected into + * and use initializer methods + * + * @author Stuart Douglas + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.ANNOTATION_TYPE) +@Documented +public @interface AutoProxy +{ + Class value(); +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java new file mode 100644 index 0000000..954f987 --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java @@ -0,0 +1,85 @@ +/* + * 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.Method; + +import javassist.util.proxy.MethodFilter; +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.BeanManager; + +import org.jboss.weld.extensions.bean.BeanLifecycle; + +public class AutoProxyBeanLifecycle implements BeanLifecycle +{ + private final ProxyFactory factory; + private final Class proxyClass; + private final AutoProxyHandler handler; + + public AutoProxyBeanLifecycle(Class classToImplement, Class handlerClass, BeanManager manager) + { + handler = new AutoProxyHandler(handlerClass, manager); + + // create the proxy + factory = new ProxyFactory(); + if (classToImplement.isInterface()) + { + Class[] interfaces = new Class[1]; + interfaces[0] = classToImplement; + factory.setInterfaces(interfaces); + } + else + { + factory.setSuperclass(classToImplement); + } + factory.setFilter(new MethodFilter() + { + public boolean isHandled(Method m) + { + // ignore finalize() + return !m.getName().equals("finalize"); + } + }); + proxyClass = factory.createClass(); + } + + public T create(Bean bean, CreationalContext creationalContext) + { + try + { + H handlerInstance = handler.create((CreationalContext) creationalContext); + AutoProxyMethodHandler methodHandler = new AutoProxyMethodHandler(handler, handlerInstance); + T instance = proxyClass.newInstance(); + ((ProxyObject) instance).setHandler(methodHandler); + return instance; + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + + public void destroy(Bean bean, T instance, CreationalContext creationalContext) + { + // handler.dispose(instance, (CreationalContext) creationalContext); + } + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java new file mode 100644 index 0000000..a7dbb0d --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java @@ -0,0 +1,69 @@ +/* + * 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.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.ProcessAnnotatedType; + +import org.jboss.weld.extensions.bean.BeanBuilder; +import org.jboss.weld.extensions.util.AnnotationInspector; + +/** + * This extension provides an implementation of interfaces and abstract classes. + * + * @author Stuart Douglas + * + */ +public class AutoProxyExtension implements Extension +{ + Set> beans = new HashSet>(); + + public void processAnnotatedType(@Observes ProcessAnnotatedType event, BeanManager beanManager) + { + AutoProxy annotation = AnnotationInspector.getMetaAnnotation(event.getAnnotatedType(), AutoProxy.class); + if (annotation != null) + { + Class handlerClass = annotation.value(); + try + { + BeanBuilder builder = new BeanBuilder(beanManager); + builder.defineBeanFromAnnotatedType(event.getAnnotatedType()); + builder.setBeanLifecycle(new AutoProxyBeanLifecycle(event.getAnnotatedType().getJavaClass(), handlerClass, beanManager)); + beans.add(builder.create()); + } + catch (IllegalArgumentException e) + { + throw new RuntimeException(e); + } + } + } + + public void afterBeanDiscovery(@Observes AfterBeanDiscovery event) + { + for (Bean b : beans) + { + event.addBean(b); + } + } +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java new file mode 100644 index 0000000..fe31a9b --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java @@ -0,0 +1,103 @@ +/* + * 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.Method; +import java.util.Set; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionTarget; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder; +import org.jboss.weld.extensions.util.Reflections; + +/** + * Manages the handler for the auto proxy extension. This class is responsible + * for managing the lifecycle of the handler class instances + * + * @author Stuart Douglas + * + */ +public class AutoProxyHandler +{ + private final Class handlerClass; + private final Method handlerMethod; + private final InjectionTarget injectionTarget; + + /** + * Creates a wrapper around an AutoProxy handler class + * + * @param handlerClass The handler class + * @throws IllegalArgumentException if the handler class is does not have a + * suitable @AroundInvoke method + */ + public AutoProxyHandler(Class handlerClass, BeanManager beanManager) throws IllegalArgumentException + { + this.handlerClass = handlerClass; + Set methods = Reflections.getAllMethods(handlerClass); + Method handler = null; + //search for the handler method + for (Method m : methods) + { + if (m.isAnnotationPresent(AroundInvoke.class)) + { + if (m.getParameterTypes().length != 1 || m.getParameterTypes()[0] != InvocationContext.class) + { + throw new IllegalArgumentException("Could not find suitable AroundInvoke method on class " + handlerClass + " methods denoted @AroundInvoke must have a single InvokationContext parameter"); + } + handler = m; + break; + } + } + if (handler == null) + { + throw new IllegalArgumentException("Could not find suitable AroundInvoke method on class " + handlerClass); + } + handlerMethod = handler; + //now create the InjectionTarget + AnnotatedTypeBuilder typeBuilder = new AnnotatedTypeBuilder().readFromType(handlerClass); + injectionTarget = beanManager.createInjectionTarget(typeBuilder.create()); + } + + public T create(CreationalContext ctx) + { + T instance = injectionTarget.produce(ctx); + injectionTarget.inject(instance, ctx); + injectionTarget.postConstruct(instance); + return instance; + } + + public void dispose(T instance, CreationalContext ctx) + { + injectionTarget.dispose(instance); + ctx.release(); + } + + public Object invoke(Object instance, InvocationContext ctx) throws Exception + { + return handlerMethod.invoke(instance, ctx); + } + + public Class getHandlerClass() + { + return handlerClass; + } + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java new file mode 100644 index 0000000..8e48e8f --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java @@ -0,0 +1,105 @@ +/* + * 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.Method; +import java.util.Collections; +import java.util.Map; + +import javassist.util.proxy.MethodHandler; + +import javax.interceptor.InvocationContext; + +/** + * MethodHandler that forwards calls to abstract methods to handler instance + * + * @author Stuart Douglas + * + */ +public class AutoProxyMethodHandler implements MethodHandler +{ + + private final AutoProxyHandler handler; + private final H handlerInstance; + + public AutoProxyMethodHandler(AutoProxyHandler handler, H handlerInstance) + { + this.handler = handler; + this.handlerInstance = handlerInstance; + } + + public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable + { + if (proceed == null) + { + return handler.invoke(handlerInstance, new InvocationContextImpl(self, thisMethod, args)); + } + return proceed.invoke(self, args); + } + + private final class InvocationContextImpl implements InvocationContext + { + + public InvocationContextImpl(Object target, Method method, Object[] params) + { + this.target = target; + this.method = method; + this.params = params; + } + + private final Object target; + private final Method method; + private final Object[] params; + + public Map getContextData() + { + return Collections.emptyMap(); + } + + public Method getMethod() + { + return method; + } + + public Object[] getParameters() + { + return params; + } + + public Object getTarget() + { + return target; + } + + public Object getTimer() + { + return null; + } + + public Object proceed() throws Exception + { + throw new UnsupportedOperationException("Cannot call proceed() in AutoProxy invocation handler"); + } + + public void setParameters(Object[] params) + { + throw new UnsupportedOperationException("Cannot call setParameters() in AutoProxy invocation handler"); + } + + } + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java new file mode 100644 index 0000000..2294fb2 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java @@ -0,0 +1,52 @@ +/* + * 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.test.autoproxy; + +import javax.inject.Inject; + +import org.jboss.arquillian.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Stuart Douglas + * + */ +@RunWith(Arquillian.class) +public class AutoProxyTest +{ + + @Inject + HelloWorld helloWorld; + + @Deployment + public static Archive deploy() + { + return ShrinkWrap.create("test.jar", JavaArchive.class).addPackage(AutoProxyTest.class.getPackage()); + } + + @Test + public void testExact() + { + assert helloWorld.helloWorld().equals("helloWorld"); + } + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java new file mode 100644 index 0000000..d7ec63f --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java @@ -0,0 +1,29 @@ +/* + * 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.test.autoproxy; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.jboss.weld.extensions.autoproxy.AutoProxy; + +@Retention(RetentionPolicy.RUNTIME) +@AutoProxy(EchoServiceHandler.class) +public @interface EchoService +{ + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java new file mode 100644 index 0000000..c69cf2e --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java @@ -0,0 +1,29 @@ +/* + * 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.test.autoproxy; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +public class EchoServiceHandler +{ + @AroundInvoke + public Object invoke(InvocationContext ctx) + { + return ctx.getMethod().getName().toString(); + } +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java new file mode 100644 index 0000000..ff9ba87 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java @@ -0,0 +1,7 @@ +package org.jboss.weld.extensions.test.autoproxy; + +@EchoService +public interface HelloWorld +{ + public String helloWorld(); +} From 821e23f773e6f2d4ccb68fc900a159fe49d7f4c0 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 21:06:39 +1000 Subject: [PATCH 3/6] rename @AutoProxy to @ServiceHandler --- docs/.gitignore | 4 +- impl/.gitignore | 5 ++- .../ServiceHandler.java} | 4 +- .../ServiceHandlerBeanLifecycle.java} | 12 +++--- .../ServiceHandlerExtension.java} | 8 ++-- .../ServiceHandlerManager.java} | 6 +-- .../ServiceHandlerMethodHandler.java} | 8 ++-- .../extensions/util/AnnotationInspector.java | 39 +++++++++++++++++++ .../javax.enterprise.inject.spi.Extension | 1 + .../EchoService.java | 6 +-- .../EchoServiceHandler.java | 2 +- .../HelloWorld.java | 2 +- .../ServiceHandlerTest.java} | 6 +-- 13 files changed, 74 insertions(+), 29 deletions(-) rename impl/src/main/java/org/jboss/weld/extensions/{autoproxy/AutoProxy.java => servicehandler/ServiceHandler.java} (95%) rename impl/src/main/java/org/jboss/weld/extensions/{autoproxy/AutoProxyBeanLifecycle.java => servicehandler/ServiceHandlerBeanLifecycle.java} (83%) rename impl/src/main/java/org/jboss/weld/extensions/{autoproxy/AutoProxyExtension.java => servicehandler/ServiceHandlerExtension.java} (85%) rename impl/src/main/java/org/jboss/weld/extensions/{autoproxy/AutoProxyHandler.java => servicehandler/ServiceHandlerManager.java} (94%) rename impl/src/main/java/org/jboss/weld/extensions/{autoproxy/AutoProxyMethodHandler.java => servicehandler/ServiceHandlerMethodHandler.java} (90%) rename impl/src/test/java/org/jboss/weld/extensions/test/{autoproxy => servicehandler}/EchoService.java (85%) rename impl/src/test/java/org/jboss/weld/extensions/test/{autoproxy => servicehandler}/EchoServiceHandler.java (94%) rename impl/src/test/java/org/jboss/weld/extensions/test/{autoproxy => servicehandler}/HelloWorld.java (58%) rename impl/src/test/java/org/jboss/weld/extensions/test/{autoproxy/AutoProxyTest.java => servicehandler/ServiceHandlerTest.java} (91%) diff --git a/docs/.gitignore b/docs/.gitignore index e970233..f9a50e1 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -1 +1,3 @@ -.project \ No newline at end of file +.project +.settings +.classpath diff --git a/impl/.gitignore b/impl/.gitignore index e970233..68cde20 100644 --- a/impl/.gitignore +++ b/impl/.gitignore @@ -1 +1,4 @@ -.project \ No newline at end of file +.project +.settings +.classpath +target \ No newline at end of file diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java similarity index 95% rename from impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java rename to impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java index 10cff1e..d29cda0 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxy.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.autoproxy; +package org.jboss.weld.extensions.servicehandler; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -46,7 +46,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) @Documented -public @interface AutoProxy +public @interface ServiceHandler { Class value(); } diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java similarity index 83% rename from impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java rename to impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java index 954f987..8ad22f9 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyBeanLifecycle.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.autoproxy; +package org.jboss.weld.extensions.servicehandler; import java.lang.reflect.Method; @@ -28,15 +28,15 @@ import org.jboss.weld.extensions.bean.BeanLifecycle; -public class AutoProxyBeanLifecycle implements BeanLifecycle +public class ServiceHandlerBeanLifecycle implements BeanLifecycle { private final ProxyFactory factory; private final Class proxyClass; - private final AutoProxyHandler handler; + private final ServiceHandlerManager handler; - public AutoProxyBeanLifecycle(Class classToImplement, Class handlerClass, BeanManager manager) + public ServiceHandlerBeanLifecycle(Class classToImplement, Class handlerClass, BeanManager manager) { - handler = new AutoProxyHandler(handlerClass, manager); + handler = new ServiceHandlerManager(handlerClass, manager); // create the proxy factory = new ProxyFactory(); @@ -66,7 +66,7 @@ public T create(Bean bean, CreationalContext creationalContext) try { H handlerInstance = handler.create((CreationalContext) creationalContext); - AutoProxyMethodHandler methodHandler = new AutoProxyMethodHandler(handler, handlerInstance); + ServiceHandlerMethodHandler methodHandler = new ServiceHandlerMethodHandler(handler, handlerInstance); T instance = proxyClass.newInstance(); ((ProxyObject) instance).setHandler(methodHandler); return instance; diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java similarity index 85% rename from impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java rename to impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java index a7dbb0d..a730efa 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyExtension.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.autoproxy; +package org.jboss.weld.extensions.servicehandler; import java.util.HashSet; import java.util.Set; @@ -35,13 +35,13 @@ * @author Stuart Douglas * */ -public class AutoProxyExtension implements Extension +public class ServiceHandlerExtension implements Extension { Set> beans = new HashSet>(); public void processAnnotatedType(@Observes ProcessAnnotatedType event, BeanManager beanManager) { - AutoProxy annotation = AnnotationInspector.getMetaAnnotation(event.getAnnotatedType(), AutoProxy.class); + ServiceHandler annotation = AnnotationInspector.getMetaAnnotation(event.getAnnotatedType(), ServiceHandler.class); if (annotation != null) { Class handlerClass = annotation.value(); @@ -49,7 +49,7 @@ public void processAnnotatedType(@Observes ProcessAnnotatedType event, Be { BeanBuilder builder = new BeanBuilder(beanManager); builder.defineBeanFromAnnotatedType(event.getAnnotatedType()); - builder.setBeanLifecycle(new AutoProxyBeanLifecycle(event.getAnnotatedType().getJavaClass(), handlerClass, beanManager)); + builder.setBeanLifecycle(new ServiceHandlerBeanLifecycle(event.getAnnotatedType().getJavaClass(), handlerClass, beanManager)); beans.add(builder.create()); } catch (IllegalArgumentException e) diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java similarity index 94% rename from impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java rename to impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java index fe31a9b..9d5d278 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyHandler.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.autoproxy; +package org.jboss.weld.extensions.servicehandler; import java.lang.reflect.Method; import java.util.Set; @@ -35,7 +35,7 @@ * @author Stuart Douglas * */ -public class AutoProxyHandler +public class ServiceHandlerManager { private final Class handlerClass; private final Method handlerMethod; @@ -48,7 +48,7 @@ public class AutoProxyHandler * @throws IllegalArgumentException if the handler class is does not have a * suitable @AroundInvoke method */ - public AutoProxyHandler(Class handlerClass, BeanManager beanManager) throws IllegalArgumentException + public ServiceHandlerManager(Class handlerClass, BeanManager beanManager) throws IllegalArgumentException { this.handlerClass = handlerClass; Set methods = Reflections.getAllMethods(handlerClass); diff --git a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java similarity index 90% rename from impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java rename to impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java index 8e48e8f..6ddf50d 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/autoproxy/AutoProxyMethodHandler.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.autoproxy; +package org.jboss.weld.extensions.servicehandler; import java.lang.reflect.Method; import java.util.Collections; @@ -30,13 +30,13 @@ * @author Stuart Douglas * */ -public class AutoProxyMethodHandler implements MethodHandler +public class ServiceHandlerMethodHandler implements MethodHandler { - private final AutoProxyHandler handler; + private final ServiceHandlerManager handler; private final H handlerInstance; - public AutoProxyMethodHandler(AutoProxyHandler handler, H handlerInstance) + public ServiceHandlerMethodHandler(ServiceHandlerManager handler, H handlerInstance) { this.handler = handler; this.handlerInstance = handlerInstance; diff --git a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java index 6f59288..0b0cc69 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java +++ b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java @@ -1,9 +1,26 @@ +/* + * 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.util; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import javax.enterprise.inject.Stereotype; +import javax.enterprise.inject.spi.Annotated; import javax.enterprise.inject.spi.BeanManager; public class AnnotationInspector @@ -64,4 +81,26 @@ public static A getAnnotation(AnnotatedElement element, f } } + /** + * Inspects an annotated element for the given meta annotation. This should + * only be used for user defined meta annotations, where the annotation must + * be physically present. + * + * @param element The element to inspect + * @param annotationType The meta annotation to search for + * @return The annotation instance found on this method or null if no + * matching annotation was found. + */ + public static A getMetaAnnotation(Annotated element, final Class annotationType) + { + for (Annotation annotation : element.getAnnotations()) + { + if (annotation.annotationType().isAnnotationPresent(annotationType)) + { + return annotation.annotationType().getAnnotation(annotationType); + } + } + return null; + } + } diff --git a/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension index ceb58ec..ec3a889 100644 --- a/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension +++ b/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -5,3 +5,4 @@ org.jboss.weld.extensions.interceptor.InterceptorExtension org.jboss.weld.extensions.managedproducer.ManagedProducerExtension org.jboss.weld.extensions.defaultbean.DefaultBeanExtension org.jboss.weld.extensions.log.LoggerExtension +org.jboss.weld.extensions.servicehandler.ServiceHandlerExtension diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java similarity index 85% rename from impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java index d7ec63f..e264723 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoService.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java @@ -14,15 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.test.autoproxy; +package org.jboss.weld.extensions.test.servicehandler; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.jboss.weld.extensions.autoproxy.AutoProxy; +import org.jboss.weld.extensions.servicehandler.ServiceHandler; @Retention(RetentionPolicy.RUNTIME) -@AutoProxy(EchoServiceHandler.class) +@ServiceHandler(EchoServiceHandler.class) public @interface EchoService { diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java similarity index 94% rename from impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java index c69cf2e..5461ce7 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/EchoServiceHandler.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.test.autoproxy; +package org.jboss.weld.extensions.test.servicehandler; import javax.interceptor.AroundInvoke; import javax.interceptor.InvocationContext; diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java similarity index 58% rename from impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java index ff9ba87..9277afa 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/HelloWorld.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java @@ -1,4 +1,4 @@ -package org.jboss.weld.extensions.test.autoproxy; +package org.jboss.weld.extensions.test.servicehandler; @EchoService public interface HelloWorld diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java similarity index 91% rename from impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java index 2294fb2..424971d 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/autoproxy/AutoProxyTest.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.test.autoproxy; +package org.jboss.weld.extensions.test.servicehandler; import javax.inject.Inject; @@ -31,7 +31,7 @@ * */ @RunWith(Arquillian.class) -public class AutoProxyTest +public class ServiceHandlerTest { @Inject @@ -40,7 +40,7 @@ public class AutoProxyTest @Deployment public static Archive deploy() { - return ShrinkWrap.create("test.jar", JavaArchive.class).addPackage(AutoProxyTest.class.getPackage()); + return ShrinkWrap.create("test.jar", JavaArchive.class).addPackage(ServiceHandlerTest.class.getPackage()); } @Test From b455d143bd51fbb82c285d8783b89f4240290cee Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 21:14:53 +1000 Subject: [PATCH 4/6] update service handler tests --- .gitignore | 3 ++- .../servicehandler/ServiceHandler.java | 4 ++-- .../ServiceHandlerBeanLifecycle.java | 10 +++++++++- .../servicehandler/ServiceHandlerManager.java | 3 +-- .../test/servicehandler/GoodbyeWorld.java | 13 +++++++++++++ .../test/servicehandler/ServiceHandlerTest.java | 16 ++++++++++++++-- 6 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java diff --git a/.gitignore b/.gitignore index e970233..4f0682f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.project \ No newline at end of file +.project +.settings diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java index d29cda0..a17350d 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java @@ -38,9 +38,9 @@ * * * This is the same as an intercepter class. This handler can be injected into - * and use initializer methods + * and use initializer methods, however @PreDestory methods are not availble * - * @author Stuart Douglas + * @author Stuart Douglas * */ @Retention(RetentionPolicy.RUNTIME) diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java index 8ad22f9..578f0fa 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java @@ -28,6 +28,14 @@ import org.jboss.weld.extensions.bean.BeanLifecycle; +/** + * Bean lifecycle for ServiceHandler beans + * + * @author Stuart Douglas + * + * @param + * @param + */ public class ServiceHandlerBeanLifecycle implements BeanLifecycle { private final ProxyFactory factory; @@ -79,7 +87,7 @@ public T create(Bean bean, CreationalContext creationalContext) public void destroy(Bean bean, T instance, CreationalContext creationalContext) { - // handler.dispose(instance, (CreationalContext) creationalContext); + // handler.dispose(instance); } } diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java index 9d5d278..bd515c4 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java @@ -84,10 +84,9 @@ public T create(CreationalContext ctx) return instance; } - public void dispose(T instance, CreationalContext ctx) + public void dispose(T instance) { injectionTarget.dispose(instance); - ctx.release(); } public Object invoke(Object instance, InvocationContext ctx) throws Exception diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java new file mode 100644 index 0000000..d3a6bd7 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java @@ -0,0 +1,13 @@ +package org.jboss.weld.extensions.test.servicehandler; + +@EchoService +public abstract class GoodbyeWorld +{ + public String otherMethod() + { + return "not saying goodbye"; + } + + public abstract String goodbyeWorld(); + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java index 424971d..ae11c99 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java @@ -18,6 +18,8 @@ import javax.inject.Inject; +import junit.framework.Assert; + import org.jboss.arquillian.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; @@ -37,6 +39,9 @@ public class ServiceHandlerTest @Inject HelloWorld helloWorld; + @Inject + GoodbyeWorld goodbyteWorld; + @Deployment public static Archive deploy() { @@ -44,9 +49,16 @@ public static Archive deploy() } @Test - public void testExact() + public void testProxiedInterface() + { + Assert.assertTrue(helloWorld.helloWorld().equals("helloWorld")); + } + + @Test + public void testProxiedAbstractClass() { - assert helloWorld.helloWorld().equals("helloWorld"); + Assert.assertTrue(goodbyteWorld.goodbyeWorld().equals("goodbyeWorld")); + Assert.assertFalse(goodbyteWorld.otherMethod().equals("otherMethod")); } } From cf4e70ccf8c5cf19d2bcc0a78ff813f5db1df25b Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 21:21:41 +1000 Subject: [PATCH 5/6] test injection into handler --- .../servicehandler/ServiceHandlerManager.java | 5 --- .../servicehandler/DecoratedEchoService.java | 29 ++++++++++++++++ .../DecoratedEchoServiceHandler.java | 34 +++++++++++++++++++ .../servicehandler/DecoratedHelloWorld.java | 7 ++++ .../test/servicehandler/EchoDecorator.java | 25 ++++++++++++++ .../test/servicehandler/GoodbyeWorld.java | 16 +++++++++ .../test/servicehandler/HelloWorld.java | 16 +++++++++ .../servicehandler/ServiceHandlerTest.java | 9 +++++ 8 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java create mode 100644 impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java index bd515c4..0ccf01e 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java @@ -84,11 +84,6 @@ public T create(CreationalContext ctx) return instance; } - public void dispose(T instance) - { - injectionTarget.dispose(instance); - } - public Object invoke(Object instance, InvocationContext ctx) throws Exception { return handlerMethod.invoke(instance, ctx); diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java new file mode 100644 index 0000000..f39a233 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java @@ -0,0 +1,29 @@ +/* + * 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.test.servicehandler; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.jboss.weld.extensions.servicehandler.ServiceHandler; + +@Retention(RetentionPolicy.RUNTIME) +@ServiceHandler(DecoratedEchoServiceHandler.class) +public @interface DecoratedEchoService +{ + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java new file mode 100644 index 0000000..f5c803f --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java @@ -0,0 +1,34 @@ +/* + * 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.test.servicehandler; + +import javax.inject.Inject; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +public class DecoratedEchoServiceHandler +{ + // test injection into the handler + @Inject + EchoDecorator decorator; + + @AroundInvoke + public Object invoke(InvocationContext ctx) + { + return decorator.decorate(ctx.getMethod().getName().toString()); + } +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java new file mode 100644 index 0000000..a670d58 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java @@ -0,0 +1,7 @@ +package org.jboss.weld.extensions.test.servicehandler; + +@DecoratedEchoService +public interface DecoratedHelloWorld +{ + public String decoratedHelloWorld(); +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java new file mode 100644 index 0000000..f996b3f --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java @@ -0,0 +1,25 @@ +/* + * 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.test.servicehandler; + +public class EchoDecorator +{ + public String decorate(String message) + { + return "-" + message + "-"; + } +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java index d3a6bd7..a972dd8 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java @@ -1,3 +1,19 @@ +/* + * 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.test.servicehandler; @EchoService diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java index 9277afa..2e3b60e 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java @@ -1,3 +1,19 @@ +/* + * 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.test.servicehandler; @EchoService diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java index ae11c99..e6e7c10 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java @@ -42,6 +42,9 @@ public class ServiceHandlerTest @Inject GoodbyeWorld goodbyteWorld; + @Inject + DecoratedHelloWorld decoratedHelloWorld; + @Deployment public static Archive deploy() { @@ -61,4 +64,10 @@ public void testProxiedAbstractClass() Assert.assertFalse(goodbyteWorld.otherMethod().equals("otherMethod")); } + @Test + public void testInjectionIntoHandler() + { + Assert.assertTrue(decoratedHelloWorld.decoratedHelloWorld().equals("-decoratedHelloWorld-")); + } + } From 16dfd9ecf311c44189ef7fe219370690ffd552ca Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 26 Jul 2010 21:25:34 +1000 Subject: [PATCH 6/6] minor --- .../servicehandler/ServiceHandlerBeanLifecycle.java | 6 +++--- .../extensions/servicehandler/ServiceHandlerExtension.java | 2 +- .../extensions/servicehandler/ServiceHandlerManager.java | 4 ++-- .../servicehandler/ServiceHandlerMethodHandler.java | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java index 578f0fa..15bbb8e 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java @@ -50,7 +50,7 @@ public ServiceHandlerBeanLifecycle(Class classToImplement, Class factory = new ProxyFactory(); if (classToImplement.isInterface()) { - Class[] interfaces = new Class[1]; + Class[] interfaces = new Class[1]; interfaces[0] = classToImplement; factory.setInterfaces(interfaces); } @@ -73,8 +73,8 @@ public T create(Bean bean, CreationalContext creationalContext) { try { - H handlerInstance = handler.create((CreationalContext) creationalContext); - ServiceHandlerMethodHandler methodHandler = new ServiceHandlerMethodHandler(handler, handlerInstance); + H handlerInstance = handler.create((CreationalContext) creationalContext); //not sure if this is ok + ServiceHandlerMethodHandler methodHandler = new ServiceHandlerMethodHandler(handler, handlerInstance); T instance = proxyClass.newInstance(); ((ProxyObject) instance).setHandler(methodHandler); return instance; diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java index a730efa..59b91c6 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java @@ -30,7 +30,7 @@ import org.jboss.weld.extensions.util.AnnotationInspector; /** - * This extension provides an implementation of interfaces and abstract classes. + * This extension automatically implements interfaces and abstract classes. * * @author Stuart Douglas * diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java index 0ccf01e..e0a20a5 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java @@ -29,8 +29,8 @@ import org.jboss.weld.extensions.util.Reflections; /** - * Manages the handler for the auto proxy extension. This class is responsible - * for managing the lifecycle of the handler class instances + * Manages the handler class for the service handler extension. This class is + * responsible for managing the lifecycle of the handler class instances * * @author Stuart Douglas * diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java index 6ddf50d..373ceb7 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java @@ -25,7 +25,8 @@ import javax.interceptor.InvocationContext; /** - * MethodHandler that forwards calls to abstract methods to handler instance + * MethodHandler that forwards calls to abstract methods to the service handler + * instance * * @author Stuart Douglas *