camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r1380193 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/ components/camel-cdi/src/test/java/org/apache/camel/cdi/ components/camel-cdi/src/test/j...
Date Mon, 03 Sep 2012 10:39:22 GMT
Author: jstrachan
Date: Mon Sep  3 10:39:21 2012
New Revision: 1380193

URL: http://svn.apache.org/viewvc?rev=1380193&view=rev
Log:
initial spike of supporting naked @Produce annotations on CDI for CAMEL-5553

Added:
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java
  (with props)
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java?rev=1380193&r1=1380192&r2=1380193&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
Mon Sep  3 10:39:21 2012
@@ -131,7 +131,7 @@ public class DefaultCamelBeanPostProcess
     /**
      * Strategy to get the {@link CamelPostProcessorHelper}
      */
-    protected CamelPostProcessorHelper getPostProcessorHelper() {
+    public CamelPostProcessorHelper getPostProcessorHelper() {
         if (camelPostProcessorHelper == null) {
             camelPostProcessorHelper = new CamelPostProcessorHelper(getOrLookupCamelContext());
         }
@@ -178,7 +178,7 @@ public class DefaultCamelBeanPostProcess
         });
     }
 
-    protected void injectField(Field field, String endpointUri, String endpointRef, String
endpointProperty,
+    public void injectField(Field field, String endpointUri, String endpointRef, String endpointProperty,
                                Object bean, String beanName) {
         ReflectionHelper.setField(field, bean,
                 getPostProcessorHelper().getInjectionValue(field.getType(), endpointUri,
endpointRef, endpointProperty,
@@ -206,7 +206,7 @@ public class DefaultCamelBeanPostProcess
         }
     }
 
-    protected void setterInjection(Method method, Object bean, String beanName, String endpointUri,
String endpointRef, String endpointProperty) {
+    public void setterInjection(Method method, Object bean, String beanName, String endpointUri,
String endpointRef, String endpointProperty) {
         Class<?>[] parameterTypes = method.getParameterTypes();
         if (parameterTypes != null) {
             if (parameterTypes.length != 1) {

Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java?rev=1380193&r1=1380192&r2=1380193&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
(original)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
Mon Sep  3 10:39:21 2012
@@ -17,17 +17,24 @@
  */
 package org.apache.camel.component.cdi.internal;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 import javax.enterprise.inject.spi.Bean;
 
+import org.apache.camel.Produce;
+import org.apache.camel.impl.CamelPostProcessorHelper;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
+
 /**
  * Contains the bean and the consume methods
  */
 public class BeanAdapter {
     private final Bean<?> bean;
     private final List<Method> consumeMethods = new ArrayList<Method>();
+    private final List<Method> produceMethods = new ArrayList<Method>();
+    private final List<Field> produceFields = new ArrayList<Field>();
 
     public BeanAdapter(Bean<?> bean) {
         this.bean = bean;
@@ -41,7 +48,50 @@ public class BeanAdapter {
         return consumeMethods;
     }
 
+    public List<Method> getProduceMethods() {
+        return produceMethods;
+    }
+
+    public List<Field> getProduceFields() {
+        return produceFields;
+    }
+
     public void addConsumeMethod(Method method) {
         consumeMethods.add(method);
     }
+
+    public void addProduceMethod(Method method) {
+        produceMethods.add(method);
+    }
+
+    public void addProduceField(Field field) {
+        produceFields.add(field);
+    }
+
+    /**
+     * Perform processing of the various @Consume, @Produce methods on the given bean reference
+     */
+    public void initialiseBean(DefaultCamelBeanPostProcessor postProcessor, Object reference,
+                               String beanName) {
+        CamelPostProcessorHelper postProcessorHelper = postProcessor.getPostProcessorHelper();
+        for (Method method : consumeMethods) {
+            postProcessorHelper.consumerInjection(method, reference, beanName);
+        }
+        for (Method method : produceMethods) {
+            Produce produce = method.getAnnotation(Produce.class);
+            if (produce != null && postProcessorHelper.matchContext(produce.context()))
{
+                postProcessor.setterInjection(method, bean, beanName, produce.uri(), produce.ref(),
+                        produce.property());
+
+            }
+        }
+        for (Field field : produceFields) {
+            Produce produce = field.getAnnotation(Produce.class);
+            if (produce != null && postProcessorHelper.matchContext(produce.context()))
{
+                postProcessor.injectField(field, produce.uri(), produce.ref(),
+                        produce.property(), reference, beanName);
+            }
+        }
+    }
+
 }

Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java?rev=1380193&r1=1380192&r2=1380193&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
(original)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
Mon Sep  3 10:39:21 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.cdi.internal;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.HashMap;
@@ -32,12 +33,15 @@ import javax.enterprise.inject.spi.Befor
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
 import javax.enterprise.inject.spi.ProcessBean;
+import javax.inject.Inject;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.Consume;
+import org.apache.camel.Produce;
 import org.apache.camel.component.cdi.CdiCamelContext;
 import org.apache.camel.impl.CamelPostProcessorHelper;
+import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ReflectionHelper;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
@@ -125,13 +129,35 @@ public class CamelExtension implements E
                     BeanAdapter beanAdapter = getBeanAdapter(bean);
                     beanAdapter.addConsumeMethod(method);
                 }
+                Produce produce = method.getAnnotation(Produce.class);
+                if (produce != null) {
+                    BeanAdapter beanAdapter = getBeanAdapter(bean);
+                    beanAdapter.addProduceMethod(method);
+                }
+            }
+        });
+        ReflectionHelper.doWithFields(bean.getBeanClass(), new ReflectionHelper.FieldCallback()
{
+            @Override
+            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException
{
+                Produce produce = field.getAnnotation(Produce.class);
+                if (produce != null && !injectAnnotatedField(field)) {
+                    BeanAdapter beanAdapter = getBeanAdapter(bean);
+                    beanAdapter.addProduceField(field);
+                }
             }
         });
     }
 
+    /**
+     * Returns true if this field is annotated with @Inject
+     */
+    protected static boolean injectAnnotatedField(Field field) {
+        return field.getAnnotation(Inject.class) != null;
+    }
+
     public void initializeBeans(@Observes AfterDeploymentValidation event, BeanManager beanManager)
{
         ObjectHelper.notNull(getCamelContext(), "camelContext");
-        CamelPostProcessorHelper postProcessor = new CamelPostProcessorHelper(getCamelContext());
+        DefaultCamelBeanPostProcessor postProcessor = new DefaultCamelBeanPostProcessor(getCamelContext());
         Collection<BeanAdapter> adapters = beanAdapters.values();
         for (BeanAdapter adapter : adapters) {
             Bean<?> bean = adapter.getBean();
@@ -140,10 +166,7 @@ public class CamelExtension implements E
             Object reference = beanManager.getReference(bean, Object.class, creationalContext);
             String beanName = bean.getName();
 
-            List<Method> consumeMethods = adapter.getConsumeMethods();
-            for (Method method : consumeMethods) {
-                postProcessor.consumerInjection(method, reference, beanName);
-            }
+            adapter.initialiseBean(postProcessor, reference, beanName);
         }
     }
 

Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java?rev=1380193&r1=1380192&r2=1380193&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
(original)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
Mon Sep  3 10:39:21 2012
@@ -67,5 +67,15 @@ public abstract class CdiContextTestSupp
     protected CamelContext createCamelContext() throws Exception {
         return BeanProvider.getContextualReference(CamelContext.class);
     }
+
+    @Override
+    protected void applyCamelPostProcessor() throws Exception {
+        // lets do nothing and let CDI do all the injection on this
+
+        // TODO as a workaround until we support backwards compatible injection
+        // on @Produce / @EndpointInject without the use of @Inject
+        // lets keep the old behaviour
+        super.applyCamelPostProcessor();
+    }
 }
 

Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java?rev=1380193&view=auto
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java
(added)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java
Mon Sep  3 10:39:21 2012
@@ -0,0 +1,45 @@
+/**
+ * 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.camel.cdi;
+
+import javax.inject.Inject;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.cdi.support.EndpointInjectedBean;
+import org.apache.camel.cdi.support.ProduceInjectedBean;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Test endpoint injection
+ */
+public class ProduceInjectTest extends CdiTestSupport {
+
+    @Inject
+    private ProduceInjectedBean bean;
+
+    @Ignore
+    public void shouldInjectEndpoint() {
+        assertNotNull(bean);
+        ProducerTemplate producer = bean.getProducer();
+        assertNotNull("Could not find injected producer!", producer);
+        assertEquals("producer default URI", "mock://foo", producer.getDefaultEndpoint().getEndpointUri());
+    }
+
+}

Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ProduceInjectTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java?rev=1380193&view=auto
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java
(added)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java
Mon Sep  3 10:39:21 2012
@@ -0,0 +1,35 @@
+/**
+ * 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.camel.cdi.support;
+
+import javax.inject.Inject;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.cdi.Mock;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class ProduceInjectedBean {
+
+    @Produce(uri = "mock:foo")
+    private ProducerTemplate producer;
+
+    public ProducerTemplate getProducer() {
+        return producer;
+    }
+}

Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ProduceInjectedBean.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message